changed type in room + fixed some segFaults

This commit is contained in:
Alexandre 2025-02-06 14:36:33 +01:00
parent dec1124c43
commit 381348a420
19 changed files with 256 additions and 190 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -357,21 +357,25 @@ void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, do
void remove_entity(entity** arr, int* memlen, int* len, int index) { void remove_entity(entity** arr, int* memlen, int* len, int index) {
if(*len > 0) { if(*len > 0) {
(*arr)[index] = (*arr)[*len -1]; entity* temp = arr[index];
arr[index] = arr[*len -1];
arr[*len -1] = temp;
*len -= 1; *len -= 1;
} }
} }
void add_entity(entity** arr, int* memlen, int* len, entity ent) { void add_entity(entity** arr, int* memlen, int* len, entity* ent) {
if(*memlen == *len) { if(*memlen == *len) {
entity* newarr = malloc(sizeof(entity)*2*(*memlen)); entity** newarr = malloc(sizeof(entity*)*2*(*memlen));
for(int k = 0; k < *len; k++) { for(int k = 0; k < *len; k++) {
newarr[k] = (*arr)[k] ; newarr[k] = malloc(sizeof(entity));
newarr[k] = arr[k] ;
free(arr[k]);
} }
free(*arr); free(*arr);
*arr = newarr ; arr = newarr ;
*memlen *= 2; *memlen *= 2;
} }
(*arr)[*len] = ent ; arr[*len] = ent ;
*len += 1; *len += 1;
} }

View File

@ -56,7 +56,7 @@ double distance_pt_cube_axis_max(double coord, double begin, double end);
double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd); double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
void remove_entity(entity** arr, int* memlen, int* len, int index); void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent); void add_entity(entity** arr, int* memlen, int* len, entity* ent);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz); void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c); void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c);

View File

