Added chunk list basic code

This commit is contained in:
2021-04-02 22:33:41 +11:00
parent c57f003ac5
commit 70099a59d8
4 changed files with 132 additions and 88 deletions

View File

@ -1,58 +0,0 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunk.h"
chunklist_t * chunksCreate(uint32_t width,uint32_t height, uint32_t depth) {
chunklist_t *chunks;
uint32_t i;
chunks = malloc(sizeof(chunklist_t));
if(!chunks) return NULL;
chunks->width = width;
chunks->height = height;
chunks->depth = depth;
chunks->count = width * height * depth;
chunks->indexes = malloc(sizeof(uint32_t) * chunks->count);
if(!chunks->indexes) {
free(chunks);
return NULL;
}
for(i = 0; i < chunks->count; i++) chunks->indexes[i] = i;
return chunks;
}
void chunksDispose(chunklist_t *list) {
free(list->indexes);
free(list);
}
void chunksShift(chunklist_t *list, uint32_t direction[3]) {
uint32_t i, x, y, z, my, mz;
// Precalculate these values
my = list->width * list->height;
mz = my * list->depth;
// For each chunk
for(i = 0; i < list->count; i++) {
// Precalculate, store here for now.
x = i / list->width;
// Calculate the new xyz for the current index.
y = (x + direction[0]) % list->height;
z = (x / list->height + direction[1]) % list->depth;
x = (i + direction[2]) % list->width;
// Set back into the indexes array.
list->indexes[i] = (x * list->width) + (y * my) + (z * mz);
}
}

View File

@ -5,35 +5,7 @@
#pragma once
#include <stdint.h>
#include <malloc.h>
typedef struct {
uint32_t width, height, depth;
uint32_t count;
uint32_t *indexes;
} chunklist_t;
/**
* Create a new chunk manager.
*
* @param width Width of the chunk list.
* @param height Height of the chunk list.
* @param depth Depth of the chunk list.
* @returns The new chunk list manager
*/
chunklist_t * chunksCreate(uint32_t width,uint32_t height, uint32_t depth);
/**
* Cleans up a previously created chunk list.
*
* @param list List to dispose.
*/
void chunksDispose(chunklist_t *list);
/**
* Shift the chunk list along a set of axis.
*
* @param list List to shift.
* @param direction Array of directions to shift along.
*/
void chunksShift(chunklist_t *list, uint32_t direction[3]);
int32_t x, y, z;
} chunk_t;

View File

@ -0,0 +1,75 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "chunklist.h"
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth) {
chunklist_t *list;
int32_t i;
list = malloc(sizeof(chunklist_t));
if(!list) return NULL;
list->width = width, list->height = height, list->depth = depth;
list->x = 0, list->y = 0, list->z = 0;
list->count = width * height * depth;
//Empty chunks.
list->chunks = calloc(list->count, sizeof(chunk_t));
if(list->chunks == NULL) {
free(list);
return NULL;
}
return list;
}
void chunkListDispose(chunklist_t *list) {
free(list->chunks);
free(list);
}
void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z) {
int32_t i, lx, ly, lz, nx, ny, nz, ni, wh;
chunk_t *chunk;
chunk_t **chunks;
// Calculate the new chunklist coordinates
lx = list->x - x;
ly = list->y - y;
lz = list->z - z;
// Precalc width * height.
wh = list->width * list->height;
chunks = calloc(list->count, sizeof(chunk_t));
for(i = 0; i < list->count; i++) {
chunk = list->chunks[i];
if(chunk == NULL) continue;
// Calculate the new local positions for the chunk.
nx = chunk->x - list->x - x;// chunk abs x - list abs x - move x
ny = chunk->y - list->y - y;
nz = chunk->z - list->z - y;
// Set the chunks' new absolute positions
chunk->x = nx + lx;
chunk->y = ny + ly;
chunk->z = nz + lz;
// Now here we can actually determine which chunk(s) were moved.
//Now, based off those new local positions, calculate the new index.
ni = nx + (ny * list->width) + (nz * wh);
chunks[ni] = chunk;
}
// Now copy that array over.
memcpy(list->chunks, chunks, sizeof(chunk_t) * list->count);
free(chunks);//cleanup
}

View File

@ -0,0 +1,55 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include <stdint.h>
#include <malloc.h>
#include <string.h>
#include "chunk.h"
#define CHUNK_INDEX_NULL = 0
typedef struct {
/** Dimensions of the chunk list */
int32_t width, height, depth;
/** Position of index 0 */
int32_t x, y, z;
/** Count of chunks in the internal array */
int32_t count;
/** Chunks in the list */
int32_t **chunks;
} chunklist_t;
/**
* Creates a new chunk list. Chunk lists are managed to be memory efficient by
* only keeping a small fraction of the overall size loaded at any given time.
*
* @param width The width (x axis) of chunks to keep loaded.
* @param height The height (y axis) of chunks to keep loaded.
* @param depth The depth (z axis) of chunks to keep loaded.
* @returns A new chunk list.
*/
chunklist_t * chunkListCreate(int32_t width, int32_t height, int32_t depth);
/**
* Disposes and frees a previously created list. This does not free the chunks
* themselves.
* @param list The list to free.
*/
void chunkListDispose(chunklist_t *list);
/**
* Shift the chunk list along a set of axis (in absolute space).
*
* @param list List to shift.
* @param x X movement to shift chunk along.
* @param y Y movement to shift chunk along.
* @param z Z movement to shift chunk along.
*/
void chunkListShift(chunklist_t *list, int32_t x, int32_t y, int32_t z);