Debug chunk save/load commands.

This commit is contained in:
Avi Weinstock 2021-06-19 00:50:50 -04:00
parent 756df4b2e0
commit 114fba97dc

View File

@ -1033,23 +1033,31 @@ impl PlayState for SessionState {
self.client.borrow_mut().send_chat(msg); self.client.borrow_mut().send_chat(msg);
}, },
HudEvent::SendCommand(name, args) => { HudEvent::SendCommand(name, args) => {
use common::{terrain::TerrainGrid, volumes::dyna::Dyna}; use common::{
terrain::{sprite::SpriteKind, TerrainGrid},
volumes::dyna::{ColumnAccess, Dyna},
};
use common_net::msg::compression::{ use common_net::msg::compression::{
image_terrain_chonk, write_image_terrain, CompressedData, image_terrain_chonk, write_image_terrain, CompressedData,
GridLtrPacking, QuadPngEncoding, GridLtrPacking, QuadPngEncoding,
}; };
use std::{fs::File, io::Write}; use std::{
fs::File,
io::{Read, Write},
str::FromStr,
};
match &*name { match &*name {
"save_chunk" => { "save_chunk" => {
if let Some(filename) = args.get(0) { if let Some(filename) = args.get(0) {
let client = self.client.borrow(); let client = self.client.borrow();
let player = client.entity(); let player = client.entity();
let ecs = client.state().ecs(); let ecs = client.state().ecs();
let pos = ecs.read_storage::<Pos>().get(player).unwrap(); let positions = ecs.read_storage::<Pos>();
let pos = positions.get(player).unwrap();
let terrain = ecs.read_resource::<TerrainGrid>(); let terrain = ecs.read_resource::<TerrainGrid>();
let chunk_key = terrain.pos_key(pos.0.xy().as_()); let pos_xy: Vec3<i32> = pos.0.as_();
let chunk_key = terrain.pos_key(pos_xy);
let chunk = terrain.get_key(chunk_key).unwrap(); let chunk = terrain.get_key(chunk_key).unwrap();
let (pngs, indices) = image_terrain_chonk( let (pngs, indices) = image_terrain_chonk(
QuadPngEncoding::<1>(), QuadPngEncoding::<1>(),
GridLtrPacking, GridLtrPacking,
@ -1072,45 +1080,59 @@ impl PlayState for SessionState {
} }
}, },
"load_chunk" => { "load_chunk" => {
if let (Some(filename), Some(z_off)) = if let (Some(filename), Some(z_off)) = (
(args.get(0), args.get(1).and_then(i32::parse)) args.get(0),
{ args.get(1).and_then(|s| i32::from_str(&*s).ok()),
let client = self.client.borrow(); ) {
let mut client = self.client.borrow_mut();
let player = client.entity(); let player = client.entity();
let ecs = client.state().ecs(); let ecs = client.state().ecs();
let pos = ecs.read_storage::<Pos>().get(player).unwrap(); let positions = ecs.read_storage::<Pos>();
let terrain = ecs.read_resource::<TerrainGrid>(); let pos = positions.get(player).unwrap();
let chunk_key = terrain.pos_key(pos.0.xy().as_()); let ipos: Vec3<i32> = pos.0.as_();
let chunk_key = TerrainGrid::chunk_key(ipos);
drop(positions);
drop(ecs);
let mut pngs = Vec::new(); let mut pngs = Vec::new();
let mut indices = [0; 3]; let mut indices = [0; 3];
let mut height = 0;
for i in 0..4 { for i in 0..4 {
let file = use image::{codecs::png::PngDecoder, ImageDecoder};
let mut file =
File::open(&format!("{}_{}.png", filename, i)).unwrap(); File::open(&format!("{}_{}.png", filename, i)).unwrap();
let _ = file.read_to_end(&mut pngs); let _ = file.read_to_end(&mut pngs);
if i == 0 {
let decoder = PngDecoder::new(&pngs[..]).unwrap();
let (w, h) = decoder.dimensions();
height = (w * h) / (32 * 32);
}
if i < 3 { if i < 3 {
indices[i] = pngs.len(); indices[i] = pngs.len();
} }
} }
let dyna = let mut dyna: Dyna<Block, (), ColumnAccess> = Dyna::filled(
Dyna::filled(Vec3::new(32, 32, 512), Block::air(), ()); Vec3::new(32, 32, height),
Block::air(SpriteKind::Empty),
(),
);
write_image_terrain( write_image_terrain(
QuadPngEncoding::<1>(), QuadPngEncoding::<1>(),
GridLtrPacking, GridLtrPacking,
&mut dyna, &mut dyna,
CompressedData::compress((pngs, indices, 1)), &CompressedData::compress(&(pngs, indices), 1),
Vec3::new(0, 0, 0), Vec3::new(0, 0, 0),
Vec3::new(32, 32, 512), Vec3::new(32, 32, height),
); );
for i in 0..32 { for i in 0..32 {
for j in 0..32 { for j in 0..32 {
for k in 0..512 { for k in 0..height as i32 {
let blockpos = Vec3::new( let blockpos = Vec3::new(
chunk_key + i, 32 * chunk_key.x + i,
chunk_key + j, 32 * chunk_key.y + j,
k + z_off, ipos.z + k + z_off,
); );
if let Some(block) = dyna.get(Vec3::new(i, j, k)) { if let Ok(block) = dyna.get(Vec3::new(i, j, k)) {
client.place_block(blockpos, block); client.place_block(blockpos, *block);
} }
} }
} }