@ -27,6 +27,13 @@ int MAX_SIZE = 8192 ;
int* drawOrder ; int* drawOrder ;
GLint loc_scale;
GLint loc_model;
GLint loc_view;
GLint loc_proj;
GLint loc_frag;
void init_draworder() { void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ; drawOrder = malloc(sizeof(int)*6) ;
} }
@ -128,23 +135,24 @@ void gl_renderCube(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f}); glm_rotate(model, (float)(-c->hz_angle), (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f}); glm_rotate(model, (float)(c->vt_angle), (vec3){1.0f, 0.0f, 0.0f});
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "scale"), 1, GL_FALSE, (float*)scale); glUniformMatrix4fv(loc_scale, 1, GL_FALSE, (float*)scale);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (float*)model); glUniformMatrix4fv(loc_model, 1, GL_FALSE, (float*)model);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, (float*)view); glUniformMatrix4fv(loc_view, 1, GL_FALSE, (float*)view);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (float*)projection); glUniformMatrix4fv(loc_proj, 1, GL_FALSE, (float*)projection);
glUniform4f(glGetUniformLocation(fragmentShader, "u_color"), c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f); glUniform4f(loc_frag, c->red/255.0f, c->green/255.0f, c->blue/255.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
triCount += 12; triCount += 12;
} }
double px, py, pz, pz2; double px, py, pz;
double px2, py2, pz2;
bool is_visible(cube_0* cb, double offx, double offy, double offz) { bool is_visible(cube_0* cb, double offx, double offy, double offz) {
for(int d = 0; d < 8; d++) { for(int d = 0; d < 8; d++) {
project_to_cube(cb->x+cb->w*(d%2==0)+offx, cb->y+cb->h*((d/2)%2==0)+offy, cb->z+cb->d*((d/4)%2==0)+offz, &px, &py, &pz, cb); project_to_cube(cb->x+cb->w*(d%2==0)+offx, cb->y+cb->h*((d/2)%2==0)+offy, cb->z+cb->d*((d/4)%2==0)+offz, &px, &py, &pz, cb);
project_to_camera(px, py, pz, NULL, NULL, &pz2); project_to_camera(px, py, pz, &px2, &py2, &pz2);
if(pz2 > near) { if(pz2 >= near) {
return true; return true;
} }
} }
@ -164,8 +172,8 @@ void gl_renderAll(unsigned int shaderProgram, unsigned int fragmentShader, unsig
} }
} }
for(int k = 0; k < rtd->ent_len; k++) { for(int k = 0; k < rtd->ent_len; k++) {
if(is_visible(rtd->ents[k].pos, offx, offy, offz)) { if(is_visible(rtd->ents[k]->pos, offx, offy, offz)) {
gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k].pos, offx, offy, offz); gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, rtd->ents[k]->pos, offx, offy, offz);
} }
} }
} }
@ -193,6 +201,13 @@ void gl_initRender(unsigned int shaderProgram, unsigned int fragmentShader, unsi
glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view); glm_lookat((vec3){(float)camx, (float)camy, (float)camz}, direction, (vec3){0.0f, 1.0f, 0.0f}, view);
glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection); glm_perspective(glm_rad((float)fov), 1500.0f / 1000.0f, 0.1f, 100.0f, projection);
loc_scale = glGetUniformLocation(shaderProgram, "scale");
loc_model = glGetUniformLocation(shaderProgram, "model");
loc_view = glGetUniformLocation(shaderProgram, "view");
loc_proj = glGetUniformLocation(shaderProgram, "projection");
loc_frag = glGetUniformLocation(fragmentShader, "u_color");
} }
void gl_drawData(unsigned int shaderProg) { void gl_drawData(unsigned int shaderProg) {

View File

@ -21,22 +21,22 @@ bool is_colliding_with_map(cube_0* cb) {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
for(int d = 0; d < 8; d++) { for(int d = 0; d < 8; d++) {
if(distance_pt_cube_0_3d(cb->x+cb->w*(d%2==0), cb->y+cb->h*((d/2)%2==0), cb->z+cb->d*((d/4)%2==0), current_room->map[k]) <= 0.01) { if(distance_pt_cube_0_3d(cb->x+cb->w*(d%2==0), cb->y+cb->h*((d/2)%2==0), cb->z+cb->d*((d/4)%2==0), current_room->map[k]) <= 0.01) {
return true ; return true;
} }
} }
} }
return false ; return false;
} }
bool is_colliding_with_tp(cube_0* cb) { bool is_colliding_with_tp(cube_0* cb) {
for(int k = 0; k < current_room->tps_size; k++) { for(int k = 0; k < current_room->tps_size; k++) {
for(int d = 0; d < 8; d++) { for(int d = 0; d < 8; d++) {
if(distance_pt_cube_0_3d(cb->x+cb->w*(d%2==0), cb->y+cb->h*((d/2)%2==0), cb->z+cb->d*((d/4)%2==0), current_room->tps[k]->hitbox) <= 0.01) { if(distance_pt_cube_0_3d(cb->x+cb->w*(d%2==0), cb->y+cb->h*((d/2)%2==0), cb->z+cb->d*((d/4)%2==0), current_room->tps[k]->hitbox) <= 0.01) {
return true ; return true;
} }
} }
} }
return false ; return false;
} }
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
@ -47,9 +47,9 @@ void update_entity(entity* ent, float dtime) {
void update_entities(float dtime) { void update_entities(float dtime) {
for(int k = 0; k < current_room->ent_len; k++) { for(int k = 0; k < current_room->ent_len; k++) {
if(current_room->ents[k].updatePos != NULL) { if(current_room->ents[k]->updatePos != NULL) {
//printf("e\n"); //printf("e\n");
update_entity(&(current_room->ents[k]), dtime); update_entity(current_room->ents[k], dtime);
} }
} }
} }
@ -66,7 +66,7 @@ void speen2(double x, double y, double z, double w, double h, double d, double h
double dx = (x+w/2 - camx); double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy); double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz); double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz) ; double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 170.0*dx/total; dx = 170.0*dx/total;
dy = 170.0*dy/total; dy = 170.0*dy/total;
dz = 170.0*dz/total; dz = 170.0*dz/total;
@ -78,6 +78,10 @@ void speen3(double x, double y, double z, double w, double h, double d, double h
ret->vt_angle += ((double)dtime)*22.5; ret->vt_angle += ((double)dtime)*22.5;
} }
void moving_x(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) {
}
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
if(ret->red == 193) { if(ret->red == 193) {
ret->red = 0; ret->red = 0;
@ -86,9 +90,9 @@ void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
coins += *hp; coins += *hp;
player_hp -= (*dmg); player_hp -= (*dmg);
if(*dmg != 0) { if(*dmg != 0) {
fade_dmg = 255 ; fade_dmg = 255;
} }
*hp = 0 ; *hp = 0;
} }
} }
@ -96,21 +100,21 @@ void go_to_player(double x, double y, double z, double w, double h, double d, do
double dx = (x+w/2 - camx); double dx = (x+w/2 - camx);
double dy = (y+h/2 - camy); double dy = (y+h/2 - camy);
double dz = (z+d/2 - camz); double dz = (z+d/2 - camz);
double total = sqrt(dx*dx + dy*dy + dz*dz) ; double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = 110.0*dx/total; dx = 110.0*dx/total;
dy = 110.0*dy/total; dy = 110.0*dy/total;
dz = 110.0*dz/total; dz = 110.0*dz/total;
ret->x -= dtime*dx ; ret->x -= dtime*dx;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) { if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) {
ret->x += dtime*dx ; ret->x += dtime*dx;
} }
ret->y -= dtime*dy ; ret->y -= dtime*dy;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) { if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) {
ret->y += dtime*dy ; ret->y += dtime*dy;
} }
ret->z -= dtime*dz ; ret->z -= dtime*dz;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) { if(is_colliding_with_map(ret) || is_colliding_with_tp(ret)) {
ret->z += dtime*dz ; ret->z += dtime*dz;
} }
if((int)(ret->x+ret->y+ret->z) != (int)(ret->x+ret->y+ret->z-dx-dy-dz)) { if((int)(ret->x+ret->y+ret->z) != (int)(ret->x+ret->y+ret->z-dx-dy-dz)) {
@ -120,7 +124,7 @@ void go_to_player(double x, double y, double z, double w, double h, double d, do
void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret) { void explodeOnHit(float dtime, int* hp, int* dmg, cube_0* ret) {
player_hp -= (*dmg); player_hp -= (*dmg);
if(*dmg != 0) { if(*dmg != 0) {
fade_dmg = 255 ; fade_dmg = 255;
} }
*hp = 0 ; *hp = 0;
} }

View File

@ -14,51 +14,51 @@
#include "entities.h" #include "entities.h"
#include "generation.h" #include "generation.h"
hashtbl visited ; hashtbl visited;
room* current_room ; room* current_room;
int player_chx ; int player_chx;
int player_chy ; int player_chy;
entry* pool ; entry* pool;
int pool_size ; int pool_size;
int total_weight ; int total_weight;
int coins ; int coins;
int fct_entry_size ; int fct_entry_size;
fct_entry* hashtbl_entities ; fct_entry* hashtbl_entities;
void init_ent_generator(int n) { void init_ent_generator(int n) {
hashtbl_entities = malloc(sizeof(fct_entry)*n); hashtbl_entities = malloc(sizeof(fct_entry)*n);
fct_entry_size = n; fct_entry_size = n;
for(int k = 0; k < 10; k++) { for(int k = 0; k < 10; k++) {
hashtbl_entities[k].id = (-1) ; hashtbl_entities[k].id = (-1);
} }
hashtbl_entities[0].id = 0; hashtbl_entities[0].id = 0;
hashtbl_entities[0].name = "Coin"; // 0 = default hashtbl_entities[0].name = "Coin"; // 0 = default
hashtbl_entities[0].updatePos = &speen2 ; hashtbl_entities[0].updatePos = &speen2;
hashtbl_entities[0].onHit = &detectHit ; hashtbl_entities[0].onHit = &detectHit;
hashtbl_entities[0].onDeath = NULL ; hashtbl_entities[0].onDeath = NULL;
hashtbl_entities[1].id = 1; hashtbl_entities[1].id = 1;
hashtbl_entities[1].name = "ExplosiveStill"; hashtbl_entities[1].name = "ExplosiveStill";
hashtbl_entities[1].updatePos = &speen2 ; hashtbl_entities[1].updatePos = &speen2;
hashtbl_entities[1].onHit = &explodeOnHit ; hashtbl_entities[1].onHit = &explodeOnHit;
hashtbl_entities[1].onDeath = NULL ; hashtbl_entities[1].onDeath = NULL;
hashtbl_entities[2].id = 2; hashtbl_entities[2].id = 2;
hashtbl_entities[2].name = "ExplosiveSeek"; hashtbl_entities[2].name = "ExplosiveSeek";
hashtbl_entities[2].updatePos = &go_to_player ; hashtbl_entities[2].updatePos = &go_to_player;
hashtbl_entities[2].onHit = &explodeOnHit ; hashtbl_entities[2].onHit = &explodeOnHit;
hashtbl_entities[2].onDeath = NULL ; hashtbl_entities[2].onDeath = NULL;
hashtbl_entities[3].id = 3; hashtbl_entities[3].id = 3;
hashtbl_entities[3].name = "ExplosiveShoot"; hashtbl_entities[3].name = "ExplosiveShoot";
hashtbl_entities[3].updatePos = &speen3 ; hashtbl_entities[3].updatePos = &speen3;
hashtbl_entities[3].onHit = &explodeOnHit ; hashtbl_entities[3].onHit = &explodeOnHit;
hashtbl_entities[3].onDeath = NULL ; hashtbl_entities[3].onDeath = NULL;
} }
fct_entry* get_entry(int k0) { fct_entry* get_entry(int k0) {
@ -72,8 +72,8 @@ fct_entry* get_entry(int k0) {
void copy_room(room* src, room* dest, int chx, int chy) { void copy_room(room* src, room* dest, int chx, int chy) {
// considering dest has already been malloc'd // considering dest has already been malloc'd
dest->chunk_x = chx ; dest->chunk_x = chx;
dest->chunk_y = chy ; dest->chunk_y = chy;
dest->map = malloc(sizeof(cube_0*)*src->map_size); dest->map = malloc(sizeof(cube_0*)*src->map_size);
for(int k = 0; k < src->map_size; k++) { for(int k = 0; k < src->map_size; k++) {
dest->map[k] = create_cube_0( dest->map[k] = create_cube_0(
@ -82,7 +82,7 @@ void copy_room(room* src, room* dest, int chx, int chy) {
src->map[k]->hz_angle, src->map[k]->vt_angle, src->map[k]->red, src->map[k]->green, src->map[k]->blue src->map[k]->hz_angle, src->map[k]->vt_angle, src->map[k]->red, src->map[k]->green, src->map[k]->blue
); );
} }
dest->map_size = src->map_size ; dest->map_size = src->map_size;
dest->tps = malloc(sizeof(teleporter*)*src->tps_size); dest->tps = malloc(sizeof(teleporter*)*src->tps_size);
for(int k = 0; k < src->tps_size; k++) { for(int k = 0; k < src->tps_size; k++) {
dest->tps[k] = malloc(sizeof(teleporter)); dest->tps[k] = malloc(sizeof(teleporter));
@ -93,37 +93,51 @@ void copy_room(room* src, room* dest, int chx, int chy) {
); );
dest->tps[k]->dest_chx = src->tps[k]->dest_chx + chx; dest->tps[k]->dest_chx = src->tps[k]->dest_chx + chx;
dest->tps[k]->dest_chy = src->tps[k]->dest_chy + chy; dest->tps[k]->dest_chy = src->tps[k]->dest_chy + chy;
dest->tps[k]->dest_x = src->tps[k]->dest_x ; dest->tps[k]->dest_x = src->tps[k]->dest_x;
dest->tps[k]->dest_y = src->tps[k]->dest_y ; dest->tps[k]->dest_y = src->tps[k]->dest_y;
dest->tps[k]->dest_z = src->tps[k]->dest_z ; dest->tps[k]->dest_z = src->tps[k]->dest_z;
} }
dest->ents = malloc(sizeof(entity)*src->ent_memlen); dest->ents = malloc(sizeof(entity*)*src->ent_memlen);
dest->ent_memlen = src->ent_memlen ; dest->ent_memlen = src->ent_memlen;
dest->ent_len = src->ent_len ; dest->ent_len = src->ent_len;
for(int k = 0; k < src->ent_len; k++) { for(int k = 0; k < src->ent_len; k++) {
dest->ents[k].damage = src->ents[k].damage ; dest->ents[k] = malloc(sizeof(entity));
dest->ents[k].hitpoints = malloc(sizeof(int)); dest->ents[k]->damage = src->ents[k]->damage;
*(dest->ents[k].hitpoints) = *(src->ents[k].hitpoints) ; dest->ents[k]->hitpoints = malloc(sizeof(int));
dest->ents[k].pos = create_cube_0( dest->ents[k]->meta1 = src->ents[k]->meta1;
(*(src->ents[k].pos)).x, (*(src->ents[k].pos)).y, (*(src->ents[k].pos)).z, dest->ents[k]->meta2 = src->ents[k]->meta2;
(*(src->ents[k].pos)).w, (*(src->ents[k].pos)).h, (*(src->ents[k].pos)).d, dest->ents[k]->meta3 = src->ents[k]->meta3;
(*(src->ents[k].pos)).hz_angle, (*(src->ents[k].pos)).vt_angle, (*(src->ents[k].pos)).red, (*(src->ents[k].pos)).green, (*(src->ents[k].pos)).blue dest->ents[k]->meta4 = src->ents[k]->meta4;
dest->ents[k]->meta5 = src->ents[k]->meta5;
dest->ents[k]->meta6 = src->ents[k]->meta6;
dest->ents[k]->meta7 = src->ents[k]->meta7;
dest->ents[k]->meta8 = src->ents[k]->meta8;
*(dest->ents[k]->hitpoints) = *(src->ents[k]->hitpoints);
dest->ents[k]->pos = create_cube_0(
(*(src->ents[k]->pos)).x, (*(src->ents[k]->pos)).y, (*(src->ents[k]->pos)).z,
(*(src->ents[k]->pos)).w, (*(src->ents[k]->pos)).h, (*(src->ents[k]->pos)).d,
(*(src->ents[k]->pos)).hz_angle, (*(src->ents[k]->pos)).vt_angle, (*(src->ents[k]->pos)).red, (*(src->ents[k]->pos)).green, (*(src->ents[k]->pos)).blue
); );
dest->ents[k].updatePos = src->ents[k].updatePos; dest->ents[k]->updatePos = src->ents[k]->updatePos;
dest->ents[k].onHit = src->ents[k].onHit; dest->ents[k]->onHit = src->ents[k]->onHit;
dest->ents[k].onDeath = src->ents[k].onDeath; dest->ents[k]->onDeath = src->ents[k]->onDeath;
}
for(int k = src->ent_len; k < src->ent_memlen; k++) {
dest->ents[k] = malloc(sizeof(entity));
dest->ents[k]->hitpoints = malloc(sizeof(int));
dest->ents[k]->pos = malloc(sizeof(cube_0));
} }
dest->tps_size = src->tps_size; dest->tps_size = src->tps_size;
} }
void build_starting_chunk(int chx, int chy) { void build_starting_chunk(int chx, int chy) {
room* new = malloc(sizeof(room)); room* new = malloc(sizeof(room));
new->chunk_x = chx ; new->chunk_x = chx;
new->chunk_y = chy ; new->chunk_y = chy;
new->map = malloc(sizeof(cube_0*)*9); new->map = malloc(sizeof(cube_0*)*9);
new->map_size = 9 ; new->map_size = 9;
new->tps = malloc(sizeof(teleporter*)*4); new->tps = malloc(sizeof(teleporter*)*4);
new->tps_size = 4 ; new->tps_size = 4;
new->map[0] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[0] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
new->map[1] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255); new->map[1] = create_cube_0(0.0, 0.0, 0.0, 5.0, 1.0, 5.0, 0.0, 0.0, 255, 255, 255);
@ -140,16 +154,20 @@ void build_starting_chunk(int chx, int chy) {
new->tps[2] = create_teleporter(5.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 255, 0 , chx+1, chy , 2.5, 2.5, 2.5); new->tps[2] = create_teleporter(5.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 255, 0 , chx+1, chy , 2.5, 2.5, 2.5);
new->tps[3] = create_teleporter(2.0, 1.0, 5.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 0, 255 , chx , chy+1, 2.5, 2.5, 2.5); new->tps[3] = create_teleporter(2.0, 1.0, 5.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 0.0, 0.0, 0, 0, 255 , chx , chy+1, 2.5, 2.5, 2.5);
new->ents = malloc(sizeof(entity)*128); new->ents = malloc(sizeof(entity*)*32);
new->ent_len = 1 ; for(int k = 0; k < 32; k++) {
new->ent_memlen = 128 ; new->ents[k] = malloc(sizeof(entity));
new->ents[0].pos = create_cube_0(-0.25, 8.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 128, 0); new->ents[k]->hitpoints = malloc(sizeof(int));
new->ents[0].damage = 0 ; new->ents[k]->pos = malloc(sizeof(cube_0));
new->ents[0].hitpoints = malloc(sizeof(int)); };
*(new->ents[0].hitpoints) = 5 ; new->ent_len = 1;
new->ents[0].onDeath = NULL ; new->ent_memlen = 32;
new->ents[0].onHit = *detectHit ; fill_cube_0(new->ents[0]->pos, -0.25, 8.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 128, 0);
new->ents[0].updatePos = *speen2 ; new->ents[0]->damage = 0;
*(new->ents[0]->hitpoints) = 5;
new->ents[0]->onDeath = NULL;
new->ents[0]->onHit = *detectHit;
new->ents[0]->updatePos = *speen2;
hashtbl_add(visited, chx, chy, new); hashtbl_add(visited, chx, chy, new);
} }
@ -158,32 +176,32 @@ void init_hashtbl() {
visited = hashtbl_generate(1789); visited = hashtbl_generate(1789);
build_starting_chunk(0, 0); build_starting_chunk(0, 0);
current_room = hashtbl_find_opt(visited, 0, 0); current_room = hashtbl_find_opt(visited, 0, 0);
player_chx = 0 ; player_chx = 0;
player_chy = 0 ; player_chy = 0;
total_weight = 0 ; total_weight = 0;
coins = 0 ; coins = 0;
} }
void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) { void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) {
*ret_cubes = 0 ; *ret_cubes = 0;
*ret_tps = 0 ; *ret_tps = 0;
*ret_ent = 0 ; *ret_ent = 0;
int in_blocks = 0 ; int in_blocks = 0;
char c = fgetc(ptr) ; char c = fgetc(ptr);
while(c != EOF && c != '$') { while(c != EOF && c != '$') {
//printf("%c", c); //printf("%c", c);
if(c == 'B') { if(c == 'B') {
in_blocks = 0 ; in_blocks = 0;
} else if(c == 'T') { } else if(c == 'T') {
in_blocks = 1 ; in_blocks = 1;
} else if(c == 'E') { } else if(c == 'E') {
in_blocks = 2 ; in_blocks = 2;
} else if(c == '[') { } else if(c == '[') {
if(in_blocks == 0) { if(in_blocks == 0) {
*ret_cubes += 1 ; *ret_cubes += 1;
} else if(in_blocks == 1) { } else if(in_blocks == 1) {
*ret_tps += 1 ; *ret_tps += 1;
} else { } else {
*ret_ent += 1; *ret_ent += 1;
} }
@ -203,24 +221,24 @@ void align_to(FILE* ptr, char ch) {
int read_int(FILE* ptr, bool print) { int read_int(FILE* ptr, bool print) {
bool is_reading = false; bool is_reading = false;
int buffer = 0 ; int buffer = 0;
int sign = 1 ; int sign = 1;
char c = fgetc(ptr); char c = fgetc(ptr);
while(c != EOF) { while(c != EOF) {
if(c == '-') { if(c == '-') {
sign = -1 ; sign = -1;
} else if((int)c >= 48 && (int)c <= 57) { } else if((int)c >= 48 && (int)c <= 57) {
is_reading = true ; is_reading = true;
buffer = 10*buffer + (int)c - 48 ; buffer = 10*buffer + (int)c - 48;
} else if(is_reading) { } else if(is_reading) {
/*if(print) { /*if(print) {
printf("%d ; ", buffer*sign); printf("%d; ", buffer*sign);
}*/ }*/
return buffer*sign; return buffer*sign;
} }
c = fgetc(ptr); c = fgetc(ptr);
} }
return buffer*sign ; return buffer*sign;
} }
double sign(double __x) { double sign(double __x) {
@ -233,7 +251,7 @@ double sign(double __x) {
double read_float(FILE* ptr) { double read_float(FILE* ptr) {
int ent = read_int(ptr, false); int ent = read_int(ptr, false);
int frac = read_int(ptr, false); int frac = read_int(ptr, false);
//printf("%d.%d ; ", ent, frac); //printf("%d.%d; ", ent, frac);
if(ent != 0.0) { if(ent != 0.0) {
return (ent/abs(ent))*(absf((double)ent) + ((double)frac)/(pow(10.0, (double)ln_baseN(frac, 10)))); return (ent/abs(ent))*(absf((double)ent) + ((double)frac)/(pow(10.0, (double)ln_baseN(frac, 10))));
} else { } else {
@ -247,18 +265,15 @@ void parse_one_room(int id, char* filename) {
FILE* ptr = fopen(filename, "r"); FILE* ptr = fopen(filename, "r");
pool[id].area = malloc(sizeof(room)); pool[id].area = malloc(sizeof(room));
int ncubes ; int ncubes;
int ntps ; int ntps;
int nent ; int nent;
FILE* ptr2 = fopen(filename, "r") ; FILE* ptr2 = fopen(filename, "r");
get_number_blocks(&ncubes, &ntps, &nent, ptr2); get_number_blocks(&ncubes, &ntps, &nent, ptr2);
printf("(%d, %d, %d)\n", ncubes, ntps, nent); printf("(%d, %d, %d)\n", ncubes, ntps, nent);
int nmemlen = 128 ; int nmemlen = maxd(nent, 64);
while(nmemlen < nent) {
nmemlen *= 2.;
}
pool[id].area->map = malloc(sizeof(cube_0*)*ncubes); pool[id].area->map = malloc(sizeof(cube_0*)*ncubes);
pool[id].area->map_size = ncubes; pool[id].area->map_size = ncubes;
@ -267,9 +282,14 @@ void parse_one_room(int id, char* filename) {
pool[id].area->tps[k] = malloc(sizeof(teleporter)); pool[id].area->tps[k] = malloc(sizeof(teleporter));
} }
pool[id].area->tps_size = ntps; pool[id].area->tps_size = ntps;
pool[id].area->ents = malloc(sizeof(entity)*nmemlen); pool[id].area->ents = malloc(sizeof(entity*)*nmemlen);
pool[id].area->ent_len = nent ; for(int k = 0; k < nmemlen; k++) {
pool[id].area->ent_memlen = nmemlen ; pool[id].area->ents[k] = malloc(sizeof(entity));
pool[id].area->ents[k]->hitpoints = malloc(sizeof(int));
pool[id].area->ents[k]->pos = malloc(sizeof(cube_0));
}
pool[id].area->ent_len = nent;
pool[id].area->ent_memlen = nmemlen;
printf("0/3...\n"); printf("0/3...\n");
fflush(stdout); fflush(stdout);
@ -336,17 +356,23 @@ void parse_one_room(int id, char* filename) {
if(entry == NULL) { if(entry == NULL) {
entry = get_entry(0); entry = get_entry(0);
} }
pool[id].area->ents[k].pos = create_cube_0(cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue); fill_cube_0(pool[id].area->ents[k]->pos, cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue);
pool[id].area->ents[k].damage = dmg ; pool[id].area->ents[k]->damage = dmg;
pool[id].area->ents[k].hitpoints = malloc(sizeof(int)); *(pool[id].area->ents[k]->hitpoints) = hp;
*(pool[id].area->ents[k].hitpoints) = hp ; pool[id].area->ents[k]->updatePos = entry->updatePos;
pool[id].area->ents[k]->onHit = entry->onHit ;
pool[id].area->ents[k].updatePos = entry->updatePos ; pool[id].area->ents[k]->onDeath = entry->onDeath ;
pool[id].area->ents[k].onHit = entry->onHit ; pool[id].area->ents[k]->meta1 = 0;
pool[id].area->ents[k].onDeath = entry->onDeath ; pool[id].area->ents[k]->meta2 = 0;
//pool[id].area->ents[k].updatePos = &speen2 ; pool[id].area->ents[k]->meta3 = 0;
//pool[id].area->ents[k].onHit = &detectHit ; pool[id].area->ents[k]->meta4 = 0;
//pool[id].area->ents[k].onDeath = NULL ; pool[id].area->ents[k]->meta5 = 0.0;
pool[id].area->ents[k]->meta6 = 0.0;
pool[id].area->ents[k]->meta7 = 0.0;
pool[id].area->ents[k]->meta8 = 0.0;
//pool[id].area->ents[k]->updatePos = &speen2;
//pool[id].area->ents[k]->onHit = &detectHit ;
//pool[id].area->ents[k]->onDeath = NULL ;
//printf("\n"); //printf("\n");
} }
@ -375,44 +401,44 @@ void parse_one_room(int id, char* filename) {
printf("\n\n"); printf("\n\n");
pool[id].weight = read_int(ptr, true); pool[id].weight = read_int(ptr, true);
total_weight += pool[id].weight ; total_weight += pool[id].weight;
fclose(ptr); fclose(ptr);
} }
void parse_rooms(int n_rooms) { void parse_rooms(int n_rooms) {
char* name = malloc(sizeof(char)*19); // 1000 rooms max char* name = malloc(sizeof(char)*19); // 1000 rooms max
name[0] = 't' ; name[0] = 't';
name[1] = 'e' ; name[1] = 'e';
name[2] = 'm' ; name[2] = 'm';
name[3] = 'p' ; name[3] = 'p';
name[4] = 'l' ; name[4] = 'l';
name[5] = 'a' ; name[5] = 'a';
name[6] = 't' ; name[6] = 't';
name[7] = 'e' ; name[7] = 'e';
name[8] = 's' ; name[8] = 's';
name[9] = '/' ; name[9] = '/';
name[10] = 'r' ; name[10] = 'r';
name[11] = 'o' ; name[11] = 'o';
name[12] = 'o' ; name[12] = 'o';
name[13] = 'm' ; name[13] = 'm';
name[14] = '_' ; name[14] = '_';
name[15] = '0' ; name[15] = '0';
name[16] = '\0' ; name[16] = '\0';
name[17] = '\0' ; name[17] = '\0';
name[18] = '\0' ; name[18] = '\0';
pool = malloc(sizeof(entry)*n_rooms); pool = malloc(sizeof(entry)*n_rooms);
pool_size = n_rooms ; pool_size = n_rooms;
printf("Parsing...\n"); printf("Parsing...\n");
for(int k = 0; k < n_rooms; k++) { for(int k = 0; k < n_rooms; k++) {
if(k < 10) { if(k < 10) {
name[15] = (char)(k%10 + 48) ; name[15] = (char)(k%10 + 48);
} else if(k < 100) { } else if(k < 100) {
name[15] = (char)((k/10)%10 + 48) ; name[15] = (char)((k/10)%10 + 48);
name[16] = (char)(k%10 + 48) ; name[16] = (char)(k%10 + 48);
} }
parse_one_room(k, name); parse_one_room(k, name);
} }
@ -423,14 +449,14 @@ void parse_rooms(int n_rooms) {
free(name); free(name);
} }
int divider = 8; // has to be a multiple of both room_width and room_depth
int divider = 4;
void generate_terrain(room* r) { void generate_terrain(room* r) {
int rsize = 4*(room_width/divider)*(room_width/divider)*(room_depth/divider)*(room_depth/divider); // floor size (with 1x1 cubes) int rsize = 4*(room_width/divider)*(room_width/divider)*(room_depth/divider)*(room_depth/divider); // floor size (with 1x1 cubes)
cube** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size)); cube_0** newMap = malloc(sizeof(cube_0*)*(rsize+r->map_size));
for(int k = 0; k < rsize+r->map_size; k++) { for(int k = 0; k < rsize+r->map_size; k++) {
newMap[k] = malloc(sizeof(cube_0)); newMap[k] = malloc(sizeof(cube_0));
} }
for(int k = 0; k < r->map_size; k++) { for(int k = 0; k < r->map_size; k++) {
@ -443,7 +469,7 @@ void generate_terrain(room* r) {
int i = r->map_size; int i = r->map_size;
for(int l = -room_width; l < room_width; l+=divider) { for(int l = -room_width; l < room_width; l+=divider) {
for(int d = -room_depth; d < room_depth; d+=divider) { for(int d = -room_depth; d < room_depth; d+=divider) {
newMap[i] = create_cube_0((double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32); fill_cube_0(newMap[i], (double)l, -20.0, (double)d, (double)divider, 1.0, (double)divider, 0.0, 0.0,32, 128, 32);
i+=1; i+=1;
} }
} }
@ -458,19 +484,20 @@ void generate_nearby_chunks(int render_dist) {
if(!hashtbl_mem(visited, player_chx + w, player_chy + h)) { if(!hashtbl_mem(visited, player_chx + w, player_chy + h)) {
printf("generating (%d, %d)... ", player_chx + w, player_chy + h); printf("generating (%d, %d)... ", player_chx + w, player_chy + h);
//build_starting_chunk(player_chx + w, player_chy + h); //build_starting_chunk(player_chx + w, player_chy + h);
int pick = rand()%total_weight ; int pick = rand()%total_weight;
int sum = 0 ; int sum = 0;
for(int k = 0; k < pool_size; k++) { for(int k = 0; k < pool_size; k++) {
sum += pool[k].weight ; sum += pool[k].weight;
if(pick < sum) { if(pick < sum) {
printf("chose %d\n", k); printf("chose %d\n", k);
room* new = malloc(sizeof(room)); room* new = malloc(sizeof(room));
copy_room(pool[k].area, new, player_chx + w, player_chy + h); copy_room(pool[k].area, new, player_chx + w, player_chy + h);
generate_terrain(new); //generate_terrain(new);
hashtbl_add(visited, player_chx + w, player_chy + h, new); hashtbl_add(visited, player_chx + w, player_chy + h, new);
k = pool_size+1; k = pool_size+1;
} }
} }
printf("Done\n");
} }
} }
} }
@ -485,9 +512,10 @@ void free_pool() {
free(pool[k0].area->tps[k]->hitbox); free(pool[k0].area->tps[k]->hitbox);
free(pool[k0].area->tps[k]); free(pool[k0].area->tps[k]);
} }
for(int k = 0; k < pool[k0].area->ent_len; k++) { for(int k = 0; k < pool[k0].area->ent_memlen; k++) {
free(pool[k0].area->ents[k].hitpoints); free(pool[k0].area->ents[k]->hitpoints);
free(pool[k0].area->ents[k].pos); free(pool[k0].area->ents[k]->pos);
free(pool[k0].area->ents[k]);
} }
free(pool[k0].area->ents); free(pool[k0].area->ents);
free(pool[k0].area->tps); free(pool[k0].area->tps);

View File

@ -65,9 +65,10 @@ void free_all_cubes(room* r) {
free(r->tps[k]->hitbox); free(r->tps[k]->hitbox);
free(r->tps[k]); free(r->tps[k]);
} }
for(int k = 0; k < r->ent_len; k++) { for(int k = 0; k < r->ent_memlen; k++) {
free(r->ents[k].hitpoints); free(r->ents[k]->hitpoints);
free(r->ents[k].pos); free(r->ents[k]->pos);
free(r->ents[k]);
} }
free(r->ents); free(r->ents);
free(r->tps); free(r->tps);

View File

@ -88,10 +88,10 @@ void processInput(GLFWwindow *window, float dtime) {
} }
if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
rot_vt -= sensitivity; rot_vt = maxd(-3.14159/2.0, rot_vt-sensitivity);
} }
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) { if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
rot_vt += sensitivity; rot_vt = mind(3.14159/2.0, rot_vt+sensitivity);
} }
} }
@ -290,10 +290,12 @@ int main_alt() {
int fps = 60 ; int fps = 60 ;
int interval = 1000000/fps ; int interval = 1000000/fps ;
float delta;
clock_t finish = clock(); clock_t finish = clock();
clock_t origin = clock(); clock_t origin = clock();
while(!glfwWindowShouldClose(window) && player_hp >= 0) { while(!glfwWindowShouldClose(window) && player_hp > 0) {
// input // input
// ----- // -----
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@ -301,7 +303,9 @@ int main_alt() {
triCount = 0; triCount = 0;
origin = clock(); origin = clock();
fflush(stdout);
generate_nearby_chunks(1); generate_nearby_chunks(1);
fflush(stdout);
// draw the map // draw the map
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
@ -316,20 +320,21 @@ int main_alt() {
glBindVertexArray(RVAO); glBindVertexArray(RVAO);
finish = clock(); finish = clock();
gl_drawInteger(shaderProgramR, (int)(1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1); delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
gl_drawInteger(shaderProgramR, (int)(1.0f/(delta)), 0.9f, -0.85f, 0.05, 32, 255, 32, 0.005, -1);
gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1); gl_drawInteger(shaderProgramR, triCount, 0.0f, 0.92f, 0.04f, 128, 128, 128, 0.005f, 1);
gl_drawData(shaderProgramR); gl_drawData(shaderProgramR);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
processInput(window, ((float)finish - (float)origin)/CLOCKS_PER_SEC); processInput(window, delta);
teleport_on_edge(); teleport_on_edge();
update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC); update_entities(delta);
updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC); updateProj(delta);
//printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)); //printf("%f\n", 1.0f/(delta));
//printf("%lf, %lf, %lf\n", camx, camy, camz); //printf("%lf, %lf, %lf\n", camx, camy, camz);
usleep(interval); usleep(max(0, interval-(int)(1000000*delta)));
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View File

@ -92,15 +92,15 @@ bool is_colliding(float dtime) {
} }
} }
for(int k = 0; k < current_room->ent_len; k++) { for(int k = 0; k < current_room->ent_len; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k].pos); double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->ents[k]->pos);
if(dist <= min_dist) { if(dist <= min_dist) {
if(current_room->ents[k].onHit != NULL) { if(current_room->ents[k]->onHit != NULL) {
(*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, &current_room->ents[k].damage, &(*(current_room->ents[k].pos))); (*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, &current_room->ents[k]->damage, &(*(current_room->ents[k]->pos)));
if(*(current_room->ents[k].hitpoints) <= 0) { if(*(current_room->ents[k]->hitpoints) <= 0) {
if(current_room->ents[k].onDeath != NULL) { if(current_room->ents[k]->onDeath != NULL) {
(*current_room->ents[k].onDeath)(dtime); (*current_room->ents[k]->onDeath)(dtime);
} }
remove_entity(&current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k); remove_entity(current_room->ents, &current_room->ent_memlen, &current_room->ent_len, k);
} }
} }
return true; return true;

View File

@ -41,6 +41,15 @@ typedef struct entity {
// triggers when <hitpoints> goes negative (death) // triggers when <hitpoints> goes negative (death)
void (*onDeath)(float dtime); void (*onDeath)(float dtime);
int meta1;
int meta2;
int meta3;
int meta4;
double meta5;
double meta6;
double meta7;
double meta8;
int damage; int damage;
int* hitpoints; int* hitpoints;
} entity; } entity;
@ -53,7 +62,7 @@ struct room {
int map_size; int map_size;
teleporter** tps; teleporter** tps;
int tps_size; int tps_size;
entity* ents; entity** ents;
int ent_len; int ent_len;
int ent_memlen; int ent_memlen;
}; };