diff options
author | pommicket <pommicket@gmail.com> | 2024-08-10 21:07:21 -0400 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2024-08-10 21:10:23 -0400 |
commit | 8ad005478a0dfcb187bb1ba65668977ae8ef4cf2 (patch) | |
tree | 9973b40c23639f2de6adab5dd8e45365012d3955 | |
parent | 24564afb32d2fd65c3d39d1114764bda831d1563 (diff) |
nicer starting positions
-rw-r--r-- | game.js | 4 | ||||
-rw-r--r-- | server/src/main.rs | 27 |
2 files changed, 18 insertions, 13 deletions
@@ -379,10 +379,10 @@ window.addEventListener('load', function () { } let nibTypeIndex = 0; if (playArea.clientWidth / puzzleWidth < playArea.clientHeight / puzzleHeight) { - pieceWidth = 0.5 * playArea.clientWidth / puzzleWidth; + pieceWidth = 0.6 * playArea.clientWidth / puzzleWidth; pieceHeight = pieceWidth * (puzzleWidth / puzzleHeight) * (image.height / image.width); } else { - pieceHeight = 0.5 * playArea.clientHeight / puzzleHeight; + pieceHeight = 0.6 * playArea.clientHeight / puzzleHeight; pieceWidth = pieceHeight * (puzzleHeight / puzzleWidth) * (image.width / image.height); } nibSize = Math.min(pieceWidth / 4, pieceHeight / 4); diff --git a/server/src/main.rs b/server/src/main.rs index 1e734b9..442b6bf 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -116,21 +116,26 @@ async fn handle_connection( } drop(puzzle_data); // should be empty now puzzle_id = Some(id); - let mut pieces_data: Vec<u8>; + let pieces_data: Box<[u8]>; { let mut rng = rand::thread_rng(); - pieces_data = Vec::new(); - pieces_data.resize((width as usize) * (height as usize) * 8, 0); + let mut positions = vec![]; + positions.reserve_exact((width as usize) * (height as usize)); // positions - let mut it = pieces_data.iter_mut(); - for _ in 0..(width as u16) * (height as u16) * 2 { - let coord: f32 = rng.gen(); - let [a, b, c, d] = coord.to_le_bytes(); - *it.next().unwrap() = a; - *it.next().unwrap() = b; - *it.next().unwrap() = c; - *it.next().unwrap() = d; + for y in 0..(height as u16) { + for x in 0..(width as u16) { + let dx: f32 = rng.gen_range(0.0..0.5); + let dy: f32 = rng.gen_range(0.0..0.5); + positions.push([(x as f32 + dx) / ((width + 1) as f32), (y as f32 + dy) / ((height + 1) as f32)]); + } } + positions.shuffle(&mut rng); + // rust isn't smart enough to do the zero-copy with f32::to_le_bytes and Vec::into_flattened + let ptr: *mut [[f32; 2]] = Box::into_raw(positions.into_boxed_slice()); + let ptr: *mut [u8] = std::ptr::slice_from_raw_parts_mut(ptr.cast(), (width as usize) * (height as usize) * 8); + // evil unsafe code >:3 + pieces_data = unsafe { Box::from_raw(ptr) }; + } server.pieces.insert(id, pieces_data)?; let mut connectivity_data = Vec::new(); |