optimizations to entity processing + added enemy1 (walking seeker) + fixed segFault on addEntity

This commit is contained in:
Alexandre 2025-03-22 12:05:16 +01:00
parent 97db1731e6
commit 7d310d3f71
21 changed files with 336 additions and 110 deletions

View File

@ -137,4 +137,9 @@ entities:
all\{dmg} = double (cooldown > 0.0 and ttl > 0.0 and psize_{x,y,z} > 0.0) all\{dmg} = double (cooldown > 0.0 and ttl > 0.0 and psize_{x,y,z} > 0.0)
dmg = int (>0) dmg = int (>0)
-> 16 (type 1 entity)
[.. speed, jump_height, dmg, kbPlayer, kbEntity] with
all\{dmg} = double[>=0.0]
dmg = int (>=0)
``` ```

BIN
bin/back

Binary file not shown.

View File

@ -18,9 +18,14 @@ Blocks :
[ 15.0, 0.0, 1.5, 1.0, 8.0, 14.5, 0.0, 0.0, 192, 192, 192] [ 15.0, 0.0, 1.5, 1.0, 8.0, 14.5, 0.0, 0.0, 192, 192, 192]
[ 9.0, 0.0, -2.5, 1.0, 8.0, 5.0, 0.0, 0.0, 192, 192, 192] [ 9.0, 0.0, -2.5, 1.0, 8.0, 5.0, 0.0, 0.0, 192, 192, 192]
[ -2.0, 0.0, -2.0, 4.0, 2.0, 4.0, 0.0, 0.0, 192, 192, 192]
Entities : Entities :
[-1.0, 0.0, -1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 192, 192, 32, 1, 0, 8, 250, 1, 255, 128, 128] [-5.0, 0.5, -5.0, 1.0, 2.0, 1.0, 0.0, 0.0, 64, 64, 64, 1, 0, 16, 2.5, 1.9, 0, 0.0, 0.0]
[-0.5, 0.5, -0.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 32, 1, 0, 7, levels/level_05/, 5, impressive, 128, 255, 128] [ 4.0, 0.5, 4.0, 1.0, 2.0, 1.0, 0.0, 0.0, 64, 64, 64, 1, 0, 16, 2.5, 1.9, 0, 0.0, 0.0]
[-1.0, 2.0, -1.0, 2.0, 2.0, 2.0, 0.0, 0.0, 192, 192, 32, 1, 0, 8, 250, 1, 255, 128, 128]
[-0.5, 2.5, -0.5, 1.0, 1.0, 1.0, 0.0, 0.0, 192, 192, 32, 1, 0, 7, levels/level_05/, 5, impressive, 128, 255, 128]
Weight : Weight :
0 0

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

@ -276,6 +276,39 @@ void copy_cube(cube_0* src, cube_0* dest) {
dest->d = src->d; dest->d = src->d;
} }
void copy_entity(entity* src, entity* dest) {
dest->damage = src->damage;
dest->entity_type = src->entity_type;
*(dest->hitpoints) = *(src->hitpoints);
dest->metad1 = src->metad1;
dest->metad2 = src->metad2;
dest->metad3 = src->metad3;
dest->metad4 = src->metad4;
dest->metad5 = src->metad5;
dest->metad6 = src->metad6;
dest->metad7 = src->metad7;
dest->metad8 = src->metad8;
dest->metad9 = src->metad9;
dest->metai1 = src->metai1;
dest->metai2 = src->metai2;
dest->metai3 = src->metai3;
dest->metai4 = src->metai4;
dest->metai5 = src->metai5;
dest->metai6 = src->metai6;
dest->metach1 = src->metach1;
dest->metach2 = src->metach2;
dest->updatePos = src->updatePos;
dest->onHit = src->onHit;
dest->onDeath = src->onDeath;
dest->tex = src->tex;
dest->tex2 = src->tex2;
copy_cube(src->pos, dest->pos);
}
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) { double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
@ -434,20 +467,33 @@ void remove_entity(entity** arr, int* memlen, int* len, int index) {
} }
} }
entity* remove_entity_return(entity** arr, int* memlen, int* len, int index) {
if(*len > 0) {
entity* temp = arr[index];
arr[index] = arr[*len -1];
arr[*len -1] = temp;
*len -= 1;
return temp;
}
return NULL;
}
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] = malloc(sizeof(entity));
newarr[k] = arr[k]; newarr[k] = arr[k];
free(arr[k]); }
for(int k = *len; k < 2*(*memlen); k++) {
newarr[k] = malloc(sizeof(entity));
newarr[k]->pos = malloc(sizeof(cube_0));
newarr[k]->hitpoints = malloc(sizeof(int));
} }
free(*arr); free(*arr);
arr = newarr; arr = newarr;
*memlen *= 2; *memlen *= 2;
} }
free(arr[*len]); copy_entity(ent, arr[*len]);
arr[*len] = ent;
*len += 1; *len += 1;
} }

