forked from StrafesNET/strafe-ai
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
c5ec89093d
|
|||
|
1fab9524bd
|
|||
|
77fcb52731
|
|||
|
5902f4fb1b
|
|||
|
1f7dbbfee9
|
|||
|
f496b0ecb0
|
|||
|
d60ba4da04
|
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -5451,6 +5451,7 @@ name = "strafe-ai"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"burn",
|
||||
"png",
|
||||
"pollster",
|
||||
"strafesnet_common",
|
||||
"strafesnet_graphics",
|
||||
|
||||
@@ -14,3 +14,4 @@ strafesnet_roblox_bot_file = { version = "0.9.4", registry = "strafesnet" }
|
||||
strafesnet_roblox_bot_player = { version = "=0.6.2-depth", registry = "strafesnet" }
|
||||
strafesnet_snf = { version = "0.4.0", registry = "strafesnet" }
|
||||
pollster = "0.4.0"
|
||||
png = "0.18.1"
|
||||
|
||||
48
src/main.rs
48
src/main.rs
@@ -11,6 +11,21 @@ const LIMITS: wgpu::Limits = wgpu::Limits::defaults();
|
||||
use strafesnet_graphics::setup;
|
||||
use strafesnet_roblox_bot_file::v0;
|
||||
|
||||
pub fn output_image_native(image_data: &[u8], texture_dims: (usize, usize), path: String) {
|
||||
use std::io::Write;
|
||||
|
||||
let mut png_data = Vec::<u8>::with_capacity(image_data.len());
|
||||
let mut encoder =
|
||||
png::Encoder::new(&mut png_data, texture_dims.0 as u32, texture_dims.1 as u32);
|
||||
encoder.set_color(png::ColorType::Grayscale);
|
||||
let mut png_writer = encoder.write_header().unwrap();
|
||||
png_writer.write_image_data(image_data).unwrap();
|
||||
png_writer.finish().unwrap();
|
||||
|
||||
let mut file = std::fs::File::create(&path).unwrap();
|
||||
file.write_all(&png_data).unwrap();
|
||||
}
|
||||
|
||||
const SIZE_X: usize = 64;
|
||||
const SIZE_Y: usize = 36;
|
||||
const INPUT: usize = SIZE_X * SIZE_Y;
|
||||
@@ -89,6 +104,9 @@ fn training() {
|
||||
.unwrap();
|
||||
let timelines =
|
||||
strafesnet_roblox_bot_file::v0::read_all_to_block(std::io::Cursor::new(bot_file)).unwrap();
|
||||
let bot = strafesnet_roblox_bot_player::bot::CompleteBot::new(timelines).unwrap();
|
||||
let world_offset = bot.world_offset();
|
||||
let timelines = bot.timelines();
|
||||
|
||||
// setup graphics
|
||||
let desc = wgpu::InstanceDescriptor::new_without_display_handle_from_env();
|
||||
@@ -129,7 +147,9 @@ fn training() {
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
|
||||
| wgpu::TextureUsages::TEXTURE_BINDING
|
||||
| wgpu::TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
});
|
||||
let graphics_texture_view = graphics_texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
@@ -164,7 +184,7 @@ fn training() {
|
||||
let mut last_mx = first.event.mouse_pos.x;
|
||||
let mut last_my = first.event.mouse_pos.y;
|
||||
|
||||
for input_event in it {
|
||||
for (i, input_event) in it.enumerate() {
|
||||
let mouse_dx = input_event.event.mouse_pos.x - last_mx;
|
||||
let mouse_dy = input_event.event.mouse_pos.y - last_my;
|
||||
last_mx = input_event.event.mouse_pos.x;
|
||||
@@ -235,6 +255,9 @@ fn training() {
|
||||
fn a(a: v0::Vector3) -> [f32; 2] {
|
||||
[a.y, a.x]
|
||||
}
|
||||
fn add<T: core::ops::Add>(lhs: T, rhs: T) -> T::Output {
|
||||
lhs + rhs
|
||||
}
|
||||
|
||||
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("wgpu encoder"),
|
||||
@@ -244,14 +267,14 @@ fn training() {
|
||||
graphics.encode_commands(
|
||||
&mut encoder,
|
||||
&graphics_texture_view,
|
||||
p(output_event.event.position).into(),
|
||||
add(world_offset, p(output_event.event.position).into()),
|
||||
a(output_event.event.angles).into(),
|
||||
);
|
||||
|
||||
// copy the depth texture into ram
|
||||
encoder.copy_texture_to_buffer(
|
||||
wgpu::TexelCopyTextureInfo {
|
||||
texture: graphics.depth_texture(),
|
||||
texture: &graphics_texture,
|
||||
mip_level: 0,
|
||||
origin: wgpu::Origin3d::ZERO,
|
||||
aspect: wgpu::TextureAspect::All,
|
||||
@@ -262,7 +285,7 @@ fn training() {
|
||||
offset: 0,
|
||||
// This needs to be a multiple of 256.
|
||||
bytes_per_row: Some(stride_size as u32),
|
||||
rows_per_image: Some(size.y),
|
||||
rows_per_image: None,
|
||||
},
|
||||
},
|
||||
wgpu::Extent3d {
|
||||
@@ -288,16 +311,29 @@ fn training() {
|
||||
}
|
||||
output_staging_buffer.unmap();
|
||||
|
||||
println!("{texture_data:?}");
|
||||
|
||||
// discombolulate stride
|
||||
for y in 0..size.y {
|
||||
inputs.extend(
|
||||
texture_data[(stride_size * y) as usize
|
||||
..(stride_size * y + size.x * size_of::<f32>() as u32) as usize]
|
||||
.chunks_exact(4)
|
||||
.map(|b| f32::from_le_bytes(b.try_into().unwrap())),
|
||||
.map(|b| b[0] as f32 + b[1] as f32 + b[2] as f32),
|
||||
)
|
||||
}
|
||||
|
||||
// write a png
|
||||
output_image_native(
|
||||
&inputs[i * INPUT..(i + 1) * INPUT]
|
||||
.iter()
|
||||
.copied()
|
||||
.map(|f| f as u8)
|
||||
.collect::<Vec<u8>>(),
|
||||
(SIZE_X, SIZE_Y),
|
||||
format!("depth_images/{i}.png").into(),
|
||||
);
|
||||
|
||||
texture_data.clear();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user