diff --git a/bin/back b/bin/back index a5f5970..3fda94d 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index a3932ca..ee2db11 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index 7ccb769..126a76d 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/entities.o b/obj/entities.o index 7c54a64..c004833 100644 Binary files a/obj/entities.o and b/obj/entities.o differ diff --git a/obj/generation.o b/obj/generation.o index d39b8d7..767b7e0 100644 Binary files a/obj/generation.o and b/obj/generation.o differ diff --git a/obj/move.o b/obj/move.o index 715fff6..86641c2 100644 Binary files a/obj/move.o and b/obj/move.o differ diff --git a/src/base.c b/src/base.c index b042a3c..d5f0da2 100644 --- a/src/base.c +++ b/src/base.c @@ -69,6 +69,20 @@ int max(int a, int b) { return a; } +double mind(double a, double b) { + if(a > b) { + return b; + }; + return a; +} + +double maxd(double a, double b) { + if(a < b) { + return b; + }; + return a; +} + double absf(double n) { if(n > 0.0f) { return n; @@ -254,6 +268,29 @@ double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) { // ------------------------------------------------------------------------------------------------ // +void remove_entity(entity** arr, int* memlen, int* len, int index) { + if(*len > 0) { + (*arr)[index] = (*arr)[*len -1]; + *len -= 1; + } +} + +void add_entity(entity** arr, int* memlen, int* len, entity ent) { + if(*memlen == *len) { + entity* newarr = malloc(sizeof(entity)*2*(*memlen)); + for(int k = 0; k < *len; k++) { + newarr[k] = (*arr)[k] ; + } + free(*arr); + *arr = newarr ; + *memlen *= 2; + } + (*arr)[*len] = ent ; + *len += 1; +} + +// ------------------------------------------------------------------------------------------------ // + void import_digits(SDL_Renderer* renderer) { imgs res; res.arr = malloc(sizeof(SDL_Texture*)*11); diff --git a/src/base.h b/src/base.h index 2037d42..6c0169a 100644 --- a/src/base.h +++ b/src/base.h @@ -7,6 +7,8 @@ double pwf(double x, int n); int abs(int n); int min(int a, int b); int max(int a, int b); +double mind(double a, double b); +double maxd(double a, double b); double absf(double n); int convex_seg(int x1, int x2, double theta); bool is_an_integer(char c); @@ -36,6 +38,10 @@ double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, d double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c); double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz); double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c); + +void remove_entity(entity** arr, int* memlen, int* len, int index); +void add_entity(entity** arr, int* memlen, int* len, entity ent); + double distance_pt_cube_3d(double x0, double y0, double z0, cube cb); void import_digits(SDL_Renderer* renderer); diff --git a/src/display.c b/src/display.c index b08ec24..f5d109b 100644 --- a/src/display.c +++ b/src/display.c @@ -605,11 +605,13 @@ void swap_ent(entity* arr, int i, int j) { arr[j] = temp; } +int multhz = 10.0 ; + void insertionSort_cb(cube_0* arr, int len) { for(int k = 0; k < len; k++) { int j = k-1 ; while(j >= 0) { - if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j]) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j+1])) { + if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j]) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j+1])) { swap_cb(arr, j, j+1); j -= 1; } else { @@ -623,7 +625,7 @@ void insertionSort_tp(teleporter* arr, int len) { for(int k = 0; k < len; k++) { int j = k-1 ; while(j >= 0) { - if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j].hitbox) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, arr[j+1].hitbox)) { + if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j].hitbox) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j+1].hitbox)) { swap_tp(arr, j, j+1); j -= 1; } else { @@ -637,7 +639,7 @@ void insertionSort_ent(entity* arr, int len) { for(int k = 0; k < len; k++) { int j = k-1 ; while(j >= 0) { - if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, *(arr[j].pos)) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, 8.5, 1.0, *(arr[j+1].pos))) { + if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(arr[j].pos)) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(arr[j+1].pos))) { swap_ent(arr, j, j+1); j -= 1; } else { @@ -660,6 +662,62 @@ void drawCurrentRoom(SDL_Renderer* renderer) { for(int k = 0; k < current_room->tps_size; k++) { drawFullCube(renderer, current_room->tps[k].hitbox); } + + /*int k_cb = 0 ; + int k_tp = 0 ; + int k_et = 0 ; + + int updated = 1+2+4 ; + + double dcb = -1.0 ; + double dtp = -1.0 ; + double det = -1.0 ; + while(k_cb < current_room->map_size || k_tp < current_room->tps_size || k_et < current_room->ent_len) { + if(updated == 7) { + if(k_et < current_room->ent_len) { + det = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(current_room->ents[k_et].pos)); + } + if(k_tp < current_room->tps_size) { + dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->tps[k_tp].hitbox); + } + if(k_cb < current_room->map_size) { + dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->map[k_cb]); + } + } else if((updated/4)%2 == 1) { + if(k_et < current_room->ent_len) { + det = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(current_room->ents[k_et].pos)); + } else { + det = -1.0 ; + } + } else if((updated/2)%2 == 1) { + if(k_tp < current_room->tps_size) { + dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->tps[k_tp].hitbox); + } else { + dtp = -1.0 ; + } + } else if(updated%2 == 1) { + if(k_cb < current_room->map_size) { + dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->map[k_cb]); + } else { + dcb = -1.0 ; + } + } + updated = 0 ; + double mn = maxd(maxd(dcb, dtp), det); + if(mn == dcb) { + drawFullCube(renderer, current_room->map[k_cb]); + updated += 1 ; + k_cb += 1; + } else if(mn == dtp) { + drawFullCube(renderer, current_room->tps[k_tp].hitbox); + updated += 2 ; + k_tp += 1; + } else { + drawFullCube(renderer, *(current_room->ents[k_et].pos)); + updated += 4; + k_et += 1; + } + }*/ } // -------------------------------------------------------------------------------------------------------------------------------- // diff --git a/src/entities.c b/src/entities.c index 12a65a2..62a1129 100644 --- a/src/entities.c +++ b/src/entities.c @@ -58,18 +58,19 @@ void update_entities(float dtime) { // ------------------------------------------------------------------------------------------------------------------------------------------------ // void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) { - ret->vt_angle += ((double)dtime)*5.0; + ret->vt_angle += ((double)dtime)*15.0; } void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, cube_0* ret) { - ret->hz_angle += ((double)dtime)*5.0; + ret->hz_angle += ((double)dtime)*15.0; } void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) { - if(ret->red == 255) { + if(ret->red == 193) { ret->red = 0; ret->green = 192; ret->blue = 0; - coins += 1; + coins += *hp; + *hp = 0 ; } } \ No newline at end of file diff --git a/src/generation.c b/src/generation.c index 0f508a2..5b5c245 100644 --- a/src/generation.c +++ b/src/generation.c @@ -55,11 +55,13 @@ void copy_room(room* src, room* dest, int chx, int chy) { 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_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 = src->ents[k].hitpoints ; + dest->ents[k].hitpoints = malloc(sizeof(int)); + *(dest->ents[k].hitpoints) = *(src->ents[k].hitpoints) ; dest->ents[k].pos = malloc(sizeof(cube_0)); *(dest->ents[k].pos) = create_cube_0( (*(src->ents[k].pos)).x, (*(src->ents[k].pos)).y, (*(src->ents[k].pos)).z, @@ -96,12 +98,14 @@ 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, 3.14159/4.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, 3.14159/4.0, 0.0, 0, 0, 255 , chx , chy+1, 2.5, 2.5, 2.5); - new->ents = malloc(sizeof(entity)*1); + new->ents = malloc(sizeof(entity)*128); new->ent_len = 1 ; + new->ent_memlen = 128 ; new->ents[0].pos = malloc(sizeof(cube_0)); - *(new->ents[0].pos) = create_cube_0(-0.25, -2.25, -0.25, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 128, 0); + *(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 = 900 ; + 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 ; @@ -210,12 +214,18 @@ void parse_one_room(int id, char* filename) { get_number_blocks(&ncubes, &ntps, &nent, ptr2); printf("(%d, %d, %d)\n", ncubes, ntps, nent); + int nmemlen = 128 ; + while(nmemlen < nent) { + nmemlen *= 2.; + } + pool[id].area->map = malloc(sizeof(cube_0)*ncubes); pool[id].area->map_size = ncubes; pool[id].area->tps = malloc(sizeof(teleporter)*ntps); pool[id].area->tps_size = ntps; - pool[id].area->ents = malloc(sizeof(entity)*nent); + pool[id].area->ents = malloc(sizeof(entity)*nmemlen); pool[id].area->ent_len = nent ; + pool[id].area->ent_memlen = nmemlen ; for(int k = 0; k < ncubes; k++) { align_to(ptr, '['); diff --git a/src/move.c b/src/move.c index e0d1c47..8b76957 100644 --- a/src/move.c +++ b/src/move.c @@ -20,7 +20,7 @@ // ---------------------------------------------------------------------------------------------------- // double sensitivity = 0.22 ; double fov = 90.0 ; -double speed = 1.0 ; +double speed = 0.22 ; double min_dist = 0.7 ; // ---------------------------------------------------------------------------------------------------- // @@ -49,7 +49,7 @@ void set_player_coords(int old_chx, int old_chy) { if(current_room->tps[k].dest_chx == old_chx && current_room->tps[k].dest_chy == old_chy) { if(true) { camx = current_room->tps[k].hitbox.x + current_room->tps[k].hitbox.w/2.0; - camy = current_room->tps[k].hitbox.y + current_room->tps[k].hitbox.h +1.0; + camy = current_room->tps[k].hitbox.y + current_room->tps[k].hitbox.h +1.5; camz = current_room->tps[k].hitbox.z + current_room->tps[k].hitbox.d/2.0; } return ; @@ -81,6 +81,12 @@ bool is_colliding(float dtime) { 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); + } + remove_entity(¤t_room->ents, ¤t_room->ent_memlen, ¤t_room->ent_len, k); + } } return true ; } @@ -109,80 +115,75 @@ void playerActions(float dtime) { rot_vt = 3.0*3.14159/2.0 ; } break; - case SDL_KEYDOWN: - has_changed = true ; - switch (event.key.keysym.sym) { - case SDLK_z: - for(int k = 0; k < 10; k++) { - camz += speed*cos(rot_hz)*cos(rot_vt)/10; - camx -= speed*sin(rot_hz)*cos(rot_vt)/10; - camy += speed*sin(rot_vt)/10; - if(is_colliding(dtime)) { - camz -= speed*cos(rot_hz)*cos(rot_vt)/10; - camx += speed*sin(rot_hz)*cos(rot_vt)/10; - camy -= speed*sin(rot_vt)/10; - k=11; - } - } - break; - case SDLK_q: - for(int k = 0; k < 10; k++) { - camx -= speed*cos(rot_hz)/10; - camz -= speed*sin(rot_hz)/10; - if(is_colliding(dtime)) { - camx += speed*cos(rot_hz)/10; - camz += speed*sin(rot_hz)/10; - k=11; - } - } - break; - case SDLK_s: - for(int k = 0; k < 10; k++) { - camz -= speed*cos(rot_hz)*cos(rot_vt)/10; - camx += speed*sin(rot_hz)*cos(rot_vt)/10; - camy -= speed*sin(rot_vt)/10; - if(is_colliding(dtime)) { - camz += speed*cos(rot_hz)*cos(rot_vt)/10; - camx -= speed*sin(rot_hz)*cos(rot_vt)/10; - camy += speed*sin(rot_vt)/10; - k=11; - } - } - break; - case SDLK_d: - for(int k = 0; k < 10; k++) { - camx += speed*cos(rot_hz)/10; - camz += speed*sin(rot_hz)/10; - if(is_colliding(dtime)) { - camx -= speed*cos(rot_hz)/10; - camz -= speed*sin(rot_hz)/10; - k=11; - } - } - break; - case SDLK_t: - fprintf(stderr, "Killed.\n") ; - exit(1) ; - break; - - case SDLK_a: - rot_hz -= sensitivity ; - break; - case SDLK_e: - rot_hz += sensitivity ; - break; - - case SDLK_p: - rot_vt -= sensitivity ; - break; - case SDLK_m: - rot_vt += sensitivity ; - break; - - default: - break; - } } } + const Uint8* state = SDL_GetKeyboardState(NULL); + if(state[SDL_SCANCODE_Z] == 1) { + for(int k = 0; k < 10; k++) { + camz += speed*cos(rot_hz)*cos(rot_vt)/10; + camx -= speed*sin(rot_hz)*cos(rot_vt)/10; + camy += speed*sin(rot_vt)/10; + if(is_colliding(dtime)) { + camz -= speed*cos(rot_hz)*cos(rot_vt)/10; + camx += speed*sin(rot_hz)*cos(rot_vt)/10; + camy -= speed*sin(rot_vt)/10; + k=11; + } + } + } + if(state[SDL_SCANCODE_Q] == 1) { + for(int k = 0; k < 10; k++) { + camx -= speed*cos(rot_hz)/10; + camz -= speed*sin(rot_hz)/10; + if(is_colliding(dtime)) { + camx += speed*cos(rot_hz)/10; + camz += speed*sin(rot_hz)/10; + k=11; + } + } + } + if(state[SDL_SCANCODE_S] == 1) { + for(int k = 0; k < 10; k++) { + camz -= speed*cos(rot_hz)*cos(rot_vt)/10; + camx += speed*sin(rot_hz)*cos(rot_vt)/10; + camy -= speed*sin(rot_vt)/10; + if(is_colliding(dtime)) { + camz += speed*cos(rot_hz)*cos(rot_vt)/10; + camx -= speed*sin(rot_hz)*cos(rot_vt)/10; + camy += speed*sin(rot_vt)/10; + k=11; + } + } + } + if(state[SDL_SCANCODE_D] == 1) { + for(int k = 0; k < 10; k++) { + camx += speed*cos(rot_hz)/10; + camz += speed*sin(rot_hz)/10; + if(is_colliding(dtime)) { + camx -= speed*cos(rot_hz)/10; + camz -= speed*sin(rot_hz)/10; + k=11; + } + } + } + + if(state[SDL_SCANCODE_T] == 1) { + fprintf(stderr, "Killed.\n") ; + exit(1) ; + } + + if(state[SDL_SCANCODE_A] == 1) { + rot_hz -= sensitivity ; + } + if(state[SDL_SCANCODE_E] == 1) { + rot_hz += sensitivity ; + } + + if(state[SDL_SCANCODE_P] == 1) { + rot_vt -= sensitivity ; + } + if(state[SDL_SCANCODE_M] == 1) { + rot_vt += sensitivity ; + } } \ No newline at end of file diff --git a/src/structure.h b/src/structure.h index 2f78f13..08bc903 100644 --- a/src/structure.h +++ b/src/structure.h @@ -54,6 +54,7 @@ struct room { int tps_size ; entity* ents ; int ent_len ; + int ent_memlen ; } ; typedef struct room room ; diff --git a/templates/room_1 b/templates/room_1 index d2262f3..d7b0c93 100644 --- a/templates/room_1 +++ b/templates/room_1 @@ -11,6 +11,16 @@ Teleporters : [-1.0, 0.0, 7.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 255, 0; 0, 1] [7.0, 0.0, -1.0, 2.0, 1.0, 2.0, 0.0, 0.0, 0, 0, 255; 1, 0] +Entities : +[0.0, 3.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 4.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 5.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 6.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 7.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 8.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 9.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] + Weight : 10 diff --git a/templates/room_2 b/templates/room_2 index f1185e6..7e8b3cf 100644 --- a/templates/room_2 +++ b/templates/room_2 @@ -14,10 +14,10 @@ Teleporters : [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] Entities : -[3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 255, 0, 900, 20] -[-3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 255, 0, 900, 20] -[3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 255, 0, 900, 20] -[-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 255, 0, 900, 20] +[3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[-3.0, 3.0, 3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] +[-3.0, 3.0, -3.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] Weight : 20