diff --git a/bin/back b/bin/back index 3e934ac..d0c1e15 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index f4d3d3a..21fe395 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index 0cb879c..70fb7c7 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/entities.o b/obj/entities.o index 9661349..1d2bff8 100644 Binary files a/obj/entities.o and b/obj/entities.o differ diff --git a/obj/generation.o b/obj/generation.o index 5c1796f..59cb67d 100644 Binary files a/obj/generation.o and b/obj/generation.o differ diff --git a/obj/hash.o b/obj/hash.o index bc81cee..572ab34 100644 Binary files a/obj/hash.o and b/obj/hash.o differ diff --git a/obj/main.o b/obj/main.o index 0f3b225..48df135 100644 Binary files a/obj/main.o and b/obj/main.o differ diff --git a/obj/menus.o b/obj/menus.o index dd56cc4..24d442a 100644 Binary files a/obj/menus.o and b/obj/menus.o differ diff --git a/obj/move.o b/obj/move.o index 79509c1..15fd5f0 100644 Binary files a/obj/move.o and b/obj/move.o differ diff --git a/obj/proj.o b/obj/proj.o index aab73be..53c2d78 100644 Binary files a/obj/proj.o and b/obj/proj.o differ diff --git a/src/base.c b/src/base.c index 9d2ca98..045bf46 100644 --- a/src/base.c +++ b/src/base.c @@ -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) { if(*len > 0) { - (*arr)[index] = (*arr)[*len -1]; + entity* temp = arr[index]; + arr[index] = arr[*len -1]; + arr[*len -1] = temp; *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) { - entity* newarr = malloc(sizeof(entity)*2*(*memlen)); + entity** newarr = malloc(sizeof(entity*)*2*(*memlen)); 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); - *arr = newarr ; + arr = newarr ; *memlen *= 2; } - (*arr)[*len] = ent ; + arr[*len] = ent ; *len += 1; } \ No newline at end of file diff --git a/src/base.h b/src/base.h index 54df312..8f5bfa7 100644 --- a/src/base.h +++ b/src/base.h @@ -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); 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_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0* c); diff --git a/src/display.c b/src/display.c index dbf9ec7..e2afe7e 100644 --- a/src/display.c +++ b/src/display.c @@ -27,6 +27,13 @@ int MAX_SIZE = 8192 ; int* drawOrder ; +GLint loc_scale; +GLint loc_model; +GLint loc_view; +GLint loc_proj; + +GLint loc_frag; + void init_draworder() { 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->vt_angle), (vec3){1.0f, 0.0f, 0.0f}); - glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "scale"), 1, GL_FALSE, (float*)scale); - glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (float*)model); - glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, (float*)view); - glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (float*)projection); + glUniformMatrix4fv(loc_scale, 1, GL_FALSE, (float*)scale); + glUniformMatrix4fv(loc_model, 1, GL_FALSE, (float*)model); + glUniformMatrix4fv(loc_view, 1, GL_FALSE, (float*)view); + 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); 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) { 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_camera(px, py, pz, NULL, NULL, &pz2); - if(pz2 > near) { + project_to_camera(px, py, pz, &px2, &py2, &pz2); + if(pz2 >= near) { 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++) { - if(is_visible(rtd->ents[k].pos, offx, offy, offz)) { - gl_renderCube(shaderProgram, fragmentShader, VAO, VBO, 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); } } } @@ -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_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) { diff --git a/src/entities.c b/src/entities.c index 4aab22a..0e8625c 100644 --- a/src/entities.c +++ b/src/entities.c @@ -21,22 +21,22 @@ bool is_colliding_with_map(cube_0* cb) { for(int k = 0; k < current_room->map_size; k++) { 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) { - return true ; + return true; } } } - return false ; + return false; } bool is_colliding_with_tp(cube_0* cb) { for(int k = 0; k < current_room->tps_size; k++) { 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) { - return true ; + return true; } } } - return false ; + return false; } // ------------------------------------------------------------------------------------------------------------------------------------------------ // @@ -47,9 +47,9 @@ void update_entity(entity* ent, float dtime) { void update_entities(float dtime) { 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"); - 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 dy = (y+h/2 - camy); 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; dy = 170.0*dy/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; } +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) { if(ret->red == 193) { ret->red = 0; @@ -86,9 +90,9 @@ void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { coins += *hp; player_hp -= (*dmg); 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 dy = (y+h/2 - camy); 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; dy = 110.0*dy/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)) { - 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)) { - 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)) { - 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)) { @@ -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) { player_hp -= (*dmg); if(*dmg != 0) { - fade_dmg = 255 ; + fade_dmg = 255; } - *hp = 0 ; + *hp = 0; } \ No newline at end of file diff --git a/src/generation.c b/src/generation.c index 450adbb..061eddd 100644 --- a/src/generation.c +++ b/src/generation.c @@ -14,51 +14,51 @@ #include "entities.h" #include "generation.h" -hashtbl visited ; -room* current_room ; +hashtbl visited; +room* current_room; -int player_chx ; -int player_chy ; +int player_chx; +int player_chy; -entry* pool ; -int pool_size ; -int total_weight ; +entry* pool; +int pool_size; +int total_weight; -int coins ; -int fct_entry_size ; +int coins; +int fct_entry_size; -fct_entry* hashtbl_entities ; +fct_entry* hashtbl_entities; void init_ent_generator(int n) { hashtbl_entities = malloc(sizeof(fct_entry)*n); fct_entry_size = n; 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].name = "Coin"; // 0 = default - hashtbl_entities[0].updatePos = &speen2 ; - hashtbl_entities[0].onHit = &detectHit ; - hashtbl_entities[0].onDeath = NULL ; + hashtbl_entities[0].updatePos = &speen2; + hashtbl_entities[0].onHit = &detectHit; + hashtbl_entities[0].onDeath = NULL; hashtbl_entities[1].id = 1; hashtbl_entities[1].name = "ExplosiveStill"; - hashtbl_entities[1].updatePos = &speen2 ; - hashtbl_entities[1].onHit = &explodeOnHit ; - hashtbl_entities[1].onDeath = NULL ; + hashtbl_entities[1].updatePos = &speen2; + hashtbl_entities[1].onHit = &explodeOnHit; + hashtbl_entities[1].onDeath = NULL; hashtbl_entities[2].id = 2; hashtbl_entities[2].name = "ExplosiveSeek"; - hashtbl_entities[2].updatePos = &go_to_player ; - hashtbl_entities[2].onHit = &explodeOnHit ; - hashtbl_entities[2].onDeath = NULL ; + hashtbl_entities[2].updatePos = &go_to_player; + hashtbl_entities[2].onHit = &explodeOnHit; + hashtbl_entities[2].onDeath = NULL; hashtbl_entities[3].id = 3; hashtbl_entities[3].name = "ExplosiveShoot"; - hashtbl_entities[3].updatePos = &speen3 ; - hashtbl_entities[3].onHit = &explodeOnHit ; - hashtbl_entities[3].onDeath = NULL ; + hashtbl_entities[3].updatePos = &speen3; + hashtbl_entities[3].onHit = &explodeOnHit; + hashtbl_entities[3].onDeath = NULL; } 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) { // considering dest has already been malloc'd - dest->chunk_x = chx ; - dest->chunk_y = chy ; + dest->chunk_x = chx; + dest->chunk_y = chy; dest->map = malloc(sizeof(cube_0*)*src->map_size); for(int k = 0; k < src->map_size; k++) { 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 ); } - dest->map_size = src->map_size ; + dest->map_size = src->map_size; dest->tps = malloc(sizeof(teleporter*)*src->tps_size); for(int k = 0; k < src->tps_size; k++) { 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_chy = src->tps[k]->dest_chy + chy; - dest->tps[k]->dest_x = src->tps[k]->dest_x ; - dest->tps[k]->dest_y = src->tps[k]->dest_y ; - dest->tps[k]->dest_z = src->tps[k]->dest_z ; + dest->tps[k]->dest_x = src->tps[k]->dest_x; + dest->tps[k]->dest_y = src->tps[k]->dest_y; + dest->tps[k]->dest_z = src->tps[k]->dest_z; } - dest->ents = malloc(sizeof(entity)*src->ent_memlen); - dest->ent_memlen = src->ent_memlen ; - dest->ent_len = src->ent_len ; + dest->ents = malloc(sizeof(entity*)*src->ent_memlen); + dest->ent_memlen = src->ent_memlen; + dest->ent_len = src->ent_len; for(int k = 0; k < src->ent_len; k++) { - dest->ents[k].damage = src->ents[k].damage ; - dest->ents[k].hitpoints = malloc(sizeof(int)); - *(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] = malloc(sizeof(entity)); + dest->ents[k]->damage = src->ents[k]->damage; + dest->ents[k]->hitpoints = malloc(sizeof(int)); + dest->ents[k]->meta1 = src->ents[k]->meta1; + dest->ents[k]->meta2 = src->ents[k]->meta2; + dest->ents[k]->meta3 = src->ents[k]->meta3; + 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].onHit = src->ents[k].onHit; - dest->ents[k].onDeath = src->ents[k].onDeath; + dest->ents[k]->updatePos = src->ents[k]->updatePos; + dest->ents[k]->onHit = src->ents[k]->onHit; + 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; } void build_starting_chunk(int chx, int chy) { room* new = malloc(sizeof(room)); - new->chunk_x = chx ; - new->chunk_y = chy ; + new->chunk_x = chx; + new->chunk_y = chy; new->map = malloc(sizeof(cube_0*)*9); - new->map_size = 9 ; + new->map_size = 9; 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[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[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->ent_len = 1 ; - new->ent_memlen = 128 ; - 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[0].damage = 0 ; - new->ents[0].hitpoints = malloc(sizeof(int)); - *(new->ents[0].hitpoints) = 5 ; - new->ents[0].onDeath = NULL ; - new->ents[0].onHit = *detectHit ; - new->ents[0].updatePos = *speen2 ; + new->ents = malloc(sizeof(entity*)*32); + for(int k = 0; k < 32; k++) { + new->ents[k] = malloc(sizeof(entity)); + new->ents[k]->hitpoints = malloc(sizeof(int)); + new->ents[k]->pos = malloc(sizeof(cube_0)); + }; + new->ent_len = 1; + new->ent_memlen = 32; + 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]->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); } @@ -158,32 +176,32 @@ void init_hashtbl() { visited = hashtbl_generate(1789); build_starting_chunk(0, 0); current_room = hashtbl_find_opt(visited, 0, 0); - player_chx = 0 ; - player_chy = 0 ; - total_weight = 0 ; - coins = 0 ; + player_chx = 0; + player_chy = 0; + total_weight = 0; + coins = 0; } void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) { - *ret_cubes = 0 ; - *ret_tps = 0 ; - *ret_ent = 0 ; + *ret_cubes = 0; + *ret_tps = 0; + *ret_ent = 0; - int in_blocks = 0 ; - char c = fgetc(ptr) ; + int in_blocks = 0; + char c = fgetc(ptr); while(c != EOF && c != '$') { //printf("%c", c); if(c == 'B') { - in_blocks = 0 ; + in_blocks = 0; } else if(c == 'T') { - in_blocks = 1 ; + in_blocks = 1; } else if(c == 'E') { - in_blocks = 2 ; + in_blocks = 2; } else if(c == '[') { if(in_blocks == 0) { - *ret_cubes += 1 ; + *ret_cubes += 1; } else if(in_blocks == 1) { - *ret_tps += 1 ; + *ret_tps += 1; } else { *ret_ent += 1; } @@ -203,24 +221,24 @@ void align_to(FILE* ptr, char ch) { int read_int(FILE* ptr, bool print) { bool is_reading = false; - int buffer = 0 ; - int sign = 1 ; + int buffer = 0; + int sign = 1; char c = fgetc(ptr); while(c != EOF) { if(c == '-') { - sign = -1 ; + sign = -1; } else if((int)c >= 48 && (int)c <= 57) { - is_reading = true ; - buffer = 10*buffer + (int)c - 48 ; + is_reading = true; + buffer = 10*buffer + (int)c - 48; } else if(is_reading) { /*if(print) { - printf("%d ; ", buffer*sign); + printf("%d; ", buffer*sign); }*/ return buffer*sign; } c = fgetc(ptr); } - return buffer*sign ; + return buffer*sign; } double sign(double __x) { @@ -233,7 +251,7 @@ double sign(double __x) { double read_float(FILE* ptr) { int ent = read_int(ptr, false); int frac = read_int(ptr, false); - //printf("%d.%d ; ", ent, frac); + //printf("%d.%d; ", ent, frac); if(ent != 0.0) { return (ent/abs(ent))*(absf((double)ent) + ((double)frac)/(pow(10.0, (double)ln_baseN(frac, 10)))); } else { @@ -247,18 +265,15 @@ void parse_one_room(int id, char* filename) { FILE* ptr = fopen(filename, "r"); pool[id].area = malloc(sizeof(room)); - int ncubes ; - int ntps ; - int nent ; + int ncubes; + int ntps; + int nent; - FILE* ptr2 = fopen(filename, "r") ; + FILE* ptr2 = fopen(filename, "r"); get_number_blocks(&ncubes, &ntps, &nent, ptr2); printf("(%d, %d, %d)\n", ncubes, ntps, nent); - int nmemlen = 128 ; - while(nmemlen < nent) { - nmemlen *= 2.; - } + int nmemlen = maxd(nent, 64); pool[id].area->map = malloc(sizeof(cube_0*)*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_size = ntps; - pool[id].area->ents = malloc(sizeof(entity)*nmemlen); - pool[id].area->ent_len = nent ; - pool[id].area->ent_memlen = nmemlen ; + pool[id].area->ents = malloc(sizeof(entity*)*nmemlen); + for(int k = 0; k < nmemlen; k++) { + 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"); fflush(stdout); @@ -336,17 +356,23 @@ void parse_one_room(int id, char* filename) { if(entry == NULL) { entry = get_entry(0); } - pool[id].area->ents[k].pos = create_cube_0(cx, cy, cz, cw, ch, cd, chz, cvt, red, green, blue); - 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].updatePos = entry->updatePos ; - pool[id].area->ents[k].onHit = entry->onHit ; - pool[id].area->ents[k].onDeath = entry->onDeath ; - //pool[id].area->ents[k].updatePos = &speen2 ; - //pool[id].area->ents[k].onHit = &detectHit ; - //pool[id].area->ents[k].onDeath = NULL ; + 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]->hitpoints) = hp; + pool[id].area->ents[k]->updatePos = entry->updatePos; + pool[id].area->ents[k]->onHit = entry->onHit ; + pool[id].area->ents[k]->onDeath = entry->onDeath ; + pool[id].area->ents[k]->meta1 = 0; + pool[id].area->ents[k]->meta2 = 0; + pool[id].area->ents[k]->meta3 = 0; + pool[id].area->ents[k]->meta4 = 0; + 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"); } @@ -375,44 +401,44 @@ void parse_one_room(int id, char* filename) { printf("\n\n"); pool[id].weight = read_int(ptr, true); - total_weight += pool[id].weight ; + total_weight += pool[id].weight; fclose(ptr); } void parse_rooms(int n_rooms) { char* name = malloc(sizeof(char)*19); // 1000 rooms max - name[0] = 't' ; - name[1] = 'e' ; - name[2] = 'm' ; - name[3] = 'p' ; - name[4] = 'l' ; - name[5] = 'a' ; - name[6] = 't' ; - name[7] = 'e' ; - name[8] = 's' ; - name[9] = '/' ; - name[10] = 'r' ; - name[11] = 'o' ; - name[12] = 'o' ; - name[13] = 'm' ; - name[14] = '_' ; - name[15] = '0' ; - name[16] = '\0' ; - name[17] = '\0' ; - name[18] = '\0' ; + name[0] = 't'; + name[1] = 'e'; + name[2] = 'm'; + name[3] = 'p'; + name[4] = 'l'; + name[5] = 'a'; + name[6] = 't'; + name[7] = 'e'; + name[8] = 's'; + name[9] = '/'; + name[10] = 'r'; + name[11] = 'o'; + name[12] = 'o'; + name[13] = 'm'; + name[14] = '_'; + name[15] = '0'; + name[16] = '\0'; + name[17] = '\0'; + name[18] = '\0'; pool = malloc(sizeof(entry)*n_rooms); - pool_size = n_rooms ; + pool_size = n_rooms; printf("Parsing...\n"); for(int k = 0; k < n_rooms; k++) { if(k < 10) { - name[15] = (char)(k%10 + 48) ; + name[15] = (char)(k%10 + 48); } else if(k < 100) { - name[15] = (char)((k/10)%10 + 48) ; - name[16] = (char)(k%10 + 48) ; + name[15] = (char)((k/10)%10 + 48); + name[16] = (char)(k%10 + 48); } parse_one_room(k, name); } @@ -423,14 +449,14 @@ void parse_rooms(int n_rooms) { 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) { 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++) { newMap[k] = malloc(sizeof(cube_0)); - } for(int k = 0; k < r->map_size; k++) { @@ -443,7 +469,7 @@ void generate_terrain(room* r) { int i = r->map_size; for(int l = -room_width; l < room_width; l+=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; } } @@ -458,19 +484,20 @@ void generate_nearby_chunks(int render_dist) { if(!hashtbl_mem(visited, 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); - int pick = rand()%total_weight ; - int sum = 0 ; + int pick = rand()%total_weight; + int sum = 0; for(int k = 0; k < pool_size; k++) { - sum += pool[k].weight ; + sum += pool[k].weight; if(pick < sum) { printf("chose %d\n", k); room* new = malloc(sizeof(room)); 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); 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]); } - for(int k = 0; k < pool[k0].area->ent_len; k++) { - free(pool[k0].area->ents[k].hitpoints); - free(pool[k0].area->ents[k].pos); + for(int k = 0; k < pool[k0].area->ent_memlen; k++) { + free(pool[k0].area->ents[k]->hitpoints); + free(pool[k0].area->ents[k]->pos); + free(pool[k0].area->ents[k]); } free(pool[k0].area->ents); free(pool[k0].area->tps); diff --git a/src/hash.c b/src/hash.c index 8096938..aed16b7 100644 --- a/src/hash.c +++ b/src/hash.c @@ -65,9 +65,10 @@ void free_all_cubes(room* r) { free(r->tps[k]->hitbox); free(r->tps[k]); } - for(int k = 0; k < r->ent_len; k++) { - free(r->ents[k].hitpoints); - free(r->ents[k].pos); + for(int k = 0; k < r->ent_memlen; k++) { + free(r->ents[k]->hitpoints); + free(r->ents[k]->pos); + free(r->ents[k]); } free(r->ents); free(r->tps); diff --git a/src/main.c b/src/main.c index e56b697..2d63454 100644 --- a/src/main.c +++ b/src/main.c @@ -88,10 +88,10 @@ void processInput(GLFWwindow *window, float dtime) { } 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) { - rot_vt += sensitivity; + rot_vt = mind(3.14159/2.0, rot_vt+sensitivity); } } @@ -290,10 +290,12 @@ int main_alt() { int fps = 60 ; int interval = 1000000/fps ; + float delta; + clock_t finish = clock(); clock_t origin = clock(); - while(!glfwWindowShouldClose(window) && player_hp >= 0) { + while(!glfwWindowShouldClose(window) && player_hp > 0) { // input // ----- glClearColor(0.2f, 0.3f, 0.3f, 1.0f); @@ -301,7 +303,9 @@ int main_alt() { triCount = 0; origin = clock(); + fflush(stdout); generate_nearby_chunks(1); + fflush(stdout); // draw the map glUseProgram(shaderProgram); @@ -316,20 +320,21 @@ int main_alt() { glBindVertexArray(RVAO); 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_drawData(shaderProgramR); // 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(); - update_entities(((float)finish - (float)origin)/CLOCKS_PER_SEC); - updateProj(((float)finish - (float)origin)/CLOCKS_PER_SEC); - //printf("%f\n", 1.0f/(((float)finish - (float)origin)/CLOCKS_PER_SEC)); + update_entities(delta); + updateProj(delta); + //printf("%f\n", 1.0f/(delta)); //printf("%lf, %lf, %lf\n", camx, camy, camz); - usleep(interval); + usleep(max(0, interval-(int)(1000000*delta))); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/src/move.c b/src/move.c index 1f19f76..f912d56 100644 --- a/src/move.c +++ b/src/move.c @@ -92,15 +92,15 @@ bool is_colliding(float dtime) { } } 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(current_room->ents[k].onHit != NULL) { - (*current_room->ents[k].onHit)(dtime, current_room->ents[k].hitpoints, ¤t_room->ents[k].damage, &(*(current_room->ents[k].pos))); - if(*(current_room->ents[k].hitpoints) <= 0) { - if(current_room->ents[k].onDeath != NULL) { - (*current_room->ents[k].onDeath)(dtime); + if(current_room->ents[k]->onHit != NULL) { + (*current_room->ents[k]->onHit)(dtime, current_room->ents[k]->hitpoints, ¤t_room->ents[k]->damage, &(*(current_room->ents[k]->pos))); + if(*(current_room->ents[k]->hitpoints) <= 0) { + if(current_room->ents[k]->onDeath != NULL) { + (*current_room->ents[k]->onDeath)(dtime); } - remove_entity(¤t_room->ents, ¤t_room->ent_memlen, ¤t_room->ent_len, k); + remove_entity(current_room->ents, ¤t_room->ent_memlen, ¤t_room->ent_len, k); } } return true; diff --git a/src/structure.h b/src/structure.h index 9596e3b..f31915a 100644 --- a/src/structure.h +++ b/src/structure.h @@ -41,6 +41,15 @@ typedef struct entity { // triggers when goes negative (death) void (*onDeath)(float dtime); + int meta1; + int meta2; + int meta3; + int meta4; + double meta5; + double meta6; + double meta7; + double meta8; + int damage; int* hitpoints; } entity; @@ -53,7 +62,7 @@ struct room { int map_size; teleporter** tps; int tps_size; - entity* ents; + entity** ents; int ent_len; int ent_memlen; };