View File

@ -42,6 +42,7 @@ teleporter* create_teleporter(
int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest int chx_dest, int chy_dest, double x_dest, double y_dest, double z_dest
); );
void copy_cube(cube_0* src, cube_0* dest); void copy_cube(cube_0* src, cube_0* dest);
void copy_entity(entity* src, entity* dest);
double convex_pt(double a, double b, double theta); double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1); double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
@ -56,6 +57,7 @@ double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0* c);
double distance_pt_cube_0_3d_infinite(double x0, double y0, double z0, cube_0* c); double distance_pt_cube_0_3d_infinite(double x0, double y0, double z0, cube_0* c);
void remove_entity(entity** arr, int* memlen, int* len, int index); void remove_entity(entity** arr, int* memlen, int* len, int index);
entity* remove_entity_return(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);

View File

@ -21,49 +21,152 @@
#include "entities.h" #include "entities.h"
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
double choffx, choffz;
double choffxE, choffzE;
// necessary condition for 2 boxes to collide (w*w + h*h + d*d is (diagonal length of the cube)²)
bool is_at_least_somewhat_close(cube_0* c1, cube_0* c2) {
return (
distance_pt_pt_3d_sq(c1->x+c1->w/2.0, c1->y+c1->h/2.0, c1->z+c1->d/2.0, (c2->x+choffx)+c2->w/2.0, c2->y+c2->h/2.0, (c2->z+choffz)+c2->d/2.0) <=
c1->w*c1->w+c1->h*c1->h+c1->d*c1->d + c2->w*c2->w+c2->h*c2->h+c2->d*c2->d
);
}
bool is_colliding_with_map(cube_0* cb) { bool is_colliding_with_map(cube_0* cb) {
for(int k = 0; k < current_room->map_size; k++) { for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
room* rtd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
if(rtd != NULL) {
choffx = 2*room_width*w;
choffz = 2*room_depth*h;
for(int k = 0; k < rtd->map_size; k++) {
if(is_at_least_somewhat_close(cb, rtd->map[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.001) { if(distance_pt_cube_0_3d((cb->x-choffx)+cb->w*(d%2==0), cb->y+cb->h*((d/2)%2==0), (cb->z-choffz)+cb->d*((d/4)%2==0), rtd->map[k]) <= 0.001) {
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 w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
room* rtd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
if(rtd != NULL) {
choffx = 2*room_width*w;
choffz = 2*room_depth*h;
for(int k = 0; k < rtd->tps_size; k++) {
if(is_at_least_somewhat_close(cb, rtd->tps[k]->hitbox)) {
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.001) { 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), rtd->tps[k]->hitbox) <= 0.001) {
return true; return true;
} }
} }
} }
}
}
}
}
return false; return false;
} }
bool is_colliding_with_ent_sp(cube_0* cb, int allowed) { bool is_colliding_with_ent_sp(cube_0* cb, int allowed) {
for(int k = 0; k < current_room->ent_len; k++) { for(int w = -render_distance; w <= render_distance; w++) {
for(int d = 0; d < 8; d++) { for(int h = -render_distance; h <= render_distance; h++) {
room* rtd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
if(rtd != NULL) {
choffx = 2*room_width*w;
choffz = 2*room_depth*h;
for(int k = 0; k < rtd->ent_len; k++) {
if( if(
current_room->ents[k]->entity_type == allowed && rtd->ents[k]->entity_type == allowed &&
(cb->x != current_room->ents[k]->pos->x || cb->y != current_room->ents[k]->pos->y || cb->z != current_room->ents[k]->pos->z) && (cb->x != rtd->ents[k]->pos->x || cb->y != rtd->ents[k]->pos->y || cb->z != rtd->ents[k]->pos->z) &&
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->ents[k]->pos) <= 0.001 is_at_least_somewhat_close(cb, rtd->ents[k]->pos)
) { ) {
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), rtd->ents[k]->pos) <= 0.001) {
return true; return true;
} }
} }
} }
}
}
}
}
return false;
}
bool is_colliding_with_ent_sp2(cube_0* cb, int allowed1, int allowed2) {
for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
room* rtd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
if(rtd != NULL) {
choffx = 2*room_width*w;
choffz = 2*room_depth*h;
for(int k = 0; k < rtd->ent_len; k++) {
if(
(rtd->ents[k]->entity_type == allowed1 || rtd->ents[k]->entity_type == allowed2) &&
(cb->x != rtd->ents[k]->pos->x || cb->y != rtd->ents[k]->pos->y || cb->z != rtd->ents[k]->pos->z) &&
is_at_least_somewhat_close(cb, rtd->ents[k]->pos)
) {
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), rtd->ents[k]->pos) <= 0.001) {
return true;
}
}
}
}
}
}
}
return false; return false;
} }
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
static double choffx, choffz; // in case some entities escape their chunk
void update_entity_chunk() {
if(!is_one_room) {
for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) {
room* rtd = hashtbl_find_opt(visited, player_chx+w, player_chy+h);
if(rtd != NULL) {
for(int k = 0; k < rtd->ent_len; k++) {
int chdx = (int)(rtd->ents[k]->pos->x/room_width);
int chdz = (int)(rtd->ents[k]->pos->z/room_depth);
if(chdx != 0 || chdz != 0) {
entity* deld = remove_entity_return(rtd->ents, &rtd->ent_memlen, &rtd->ent_len, k);
//printf("(%lf, %lf) -> ", deld->pos->x, deld->pos->z);
deld->pos->x -= 2.0*room_width*chdx;
deld->pos->z -= 2.0*room_depth*chdz;
//printf("(%lf, %lf)\n", deld->pos->x, deld->pos->z);
room* dest = hashtbl_find_opt(visited, player_chx+w+chdx, player_chy+h+chdz);
if(dest != NULL) {
add_entity(dest->ents, &dest->ent_memlen, &dest->ent_len, deld);
}
k -= 1;
}
}
}
}
}
}
}
void update_entity(entity* ent, float dtime) { void update_entity(entity* ent, float dtime) {
(*ent->updatePos)(ent->pos->x, ent->pos->y, ent->pos->z, ent->pos->w, ent->pos->h, ent->pos->d, ent->pos->hz_angle, ent->pos->vt_angle, dtime, ent, ent->pos); (*ent->updatePos)(dtime, ent, ent->pos);
} }
void update_entities(float dtime, room* rtd) { void update_entities(float dtime, room* rtd) {
@ -80,8 +183,8 @@ void update_entities(float dtime, room* rtd) {
void update_nearby_entities(float dtime, int render_distance) { void update_nearby_entities(float dtime, int render_distance) {
for(int w = -render_distance; w <= render_distance; w++) { for(int w = -render_distance; w <= render_distance; w++) {
for(int h = -render_distance; h <= render_distance; h++) { for(int h = -render_distance; h <= render_distance; h++) {
choffx = 2*room_width*w; choffxE = 2*room_width*w;
choffz = 2*room_depth*h; choffzE = 2*room_depth*h;
update_entities(dtime, hashtbl_find_opt(visited, player_chx+w, player_chy+h)); update_entities(dtime, hashtbl_find_opt(visited, player_chx+w, player_chy+h));
} }
} }
@ -89,7 +192,7 @@ void update_nearby_entities(float dtime, int render_distance) {
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen(float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*1.5; ret->hz_angle += ((double)dtime)*1.5;
} }
@ -98,24 +201,24 @@ void speen(double x, double y, double z, double w, double h, double d, double hz
// metad3 = shot proj freq // metad3 = shot proj freq
// metad4 = shot proj time to live // metad4 = shot proj time to live
// metad5 = time left before shooting // metad5 = time left before shooting
void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen2(float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*ent->metad3; ret->hz_angle += ((double)dtime)*ent->metad3;
ent->metad5 -= (double)dtime; ent->metad5 -= (double)dtime;
if(ent->metad5 <= 0.0) { if(ent->metad5 <= 0.0) {
ent->metad5 = ent->metad3; ent->metad5 = ent->metad3;
double dx = (x+w/2 - (camx-choffx)); double dx = (ret->x+ret->w/2 - (camx-choffxE));
double dy = (y+h/2 - (camy)); double dy = (ret->y+ret->h/2 - (camy));
double dz = (z+d/2 - (camz-choffz)); double dz = (ret->z+ret->d/2 - (camz-choffzE));
double total = sqrt(dx*dx + dy*dy + dz*dz); double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = ent->metad2*dx/total; dx = ent->metad2*dx/total;
dy = ent->metad2*dy/total; dy = ent->metad2*dy/total;
dz = ent->metad2*dz/total; dz = ent->metad2*dz/total;
appendProj(x+w/2+choffx, y+h/2, z+d/2+choffz, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, ent->metad4); appendProj(ret->x+ret->w/2+choffxE, ret->y+ret->h/2, ret->z+ret->d/2+choffzE, 0.1, 0.1, 0.1, -dx, -dy, -dz, 0.0, 0.0, 0.0, 255, 0, 0, 10, ent->metad4);
} }
if(ent->metad1 != 0.0) { if(ent->metad1 != 0.0) {
double dx = (x+w/2 - (camx-choffx)); double dx = (ret->x+ret->w/2 - (camx-choffxE));
double dy = (y+h/2 - (camy)); double dy = (ret->y+ret->h/2 - (camy));
double dz = (z+d/2 - (camz-choffz)); double dz = (ret->z+ret->d/2 - (camz-choffzE));
double total = sqrt(dx*dx + dy*dy + dz*dz); double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = ent->metad1*dx/total; dx = ent->metad1*dx/total;
dy = ent->metad1*dy/total; dy = ent->metad1*dy/total;
@ -143,7 +246,7 @@ void speen2(double x, double y, double z, double w, double h, double d, double h
// metad9 = remaining // metad9 = remaining
// metai{1,2,3} = 1000*proj_size_{x,y,z} // metai{1,2,3} = 1000*proj_size_{x,y,z}
// metai4 = dmg // metai4 = dmg
void gunning(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void gunning(float dtime, entity* ent, cube_0* ret) {
ent->metad9 -= (double)dtime; ent->metad9 -= (double)dtime;
if(ent->metad9 <= 0.0) { if(ent->metad9 <= 0.0) {
ent->metad9 = ent->metad7; ent->metad9 = ent->metad7;
@ -151,7 +254,7 @@ void gunning(double x, double y, double z, double w, double h, double d, double
double psizey = (double)(ent->metai2)/1000.0; double psizey = (double)(ent->metai2)/1000.0;
double psizez = (double)(ent->metai3)/1000.0; double psizez = (double)(ent->metai3)/1000.0;
appendProj( appendProj(
x+w/2.0-psizex/2.0, y+h/2.0-psizey/2.0, z+d/2.0-psizez/2.0, ret->x+ret->w/2.0-psizex/2.0, ret->y+ret->h/2.0-psizey/2.0, ret->z+ret->d/2.0-psizez/2.0,
psizex, psizey, psizez, psizex, psizey, psizez,
ent->metad1, ent->metad2, ent->metad3, ent->metad1, ent->metad2, ent->metad3,
ent->metad4, ent->metad5, ent->metad6, ent->metad4, ent->metad5, ent->metad6,
@ -161,7 +264,7 @@ void gunning(double x, double y, double z, double w, double h, double d, double
} }
} }
void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void speen3(float dtime, entity* ent, cube_0* ret) {
ret->vt_angle += ((double)dtime)*2.5; ret->vt_angle += ((double)dtime)*2.5;
} }
@ -170,7 +273,7 @@ void speen3(double x, double y, double z, double w, double h, double d, double h
// metai1 = frequency multiplier // metai1 = frequency multiplier
// metai2 = frequency divider // metai2 = frequency divider
// metai3 = phase // metai3 = phase
void moving_xyz(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void moving_xyz(float dtime, entity* ent, cube_0* ret) {
bool canMove = (ent->metai5 == -1) || xor(buttonSwitch[ent->metai5], (bool)ent->metai4); bool canMove = (ent->metai5 == -1) || xor(buttonSwitch[ent->metai5], (bool)ent->metai4);
if(canMove) { if(canMove) {
ret->x = ent->metad1 + ent->metad4*cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0)); ret->x = ent->metad1 + ent->metad4*cos((double)(ent->metai1*sim_time/ent->metai2 + ent->metai3*3.14159/180.0));
@ -183,10 +286,10 @@ void moving_xyz(double x, double y, double z, double w, double h, double d, doub
// metad{1,2,3} = og pos // metad{1,2,3} = og pos
// metad{4,5,6} = speed // metad{4,5,6} = speed
// metad{7,8,9} = max_delta // metad{7,8,9} = max_delta
// metai1 = x_side (+/- 1) // metai1 = ret->x_side (+/- 1)
// metai2 = y_side (+/- 1) // metai2 = ret->y_side (+/- 1)
// metai3 = z_side (+/- 1) // metai3 = ret->z_side (+/- 1)
void moving_xyz_line(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void moving_xyz_line(float dtime, entity* ent, cube_0* ret) {
bool canMove = (ent->metai5 == -1) || xor(buttonSwitch[ent->metai5], (bool)ent->metai4); bool canMove = (ent->metai5 == -1) || xor(buttonSwitch[ent->metai5], (bool)ent->metai4);
if(canMove) { if(canMove) {
ret->x += (ent->metai1)*ent->metad4*dtime; ret->x += (ent->metai1)*ent->metad4*dtime;
@ -218,7 +321,7 @@ void moving_xyz_line(double x, double y, double z, double w, double h, double d,
// metai2 = price // metai2 = price
// metai3 = doPay // metai3 = doPay
// metach1 = text (stored here to free() easily) // metach1 = text (stored here to free() easily)
void subtitle_text_box(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void subtitle_text_box(float dtime, entity* ent, cube_0* ret) {
if(distance_pt_cube_0_3d_infinite(camx+player_chx*room_width*2, camy, camz+player_chy*room_depth*2, ret) <= 1.5) { if(distance_pt_cube_0_3d_infinite(camx+player_chx*room_width*2, camy, camz+player_chy*room_depth*2, ret) <= 1.5) {
gl_drawString(fShader, ent->metach1, 0.0f, -0.7f, 0.03f, ret->red, ret->green, ret->blue, 0.003f, 0); gl_drawString(fShader, ent->metach1, 0.0f, -0.7f, 0.03f, ret->red, ret->green, ret->blue, 0.003f, 0);
} }
@ -228,7 +331,7 @@ void subtitle_text_box(double x, double y, double z, double w, double h, double
// metad2 = time OFF // metad2 = time OFF
// metad3 = current time left // metad3 = current time left
// metai1 = 0 if OFF, 1 if ON // metai1 = 0 if OFF, 1 if ON
void beating_block(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void beating_block(float dtime, entity* ent, cube_0* ret) {
ent->metad3 -= dtime; ent->metad3 -= dtime;
if(ent->metad3 <= 0.0) { if(ent->metad3 <= 0.0) {
if(ent->metai1) { if(ent->metai1) {
@ -242,7 +345,7 @@ void beating_block(double x, double y, double z, double w, double h, double d, d
// metad1 = hz_speed // metad1 = hz_speed
// metad2 = vt_speed // metad2 = vt_speed
void spinning_platform(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void spinning_platform(float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ent->metad1*dtime; ret->hz_angle += ent->metad1*dtime;
ret->vt_angle += ent->metad2*dtime; ret->vt_angle += ent->metad2*dtime;
} }
@ -250,7 +353,7 @@ void spinning_platform(double x, double y, double z, double w, double h, double
// metai1 = default state // metai1 = default state
// metad1 = remaining time // metad1 = remaining time
// metad2 = activation time // metad2 = activation time
void math_block(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void math_block(float dtime, entity* ent, cube_0* ret) {
if(ent->metad1 > -0.9) { if(ent->metad1 > -0.9) {
ent->metad1 = maxd(ent->metad1 - (double)dtime, 0.0); ent->metad1 = maxd(ent->metad1 - (double)dtime, 0.0);
} }
@ -260,12 +363,12 @@ void math_block(double x, double y, double z, double w, double h, double d, doub
} }
} }
// metad1 = hz_speed (// to y) // metad1 = hz_speed (// to ret->y)
// metad2 = vt_speed (// to x) // metad2 = vt_speed (// to ret->x)
// metad7 = norm of HZ deviation // metad7 = norm of HZ deviation
// metad8 = norm of VT deviation // metad8 = norm of VT deviation
// metai1 = dps // metai1 = dps
void lava_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void lava_postStep(float dtime, entity* ent, cube_0* ret) {
ret->hz_angle += ((double)dtime)*ent->metad1; ret->hz_angle += ((double)dtime)*ent->metad1;
ret->vt_angle += ((double)dtime)*ent->metad2; ret->vt_angle += ((double)dtime)*ent->metad2;
@ -290,8 +393,8 @@ void lava_postStep(double x, double y, double z, double w, double h, double d, d
// metad3 = vz // metad3 = vz
// metad4 = friction // metad4 = friction
// metad5 = mass (kg) // metad5 = mass (kg)
void movableCrate_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void movableCrate_postStep(float dtime, entity* ent, cube_0* ret) {
if(y < -48) { if(ret->y < -48) {
*(ent->hitpoints) = 0; *(ent->hitpoints) = 0;
} else { } else {
//printf("(%lf %lf %lf)\n", ent->metad1, ent->metad2, ent->metad3); //printf("(%lf %lf %lf)\n", ent->metad1, ent->metad2, ent->metad3);
@ -335,7 +438,7 @@ void movableCrate_postStep(double x, double y, double z, double w, double h, dou
// metai1 = button freq // metai1 = button freq
// metai2 = is triggered (0/1) // metai2 = is triggered (0/1)
void movCrateButton_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void movCrateButton_postStep(float dtime, entity* ent, cube_0* ret) {
bool isIn = false; bool isIn = false;
for(int k = 0; k < current_room->ent_len*(1-isIn); k++) { for(int k = 0; k < current_room->ent_len*(1-isIn); k++) {
for(int d = 0; d < 8*(1-isIn); d++) { for(int d = 0; d < 8*(1-isIn); d++) {
@ -359,6 +462,59 @@ void movCrateButton_postStep(double x, double y, double z, double w, double h, d
} }
} }
// metad1 = speed
// metad2 = jump height
// metad3 = vertical speed
// metad4 = knockback on hit (for player) [speed]
// metad5 = knockback on hit (for entity) [speed]
// metai1 = damage
void enemy1_postStep(float dtime, entity* ent, cube_0* ret) {
double dx = (camx - choffxE)-ret->x-ret->w/2.0;
ent->metad3 -= gravity_factor*dtime;
double dz = (camz - choffzE)-ret->z-ret->d/2.0;
double norm = sqrt(dx*dx + dz*dz);
dx = ent->metad1*dx/norm;
dz = ent->metad1*dz/norm;
bool hasCollisionXZ = false;
bool hasCollisionY = false;
ret->x += dtime*dx;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret) || is_colliding_with_ent_sp2(ret, 10, 16) || distance_pt_cube_0_3d_infinite(camx, camy, camz, ret) <= min_dist) {
ret->x -= dtime*dx;
hasCollisionXZ = true;
}
ret->z += dtime*dz;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret) || is_colliding_with_ent_sp2(ret, 10, 16) || distance_pt_cube_0_3d_infinite(camx, camy, camz, ret) <= min_dist) {
ret->z -= dtime*dz;
hasCollisionXZ = true;
}
ret->y += dtime*ent->metad3;
if(is_colliding_with_map(ret) || is_colliding_with_tp(ret) || is_colliding_with_ent_sp2(ret, 10, 16) || distance_pt_cube_0_3d_infinite(camx, camy, camz, ret) <= min_dist) {
if(distance_pt_cube_0_3d_infinite(camx, camy, camz, ret) <= min_dist) {
hasCollisionXZ = true;
hasCollisionY = true;
}
ret->y -= dtime*ent->metad3;
hasCollisionY = hasCollisionY || (ent->metad3 <= 0.0);
ent->metad3 = 0.0;
}
if(hasCollisionXZ && hasCollisionY) {
ent->metad3 += sqrt(2.0*gravity_factor*ent->metad2);
//printf("%lf\n", ent->metad3);
}
if(ret->y <= -48.0) {
*(ent->hitpoints) = 0;
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------- //
// ---------------------------------------------------------------------------------------------------------------------------------------------------------- //
void movableCrate_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) { void movableCrate_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
//printf("(+%lf, +%lf)\n", camvx, camvz); //printf("(+%lf, +%lf)\n", camvx, camvz);
//entToVelCheck = ent; //entToVelCheck = ent;
@ -378,7 +534,7 @@ void active_math(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
} }
void spinning_translate(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) { void spinning_translate(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret) {
// ye // ret->ye
} }
// metai1 = button freq (same for blocks) // metai1 = button freq (same for blocks)
@ -444,10 +600,10 @@ void translatePlayerLine(float dtime, int* hp, int* dmg, entity* ent, cube_0* re
} }
} }
void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret) { void go_to_player(float dtime, entity* ent, cube_0* ret) {
double dx = (x+w/2 - (camx-choffx)); double dx = (ret->x+ret->w/2 - (camx-choffxE));
double dy = (y+h/2 - (camy)); double dy = (ret->y+ret->h/2 - (camy));
double dz = (z+d/2 - (camz-choffz)); double dz = (ret->z+ret->d/2 - (camz-choffzE));
double total = sqrt(dx*dx + dy*dy + dz*dz); double total = sqrt(dx*dx + dy*dy + dz*dz);
dx = dx/total; dx = dx/total;
dy = dy/total; dy = dy/total;

View File

@ -11,20 +11,23 @@ void update_entity(entity* ent, float dtime);
void update_entities(float dtime, room* rtd); void update_entities(float dtime, room* rtd);
void update_nearby_entities(float dtime, int render_distance); void update_nearby_entities(float dtime, int render_distance);
void speen(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void update_entity_chunk();
void gunning(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret);
void speen2(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void speen(float dtime, entity* ent, cube_0* ret);
void speen3(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void gunning(float dtime, entity* ent, cube_0* ret);
void moving_xyz(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void speen2(float dtime, entity* ent, cube_0* ret);
void moving_xyz_line(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void speen3(float dtime, entity* ent, cube_0* ret);
void go_to_player(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void moving_xyz(float dtime, entity* ent, cube_0* ret);
void subtitle_text_box(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void moving_xyz_line(float dtime, entity* ent, cube_0* ret);
void beating_block(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void go_to_player(float dtime, entity* ent, cube_0* ret);
void spinning_platform(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void subtitle_text_box(float dtime, entity* ent, cube_0* ret);
void math_block(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void beating_block(float dtime, entity* ent, cube_0* ret);
void lava_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void spinning_platform(float dtime, entity* ent, cube_0* ret);
void movableCrate_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void math_block(float dtime, entity* ent, cube_0* ret);
void movCrateButton_postStep(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void lava_postStep(float dtime, entity* ent, cube_0* ret);
void movableCrate_postStep(float dtime, entity* ent, cube_0* ret);
void movCrateButton_postStep(float dtime, entity* ent, cube_0* ret);
void enemy1_postStep(float dtime, entity* ent, cube_0* ret);
void movableCrate_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret); void movableCrate_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
void lava_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret); void lava_onHit(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);

View File

@ -169,6 +169,14 @@ void init_ent_generator(int n) {
hashtbl_entities[15].updatePos = &gunning; hashtbl_entities[15].updatePos = &gunning;
hashtbl_entities[15].onHit = NULL; hashtbl_entities[15].onHit = NULL;
hashtbl_entities[15].onDeath = NULL; hashtbl_entities[15].onDeath = NULL;
hashtbl_entities[16].id = 16;
hashtbl_entities[16].tex = 14;
hashtbl_entities[16].tex2 = 14;
hashtbl_entities[16].name = "ennemy1";
hashtbl_entities[16].updatePos = &enemy1_postStep;
hashtbl_entities[16].onHit = NULL;
hashtbl_entities[16].onDeath = NULL;
} }
fct_entry* get_entry(int k0) { fct_entry* get_entry(int k0) {
@ -368,7 +376,7 @@ void parse_one_room(int id, char* filename) {
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 = maxd(nent, 64); int nmemlen = max(nent, 64);
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;
@ -756,6 +764,20 @@ void parse_one_room(int id, char* filename) {
pool[id].area->ents[k]->metai2 = (int)(psizey*1000.0); pool[id].area->ents[k]->metai2 = (int)(psizey*1000.0);
pool[id].area->ents[k]->metai3 = (int)(psizez*1000.0); pool[id].area->ents[k]->metai3 = (int)(psizez*1000.0);
pool[id].area->ents[k]->metai4 = dmg; pool[id].area->ents[k]->metai4 = dmg;
} else if(entry->id == 16) {
// entity1
double speed = read_float(ptr);
double height = read_float(ptr);
int damage = read_int(ptr);
double kb_player = read_float(ptr);
double kb_entity = read_float(ptr);
pool[id].area->ents[k]->metad1 = speed;
pool[id].area->ents[k]->metad2 = height;
pool[id].area->ents[k]->metad3 = 0.0;
pool[id].area->ents[k]->metad4 = kb_player;
pool[id].area->ents[k]->metad5 = kb_entity;
pool[id].area->ents[k]->metai1 = damage;
} else { } else {
pool[id].area->ents[k]->metai3 = 0; pool[id].area->ents[k]->metai3 = 0;
} }
@ -843,37 +865,6 @@ void parse_rooms(int n_rooms, char* folder) {
current_room = hashtbl_find_opt(visited, player_chx, player_chy); current_room = hashtbl_find_opt(visited, player_chx, player_chy);
} }
// has to be a multiple of both room_width and room_depth
int divider = 4;
// unused
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_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++) {
copy_cube(r->map[k], newMap[k]);
free(r->map[k]);
}
free(r->map);
// use whatever noise funtcion here
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) {
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;
}
}
r->map = newMap;
r->map_size += rsize;
}
void generate_nearby_chunks(int render_dist) { void generate_nearby_chunks(int render_dist) {
for(int w = -render_dist; w <= render_dist; w++) { for(int w = -render_dist; w <= render_dist; w++) {
for(int h = -render_dist; h <= render_dist; h++) { for(int h = -render_dist; h <= render_dist; h++) {

View File

@ -9,7 +9,7 @@ typedef struct entry {
typedef struct fct_entry { typedef struct fct_entry {
int id; int id;
char* name; char* name;
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, entity* ent, cube_0* ret); void (*updatePos)(float dtime, entity* ent, cube_0* ret);
// act as velocity function // act as velocity function
void (*onHit)(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret); void (*onHit)(float dtime, int* hp, int* dmg, entity* ent, cube_0* ret);
// triggers when object is hit // triggers when object is hit

View File

@ -697,6 +697,14 @@ int main_alt() {
vec3 md_dir0; vec3 md_dir0;
vec3 md_direction; vec3 md_direction;
struct timeval st_fps;
struct timeval et_fps;
gettimeofday(&st_fps, NULL);
gettimeofday(&et_fps, NULL);
int count_fps = 0;
while(!glfwWindowShouldClose(window) && player_hp > 0) { while(!glfwWindowShouldClose(window) && player_hp > 0) {
gettimeofday(&ogn, NULL); gettimeofday(&ogn, NULL);
// input // input
@ -706,7 +714,7 @@ int main_alt() {
triCount = 0; triCount = 0;
fflush(stdout); fflush(stdout);
generate_nearby_chunks(1); generate_nearby_chunks(render_distance);
fflush(stdout); fflush(stdout);
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
@ -717,7 +725,7 @@ int main_alt() {
//printf("1\n"); fflush(stdout); //printf("1\n"); fflush(stdout);
gl_initRender(shaderProgram, shaderProgram, VAO, VBO); gl_initRender(shaderProgram, shaderProgram, VAO, VBO);
//printf("2\n"); fflush(stdout); //printf("2\n"); fflush(stdout);
gl_renderNearbyChunks(1); gl_renderNearbyChunks(render_distance);
//printf("3\n"); fflush(stdout); //printf("3\n"); fflush(stdout);
gl_renderProj(); gl_renderProj();
//printf("4\n"); fflush(stdout); //printf("4\n"); fflush(stdout);
@ -745,6 +753,7 @@ int main_alt() {
} }
teleport_on_edge(); teleport_on_edge();
update_nearby_entities(delta*time_dilation, 1); update_nearby_entities(delta*time_dilation, 1);
update_entity_chunk();
updateProj(delta*time_dilation); updateProj(delta*time_dilation);
update_buttons(delta*time_dilation); update_buttons(delta*time_dilation);
} else { } else {
@ -788,8 +797,11 @@ int main_alt() {
} }
lastDmg = player_hp; lastDmg = player_hp;
delta = (float)(interval_s + maxf(0.0f, -interval_s+(calc_T))); delta = (float)(interval_s + maxf(0.0f, -interval_s+(calc_T)));
if(count%(fps/10)==0) { if(count_fps >= fps/5) {
real_T = (float)((fnn.tv_sec-ogn.tv_sec + (fnn.tv_usec-ogn.tv_usec)/1000000.0f)); gettimeofday(&et_fps, NULL);
real_T = (float)((et_fps.tv_sec-st_fps.tv_sec + (et_fps.tv_usec-st_fps.tv_usec)/1000000.0f)/count_fps);
count_fps = 0;
gettimeofday(&st_fps, NULL);
} }
incr = 0.0f; incr = 0.0f;
@ -798,6 +810,7 @@ int main_alt() {
glfwPollEvents(); glfwPollEvents();
count++; count++;
count_fps++;
} }
printf("| 1/5 |\n"); fflush(stdout); printf("| 1/5 |\n"); fflush(stdout);

View File

@ -28,6 +28,7 @@ double vtmult = 2.7;
double min_dist = 0.4; double min_dist = 0.4;
double friction = 0.3; double friction = 0.3;
double gravity_factor = 26.0; double gravity_factor = 26.0;
int render_distance = 1;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
const double blockRestitution = 0.2; const double blockRestitution = 0.2;

View File

@ -34,7 +34,7 @@ typedef struct entity {
int entity_type; int entity_type;
cube_0* pos; cube_0* pos;
// act as velocity function // act as velocity function
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, float dtime, struct entity* ent, cube_0* ret); void (*updatePos)(float dtime, struct entity* ent, cube_0* ret);
// triggers when object is hit // triggers when object is hit
void (*onHit)(float dtime, int* hp, int* dmg, struct entity* ent, cube_0* ret); void (*onHit)(float dtime, int* hp, int* dmg, struct entity* ent, cube_0* ret);
@ -152,6 +152,7 @@ extern double fov;
extern double speed; extern double speed;
extern double creative_speed; extern double creative_speed;
extern double min_dist; extern double min_dist;
extern int render_distance;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
extern float vertices[180]; extern float vertices[180];
@ -189,6 +190,9 @@ extern bool is_one_room;
extern unsigned int textures[32]; extern unsigned int textures[32];
extern double choffx;
extern double choffz;
extern float dmgCD; extern float dmgCD;
extern int lastDmg; extern int lastDmg;