Last active
February 28, 2016 22:47
-
-
Save phase/3408562a0a0305664190 to your computer and use it in GitHub Desktop.
[PhaseBot](https://github.com/phase/phasebot) Rust stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
struct Vec3d { | |
int x; | |
int y; | |
int z; | |
}; | |
typedef struct Chunk { | |
int x; | |
int y; | |
int z; | |
int blocks[16][16][16]; | |
} Chunk; | |
typedef struct { | |
Chunk* chunks; | |
size_t nChunks; | |
size_t size; | |
size_t blockSize; | |
} chunks; | |
chunks* createChunkList(size_t blockSize) { | |
chunks* chunkList = malloc(sizeof(chunks)); | |
if(NULL != chunkList) { | |
chunkList->nChunks = 0; | |
chunkList->size = blockSize; | |
chunkList->blockSize = blockSize; | |
chunkList->chunks = malloc(sizeof(Chunk)*blockSize); | |
if(NULL == chunkList->chunks) { | |
free(chunkList); | |
return NULL; | |
} | |
} | |
return chunkList; | |
} | |
void deleteChunkList(chunks* chunkList) { | |
free(chunkList->chunks); | |
free(chunkList); | |
} | |
int addChunkToChunkList(chunks* chunkList, Chunk c) { | |
size_t nChunks = chunkList->nChunks; | |
if(nChunks >= chunkList->size) { | |
size_t newSize = chunkList->size + chunkList->blockSize; | |
void* newChunks = realloc(chunkList->chunks, sizeof(Chunk)*newSize); | |
if(NULL == newChunks) { | |
return 0; | |
} | |
else { | |
chunkList->size = newSize; | |
chunkList->chunks = (Chunk*)newChunks; | |
} | |
} | |
chunkList->chunks[nChunks] = c; | |
++chunkList->nChunks; | |
return 1; | |
} | |
Chunk* getChunkListStart(chunks* chunkList) { | |
return chunkList->chunks; | |
} | |
Chunk* getChunkListEnd(chunks* chunkList) { | |
return &chunkList->chunks[chunkList->nChunks]; | |
} | |
chunks* chunkList; | |
void initNative() { | |
chunkList = createChunkList(8); | |
} | |
void addChunk(int x, int y, int z, int (*blocks)[16][16][16]) { | |
Chunk chunk; | |
chunk.x = x; | |
chunk.y = y; | |
chunk.z = z; | |
chunk.blocks = blocks; | |
addChunkToChunkList(chunkList, chunk); | |
} | |
void updateBlock(int x, int y, int z, int id) { | |
int cx = x > 0 ? floor(x / 16) : ceil(x / 16) - 1; | |
int cy = floor(y / 16); | |
int cz = z > 0 ? floor(z / 16) : ceil(z / 16) - 1; | |
Chunk** iter; | |
for(iter = getChunkListStart(chunkList); iter != getChunkListEnd(chunkList); ++iter) { | |
if(*iter->x == cx && *iter->y == cy && *iter->z == cz) { | |
*iter->blocks[x][y][z] = id; | |
return; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![crate_type = "dylib"] | |
#[macro_use] | |
extern crate lazy_static; | |
use std::sync::Mutex; | |
use std::collections::HashSet; | |
#[repr(C)] | |
pub struct Vec3d { | |
x: f64, | |
y: f64, | |
z: f64, | |
} | |
#[derive(Eq, PartialEq, Hash)] | |
struct Chunk { | |
x: i32, | |
y: i32, | |
z: i32, | |
blocks: [[[i32; 16]; 16]; 16], | |
} | |
lazy_static! { | |
static ref CHUNKS: Mutex<HashSet<Chunk>> = Mutex::new(HashSet::new()); | |
} | |
pub fn get_chunk<'a>(cx: i32, cy: i32, cz: i32) -> &Chunk { | |
for c in CHUNKS.lock().unwrap().iter() { | |
if c.x == cx && c.y == cy && c.z == cz { | |
return c; | |
} | |
} | |
} | |
#[no_mangle] | |
pub extern fn add_chunk(cx: i32, cy: i32, cz: i32, c_blocks: [[[i32; 16]; 16]; 16]) { | |
CHUNKS.lock().unwrap().insert(Chunk {x: cx, y: cy, z: cz, blocks: c_blocks}); | |
} | |
#[no_mangle] | |
pub extern fn update_block(x: i32, y: i32, z: i32, id: i32) { | |
//CHUNKS.lock().unwrap(). | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment