re-coded hash tables to match type + cleaned some fct
This commit is contained in:
parent
8a6f109d8a
commit
69a16c4920
|
@ -2,6 +2,7 @@
|
|||
"files.associations": {
|
||||
"ncurses.h": "c",
|
||||
"stdio.h": "c",
|
||||
"math.h": "c"
|
||||
"math.h": "c",
|
||||
"sdl_image.h": "c"
|
||||
}
|
||||
}
|
BIN
obj/base.o
BIN
obj/base.o
Binary file not shown.
BIN
obj/display.o
BIN
obj/display.o
Binary file not shown.
BIN
obj/hash.o
BIN
obj/hash.o
Binary file not shown.
BIN
obj/main.o
BIN
obj/main.o
Binary file not shown.
BIN
obj/move.o
BIN
obj/move.o
Binary file not shown.
60
src/base.c
60
src/base.c
|
@ -109,66 +109,6 @@ int str_to_int(char* s) {
|
|||
return res;
|
||||
}
|
||||
|
||||
int get_integer(FILE* ptr) {
|
||||
char c = fgetc(ptr);
|
||||
|
||||
int res = 0 ;
|
||||
int sign = 1;
|
||||
|
||||
if(c == '-') {
|
||||
sign = -1;
|
||||
c = fgetc(ptr);
|
||||
};
|
||||
|
||||
while(is_an_integer(c)) {
|
||||
res = 10*res + (int)c - 48;
|
||||
c = fgetc(ptr);
|
||||
};
|
||||
return (res*sign);
|
||||
}
|
||||
|
||||
int get_integer_plus_align(FILE* ptr, FILE* ptr2) {
|
||||
char c = fgetc(ptr);
|
||||
char c2 = fgetc(ptr2);
|
||||
|
||||
int res = 0 ;
|
||||
int sign = 1;
|
||||
|
||||
if(c == '-') {
|
||||
sign = -1;
|
||||
c = fgetc(ptr);
|
||||
c2 = fgetc(ptr2);
|
||||
};
|
||||
|
||||
while(is_an_integer(c)) {
|
||||
res = 10*res + (int)c - 48;
|
||||
c = fgetc(ptr);
|
||||
c2 = fgetc(ptr2);
|
||||
};
|
||||
return (res*sign);
|
||||
}
|
||||
|
||||
int count_char_in_line(FILE* ptr, char target) {
|
||||
int res = 0 ;
|
||||
char c = fgetc(ptr);
|
||||
|
||||
while(c != EOF && c != '\n') {
|
||||
if(c == target) {
|
||||
res += 1;
|
||||
}
|
||||
c = fgetc(ptr);
|
||||
};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void terminate_line(FILE* ptr) {
|
||||
char c = fgetc(ptr);
|
||||
while(c != '\n' && c != EOF) {
|
||||
c = fgetc(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool str_equal(char* s1, char* s2) {
|
||||
if(s1[0] == '\0' || s2[0] == '\0') {
|
||||
return (s1[0] == '\0' && s2[0] == '\0');
|
||||
|
|
|
@ -13,10 +13,6 @@ double to_double(int n);
|
|||
int to_int(double n);
|
||||
int line_count(char* filename);
|
||||
int str_to_int(char* s);
|
||||
int get_integer(FILE* ptr);
|
||||
int get_integer_plus_align(FILE* ptr, FILE* ptr2);
|
||||
int count_char_in_line(FILE* ptr, char c);
|
||||
void terminate_line(FILE* ptr);
|
||||
bool str_equal(char* s1, char* s2);
|
||||
|
||||
cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, int r, int g, int b);
|
||||
|
|
251
src/hash.c
251
src/hash.c
|
@ -11,9 +11,66 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
#include "structure.h"
|
||||
#include "hash.h"
|
||||
|
||||
// Cantor pairing function
|
||||
// ------------------------------------------------------------------------------------------------------- //
|
||||
|
||||
bool linkedList_mem(linkedList lst, int chx, int chy) {
|
||||
if(lst == NULL) {
|
||||
return false ;
|
||||
} else if(lst->chx == chx && lst->chy == chy) {
|
||||
return true ;
|
||||
}
|
||||
return linkedList_mem(lst->next, chx, chy) ;
|
||||
}
|
||||
|
||||
room linkedList_find(linkedList lst, int chx, int chy) {
|
||||
if(lst == NULL) {
|
||||
fprintf(stderr, "ERROR : (%d, %d) is not a valid key in hash\n", chx, chy);
|
||||
exit(1);
|
||||
} else if(lst->chx == chx && lst->chy == chy) {
|
||||
return *(lst->area) ;
|
||||
}
|
||||
return linkedList_find(lst->next, chx, chy) ;
|
||||
}
|
||||
|
||||
room* linkedList_find_opt(linkedList lst, int chx, int chy) {
|
||||
if(lst == NULL) {
|
||||
return NULL ;
|
||||
} else if(lst->chx == chx && lst->chy == chy) {
|
||||
return (lst->area) ;
|
||||
}
|
||||
return linkedList_find_opt(lst->next, chx, chy) ;
|
||||
}
|
||||
|
||||
void linkedList_add(linkedList lst, int chx, int chy, room* area) {
|
||||
if(lst == NULL) {
|
||||
fprintf(stderr, "ERROR : cannot add to NULL\n");
|
||||
exit(1) ;
|
||||
} else if(lst->next == NULL) {
|
||||
cell* nex = malloc(sizeof(cell));
|
||||
nex->area = area ;
|
||||
nex->chx = chx ;
|
||||
nex->chy = chy ;
|
||||
nex->next = NULL ;
|
||||
lst->next = nex ;
|
||||
} else {
|
||||
linkedList_add(lst->next, chx, chy, area) ;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedList_free(linkedList lst) {
|
||||
// frees rooms as well (require all created rooms to be there)
|
||||
if(lst != NULL) {
|
||||
linkedList_free(lst->next);
|
||||
free(lst->area);
|
||||
free(lst);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------- //
|
||||
|
||||
int cantor(int x, int y) {
|
||||
return ((x + y) * (x + y + 1) / 2) + y;
|
||||
}
|
||||
|
@ -32,165 +89,51 @@ int generate_hash(int x, int y) {
|
|||
return cantor(x, y);
|
||||
}
|
||||
|
||||
void freeGrid(Grid grid) {
|
||||
for (int i = 0; i < grid->capacity; i++) {
|
||||
List list = grid->data[i];
|
||||
while (list != NULL) {
|
||||
List next = list->next;
|
||||
free(list);
|
||||
list = next;
|
||||
hashtbl hashtbl_generate(int initial_size) {
|
||||
hashtbl h = malloc(sizeof(hashtbl_0));
|
||||
h->insertedElts = 0 ;
|
||||
h->tabLength = initial_size ;
|
||||
h->maxInserted = initial_size/2 ;
|
||||
h->tab = malloc(sizeof(linkedList)*initial_size) ;
|
||||
for(int k = 0; k < initial_size; k++) {
|
||||
h->tab[k] = NULL ;
|
||||
}
|
||||
}
|
||||
free(grid->data);
|
||||
free(grid);
|
||||
return h ;
|
||||
}
|
||||
|
||||
void subGridInsert(Grid grid, int hash, chunk* elt, int x, int y) {
|
||||
int index = hash % grid->capacity;
|
||||
//printf(" inserting at index [%d]", index);
|
||||
List* data = grid->data;
|
||||
if (data[index] == NULL) {
|
||||
//printf(" <-->\n");
|
||||
List list = malloc(sizeof(struct List));
|
||||
list->hash = hash;
|
||||
list->x = x;
|
||||
list->y = y;
|
||||
list->elt = elt;
|
||||
list->next = NULL;
|
||||
grid->size++;
|
||||
data[index] = list;
|
||||
bool hashtbl_mem(hashtbl tbl, int chx, int chy) {
|
||||
int id = generate_hash(chx, chy)%(tbl->tabLength) ;
|
||||
return linkedList_mem(tbl->tab[id], chx, chy) ;
|
||||
}
|
||||
|
||||
room hashtbl_find(hashtbl tbl, int chx, int chy) {
|
||||
int id = generate_hash(chx, chy)%(tbl->tabLength) ;
|
||||
return linkedList_find(tbl->tab[id], chx, chy) ;
|
||||
}
|
||||
|
||||
room* hashtbl_find_opt(hashtbl tbl, int chx, int chy) {
|
||||
int id = generate_hash(chx, chy)%(tbl->tabLength) ;
|
||||
return linkedList_find_opt(tbl->tab[id], chx, chy) ;
|
||||
}
|
||||
|
||||
void hashtbl_add(hashtbl tbl, int chx, int chy, room* area) {
|
||||
int id = generate_hash(chx, chy)%(tbl->tabLength) ;
|
||||
tbl->insertedElts += 1 ;
|
||||
if(tbl->tab[id] != NULL) {
|
||||
linkedList_add(tbl->tab[id], chx, chy, area);
|
||||
} else {
|
||||
//printf(" <>\n");
|
||||
List list = data[index];
|
||||
while (list->next != NULL && list->hash != hash) {
|
||||
list = list->next;
|
||||
};
|
||||
//printf(" <>\n");
|
||||
if (list->x == x && list->y == y) {
|
||||
list->elt = elt;
|
||||
} else {
|
||||
List nex = malloc(sizeof(struct List));
|
||||
nex->hash = hash;
|
||||
nex->x = x;
|
||||
nex->y = y;
|
||||
nex->elt = elt;
|
||||
nex->next = NULL;
|
||||
list->next = nex;
|
||||
grid->size++;
|
||||
}
|
||||
linkedList nex = malloc(sizeof(cell));
|
||||
nex->area = area ;
|
||||
nex->chx = chx ;
|
||||
nex->chy = chy ;
|
||||
nex->next = NULL ;
|
||||
tbl->tab[id] = nex ;
|
||||
}
|
||||
}
|
||||
|
||||
void gridInsert(Grid grid, int x, int z, chunk* elt) {
|
||||
if (grid->size >= grid->capacity * grid->loadFactor) {
|
||||
grid->size = 0;
|
||||
grid->capacity *= 2;
|
||||
|
||||
printf("resizing\n");
|
||||
|
||||
List* old_data = grid->data;
|
||||
grid->data = malloc(sizeof(List) * grid->capacity);;
|
||||
for (int i = 0; i < grid->capacity; i++) {
|
||||
grid->data[i] = NULL;
|
||||
};
|
||||
for (int i = 0; i < grid->capacity/2; i++) {
|
||||
List list = old_data[i];
|
||||
while (list != NULL) {
|
||||
subGridInsert(grid, list->hash, list->elt, list->x, list->y);
|
||||
list = list->next;
|
||||
void hashtbl_free(hashtbl tbl) {
|
||||
for(int k = 0; k < tbl->tabLength; k++) {
|
||||
free(tbl->tab[k]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int hash = generate_hash(x, z);
|
||||
subGridInsert(grid, hash, elt, x, z);
|
||||
}
|
||||
|
||||
chunk* gridGet(Grid grid, int x, int z) {
|
||||
int hash = generate_hash(x, z);
|
||||
int index = hash % grid->capacity;
|
||||
List list = grid->data[index];
|
||||
while (list != NULL && (list->x != x || list->y != z)) {
|
||||
list = list->next;
|
||||
}
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return list->elt;
|
||||
}
|
||||
|
||||
bool gridMem(Grid grid, int x, int z) {
|
||||
int hash = generate_hash(x, z);
|
||||
int index = hash % grid->capacity;
|
||||
List list = grid->data[index];
|
||||
while (list != NULL && (list->x != x || list->y != z)) {
|
||||
list = list->next;
|
||||
}
|
||||
if (list == NULL) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
chunk* gridRemove(Grid grid, int x, int z) {
|
||||
int hash = generate_hash(x, z);
|
||||
int index = hash % grid->capacity;
|
||||
List list = grid->data[index];
|
||||
List prev = NULL;
|
||||
while (list != NULL && list->hash != hash) {
|
||||
prev = list;
|
||||
list = list->next;
|
||||
}
|
||||
if (list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
chunk* elt = list->elt;
|
||||
if (prev == NULL) {
|
||||
grid->data[index] = list->next;
|
||||
} else {
|
||||
prev->next = list->next;
|
||||
}
|
||||
free(list);
|
||||
grid->size--;
|
||||
return elt;
|
||||
}
|
||||
|
||||
Grid newGrid() {
|
||||
Grid grid = malloc(sizeof(struct Grid));
|
||||
grid->data = malloc(sizeof(List) * 800);
|
||||
grid->size = 0;
|
||||
grid->capacity = 800;
|
||||
grid->loadFactor = 0.75;
|
||||
for (int i = 0; i < grid->capacity; i++) {
|
||||
grid->data[i] = NULL;
|
||||
}
|
||||
grid->push = &gridInsert;
|
||||
grid->pop = &gridRemove;
|
||||
grid->get = &gridGet;
|
||||
grid->mem = &gridMem;
|
||||
return grid;
|
||||
}
|
||||
|
||||
chunk** gridIter(Grid grid) {
|
||||
chunk** array = malloc(sizeof(chunk*) * grid->size);
|
||||
int index = 0;
|
||||
for (int i = 0; i < grid->capacity; i++) {
|
||||
List list = grid->data[i];
|
||||
while (list != NULL) {
|
||||
array[index++] = list->elt;
|
||||
list = list->next;
|
||||
}
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
ChunkCoord newChunkCoord(int x, int z) {
|
||||
ChunkCoord coord = malloc(sizeof(struct ChunkCoord));
|
||||
coord->x = x;
|
||||
coord->z = z;
|
||||
return coord;
|
||||
}
|
||||
|
||||
void freeChunkCoord(ChunkCoord coord) {
|
||||
free(coord);
|
||||
free(tbl);
|
||||
}
|
83
src/hash.h
83
src/hash.h
|
@ -1,75 +1,26 @@
|
|||
// credit to Benoit //
|
||||
|
||||
#ifndef GRID_H
|
||||
#define GRID_H
|
||||
// (int * int) -> room*
|
||||
|
||||
#ifndef HASH_H
|
||||
#define HASH_H
|
||||
#include "structure.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct linkedList {
|
||||
uint8_t coord ; // coord%8 = x ; coord/16 = y
|
||||
char* flag ;
|
||||
struct linkedList* next ;
|
||||
} linkedList ;
|
||||
|
||||
typedef struct template {
|
||||
uint8_t id ;
|
||||
uint8_t* lines ; // len = 8
|
||||
|
||||
bool checkCompat ;
|
||||
|
||||
uint8_t eastsig ;
|
||||
uint8_t westsig ;
|
||||
|
||||
linkedList* meta ;
|
||||
} template ;
|
||||
|
||||
typedef struct chunk {
|
||||
int16_t chx ;
|
||||
int16_t chy ;
|
||||
|
||||
int draw_id ;
|
||||
|
||||
template chdata ;
|
||||
} chunk ;
|
||||
|
||||
//
|
||||
|
||||
typedef struct List {
|
||||
int hash;
|
||||
int x;
|
||||
int y;
|
||||
chunk* elt;
|
||||
struct List* next;
|
||||
} *List;
|
||||
|
||||
typedef struct Grid {
|
||||
List* data;
|
||||
int capacity;
|
||||
int size;
|
||||
double loadFactor;
|
||||
void (*push)(struct Grid *grid, int x, int z, chunk* elt);
|
||||
chunk* (*pop)(struct Grid *grid, int x, int z);
|
||||
chunk* (*get)(struct Grid *grid, int x, int z);
|
||||
bool (*mem)(struct Grid *grid, int x, int z);
|
||||
} *Grid;
|
||||
bool linkedList_mem(linkedList lst, int chx, int chy);
|
||||
room linkedList_find(linkedList lst, int chx, int chy);
|
||||
room* linkedList_find_opt(linkedList lst, int chx, int chy);
|
||||
void linkedList_add(linkedList lst, int chx, int chy, room* area);
|
||||
void linkedList_free(linkedList lst);
|
||||
|
||||
int cantor(int x, int y);
|
||||
int generate_hash(int x, int y);
|
||||
|
||||
Grid newGrid();
|
||||
void freeGrid(Grid grid);
|
||||
|
||||
void gridInsert(Grid grid, int x, int z, chunk* elt);
|
||||
chunk* gridGet(Grid grid, int x, int z);
|
||||
bool gridMem(Grid grid, int x, int z);
|
||||
chunk* gridRemove(Grid grid, int x, int z);
|
||||
|
||||
chunk** gridIter(Grid grid);
|
||||
|
||||
typedef struct ChunkCoord {
|
||||
int x;
|
||||
int z;
|
||||
} *ChunkCoord;
|
||||
|
||||
ChunkCoord newChunkCoord(int x, int z);
|
||||
void freeChunkCoord(ChunkCoord coord);
|
||||
hashtbl hashtbl_generate(int initial_size);
|
||||
bool hashtbl_mem(hashtbl tbl, int chx, int chy);
|
||||
room hashtbl_find(hashtbl tbl, int chx, int chy);
|
||||
room* hashtbl_find_opt(hashtbl tbl, int chx, int chy);
|
||||
void hashtbl_add(hashtbl tbl, int chx, int chy, room* area);
|
||||
void hashtbl_free(hashtbl tbl);
|
||||
|
||||
#endif
|
|
@ -34,6 +34,7 @@ int main(int argc, char** argv) {
|
|||
Uint32 render_flags = SDL_RENDERER_ACCELERATED;
|
||||
SDL_Renderer* rend = SDL_CreateRenderer(win, -1, render_flags);
|
||||
SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
|
||||
|
||||
//-------------------------------------------------------------------------------//
|
||||
|
||||
if(SDL_Init(SDL_INIT_AUDIO)) {
|
||||
|
|
10
src/move.c
10
src/move.c
|
@ -67,16 +67,18 @@ void playerActions() {
|
|||
has_changed = true ;
|
||||
switch (event.key.keysym.sym) {
|
||||
case SDLK_z:
|
||||
camz += speed*cos(rot_hz);
|
||||
camx -= speed*sin(rot_hz);
|
||||
camz += speed*cos(rot_hz)*cos(rot_vt);
|
||||
camx -= speed*sin(rot_hz)*cos(rot_vt);
|
||||
camy += speed*sin(rot_vt);
|
||||
break;
|
||||
case SDLK_q:
|
||||
camx -= speed*cos(rot_hz);
|
||||
camz -= speed*sin(rot_hz);
|
||||
break;
|
||||
case SDLK_s:
|
||||
camz -= speed*cos(rot_hz);
|
||||
camx += speed*sin(rot_hz);
|
||||
camz -= speed*cos(rot_hz)*cos(rot_vt);
|
||||
camx += speed*sin(rot_hz)*cos(rot_vt);
|
||||
camy -= speed*sin(rot_vt);
|
||||
break;
|
||||
case SDLK_d:
|
||||
camx += speed*cos(rot_hz);
|
||||
|
|
|
@ -20,6 +20,43 @@ struct cube_0 {
|
|||
typedef struct cube_0 cube_0 ;
|
||||
typedef cube_0* cube ;
|
||||
|
||||
typedef struct teleporter {
|
||||
cube_0 hitbox ;
|
||||
int dest_chx ;
|
||||
int dest_chy ;
|
||||
double dest_x ;
|
||||
double dest_y ;
|
||||
} teleporter ;
|
||||
|
||||
struct room {
|
||||
// (0, 0, 0) = bottom, left and down
|
||||
int chunk_x ;
|
||||
int chunk_y ;
|
||||
cube* map ;
|
||||
} ;
|
||||
typedef struct room room ;
|
||||
|
||||
struct cell {
|
||||
int chx ;
|
||||
int chy ;
|
||||
room* area ;
|
||||
struct cell* next ;
|
||||
} ;
|
||||
|
||||
typedef struct cell cell ;
|
||||
typedef struct cell* linkedList ;
|
||||
|
||||
struct hashtbl_0 {
|
||||
int tabLength ;
|
||||
int insertedElts ;
|
||||
int maxInserted ;
|
||||
linkedList* tab ;
|
||||
} ;
|
||||
typedef struct hashtbl_0 hashtbl_0 ;
|
||||
typedef hashtbl_0* hashtbl ;
|
||||
|
||||
// ------------------------------------------------ //
|
||||
|
||||
extern imgs digits ;
|
||||
extern imgs letters ;
|
||||
|
||||
|
@ -34,4 +71,6 @@ extern double tan_fov ;
|
|||
|
||||
extern bool has_changed ;
|
||||
|
||||
extern hashtbl visited ;
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue