Compare commits

...

2 Commits

Author SHA1 Message Date
Alexandre 639449b4ae added money 2025-01-11 22:10:26 +01:00
Alexandre f3abc7266e added spinning entities + fixed segFault on sort entities 2025-01-11 18:05:33 +01:00
17 changed files with 190 additions and 26 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.

View File

@ -339,8 +339,8 @@ int surfaceDrawOrder(double x0, double y0, double z0, cube_0 cb) {
double zry = z*cos(cb.hz_angle) - x*sin(cb.hz_angle) ; double zry = z*cos(cb.hz_angle) - x*sin(cb.hz_angle) ;
// rotate (x) // rotate (x)
double xrx = xry ; double xrx = xry ;
double yrx = yry*cos(cb.vt_angle) - zry*sin(cb.vt_angle) ; double yrx = yry*cos(cb.vt_angle) + zry*sin(cb.vt_angle) ;
double zrx = zry*cos(cb.vt_angle) + yry*sin(cb.vt_angle) ; double zrx = zry*cos(cb.vt_angle) - yry*sin(cb.vt_angle) ;
// cube is centered and aligned // cube is centered and aligned
int id = 0 ; int id = 0 ;
if(xrx > cb.w/2.0) { if(xrx > cb.w/2.0) {
@ -599,6 +599,12 @@ void swap_tp(teleporter* arr, int i, int j) {
arr[j] = temp; arr[j] = temp;
} }
void swap_ent(entity* arr, int i, int j) {
entity temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void insertionSort_cb(cube_0* arr, int len) { void insertionSort_cb(cube_0* arr, int len) {
for(int k = 0; k < len; k++) { for(int k = 0; k < len; k++) {
int j = k-1 ; int j = k-1 ;
@ -627,12 +633,30 @@ void insertionSort_tp(teleporter* arr, int len) {
} }
} }
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))) {
swap_ent(arr, j, j+1);
j -= 1;
} else {
j = -1;
}
}
}
}
void drawCurrentRoom(SDL_Renderer* renderer) { void drawCurrentRoom(SDL_Renderer* renderer) {
insertionSort_cb(current_room->map, current_room->map_size); insertionSort_cb(current_room->map, current_room->map_size);
insertionSort_tp(current_room->tps, current_room->tps_size); insertionSort_tp(current_room->tps, current_room->tps_size);
insertionSort_ent(current_room->ents, current_room->ent_len);
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
drawFullCube(renderer, current_room->map[k]); drawFullCube(renderer, current_room->map[k]);
} }
for(int k = 0; k < current_room->ent_len; k++) {
drawFullCube(renderer, *(current_room->ents[k].pos));
}
for(int k = 0; k < current_room->tps_size; k++) { for(int k = 0; k < current_room->tps_size; k++) {
drawFullCube(renderer, current_room->tps[k].hitbox); drawFullCube(renderer, current_room->tps[k].hitbox);
} }

View File

@ -42,6 +42,34 @@ bool is_colliding_with_tp(cube_0 cb) {
// ------------------------------------------------------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------------------------------------------------------ //
void update_entity(entity* ent) { 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, ent->pos); (*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->pos);
}
void update_entities(float dtime) {
for(int k = 0; k < current_room->ent_len; k++) {
if(current_room->ents[k].updatePos != NULL) {
//printf("e\n");
update_entity(&(current_room->ents[k]), 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;
}
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;
}
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret) {
if(ret->red == 255) {
ret->red = 0;
ret->green = 192;
ret->blue = 0;
coins += 1;
}
} }

View File

@ -1,5 +1,16 @@
#ifndef ENTITIES_H #ifndef ENTITIES_H
#define ENTITIES_H #define ENTITIES_H
bool is_colliding_with_map(cube_0 cb);
bool is_colliding_with_tp(cube_0 cb);
void update_entity(entity* ent, float dtime);
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);
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);
void detectHit(float dtime, int* hp, int* dmg, cube_0* ret);
#endif #endif

View File

@ -14,6 +14,7 @@
#include "hash.h" #include "hash.h"
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "entities.h"
#include "generation.h" #include "generation.h"
hashtbl visited ; hashtbl visited ;
@ -26,6 +27,8 @@ entry* pool ;
int pool_size ; int pool_size ;
int total_weight ; int total_weight ;
int coins ;
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 ;
@ -52,6 +55,21 @@ 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_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_len);
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].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,
(*(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->tps_size = src->tps_size; dest->tps_size = src->tps_size;
} }
@ -78,6 +96,16 @@ 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[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->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->ent_len = 1 ;
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].damage = 0 ;
new->ents[0].hitpoints = 900 ;
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);
} }
@ -88,25 +116,31 @@ void init_hashtbl() {
player_chx = 0 ; player_chx = 0 ;
player_chy = 0 ; player_chy = 0 ;
total_weight = 0 ; total_weight = 0 ;
coins = 0 ;
} }
void get_number_blocks(int* ret_cubes, int* ret_tps, 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 ;
bool in_blocks = true ; 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 = true ; in_blocks = 0 ;
} else if(c == 'T') { } else if(c == 'T') {
in_blocks = false ; in_blocks = 1 ;
} else if(c == 'E') {
in_blocks = 2 ;
} else if(c == '[') { } else if(c == '[') {
if(in_blocks) { if(in_blocks == 0) {
*ret_cubes += 1 ; *ret_cubes += 1 ;
} else { } else if(in_blocks == 1) {
*ret_tps += 1 ; *ret_tps += 1 ;
} else {
*ret_ent += 1;
} }
} }
c = fgetc(ptr); c = fgetc(ptr);
@ -170,15 +204,18 @@ void parse_one_room(int id, char* filename) {
pool[id].area = malloc(sizeof(room)); pool[id].area = malloc(sizeof(room));
int ncubes ; int ncubes ;
int ntps ; int ntps ;
int nent ;
FILE* ptr2 = fopen(filename, "r") ; FILE* ptr2 = fopen(filename, "r") ;
get_number_blocks(&ncubes, &ntps, ptr2); get_number_blocks(&ncubes, &ntps, &nent, ptr2);
//printf("(%d, %d)\n", ncubes, ntps); printf("(%d, %d, %d)\n", ncubes, ntps, nent);
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;
pool[id].area->tps = malloc(sizeof(teleporter)*ntps); pool[id].area->tps = malloc(sizeof(teleporter)*ntps);
pool[id].area->tps_size = ntps; pool[id].area->tps_size = ntps;
pool[id].area->ents = malloc(sizeof(entity)*nent);
pool[id].area->ent_len = nent ;
for(int k = 0; k < ncubes; k++) { for(int k = 0; k < ncubes; k++) {
align_to(ptr, '['); align_to(ptr, '[');
@ -216,6 +253,32 @@ void parse_one_room(int id, char* filename) {
//printf("\n"); //printf("\n");
} }
for(int k = 0; k < nent; k++) {
align_to(ptr, '[');
double cx = read_float(ptr);
double cy = read_float(ptr);
double cz = read_float(ptr);
double cw = read_float(ptr);
double ch = read_float(ptr);
double cd = read_float(ptr);
double chz = read_float(ptr);
double cvt = read_float(ptr);
int red = read_int(ptr, true);
int green = read_int(ptr, true);
int blue = read_int(ptr, true);
int hp = read_int(ptr, true);
int dmg = read_int(ptr, true);
pool[id].area->ents[k].pos = malloc(sizeof(cube_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 = *speen ;
pool[id].area->ents[k].onHit = *detectHit ;
pool[id].area->ents[k].onDeath = NULL ;
//printf("\n");
}
// debug // debug
for(int k = 0; k < ncubes; k++) { for(int k = 0; k < ncubes; k++) {
printf("(%lf, %lf, %lf), (%lf, %lf, %lf), (%lf, %lf), (%d, %d, %d)\n", printf("(%lf, %lf, %lf), (%lf, %lf, %lf), (%lf, %lf), (%d, %d, %d)\n",

View File

@ -11,7 +11,7 @@ void build_starting_chunk(int chx, int chy) ;
void init_hashtbl() ; void init_hashtbl() ;
void get_number_blocks(int* ret_cubes, int* ret_tps, FILE* ptr) ; void get_number_blocks(int* ret_cubes, int* ret_tps, int* ret_ent, FILE* ptr) ;
void align_to(FILE* ptr, char ch) ; void align_to(FILE* ptr, char ch) ;
int read_int(FILE* ptr, bool print) ; int read_int(FILE* ptr, bool print) ;
double read_float(FILE* ptr) ; double read_float(FILE* ptr) ;

View File

@ -60,14 +60,23 @@ int main(int argc, char** argv) {
sim_time = 0.0 ; sim_time = 0.0 ;
clock_t origin = clock(); clock_t origin = clock();
clock_t finish = clock(); clock_t finish = clock();
clock_t entstart = clock();
clock_t entend = clock();
float delta; float delta;
while(true) { while(true) {
resetRenderer(rend) ; resetRenderer(rend) ;
origin = clock(); origin = clock();
SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ; SDL_SetRenderDrawColor(rend, 255, 255, 255, SDL_ALPHA_OPAQUE) ;
playerActions() ; entend = clock();
playerActions(((float)entend - (float)entstart)/CLOCKS_PER_SEC) ;
generate_nearby_chunks(1); generate_nearby_chunks(1);
entend = clock();
update_entities(((float)entend - (float)entstart)/CLOCKS_PER_SEC);
entstart = clock();
drawCurrentRoom(rend); drawCurrentRoom(rend);
drawData(rend) ; drawData(rend) ;
finish = clock(); finish = clock();
@ -75,6 +84,7 @@ int main(int argc, char** argv) {
delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC; delta = ((float)finish - (float)origin)/CLOCKS_PER_SEC;
drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0); drawNumberToRenderer(rend, digits, (int)(1.0f/delta), 720, 60, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0); drawNumberToRenderer(rend, digits, (int)(10*sim_time), 720, 110, 75/2, 105/2, 0);
drawNumberToRenderer(rend, digits, coins, 1500/2-55, 1000 - 70, 75/3, 105/3, 0);
updateRenderer(rend) ; updateRenderer(rend) ;
sim_time += delta + intervalf ; sim_time += delta + intervalf ;
usleep(interval) ; usleep(interval) ;

View File

@ -14,13 +14,14 @@
#include "hash.h" #include "hash.h"
#include "structure.h" #include "structure.h"
#include "base.h" #include "base.h"
#include "entities.h"
#include "move.h" #include "move.h"
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double sensitivity = 0.22 ; double sensitivity = 0.22 ;
double fov = 90.0 ; double fov = 90.0 ;
double speed = 1.0 ; double speed = 1.0 ;
double min_dist = 0.5 ; double min_dist = 0.7 ;
// ---------------------------------------------------------------------------------------------------- // // ---------------------------------------------------------------------------------------------------- //
double camx ; double camx ;
@ -56,7 +57,7 @@ void set_player_coords(int old_chx, int old_chy) {
} }
} }
bool is_colliding() { bool is_colliding(float dtime) {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {
double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ; double dist = distance_pt_cube_0_3d(camx, camy, camz, current_room->map[k]) ;
if(dist <= min_dist) { if(dist <= min_dist) {
@ -75,10 +76,19 @@ bool is_colliding() {
return true ; return true ;
} }
} }
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)) ;
if(dist <= min_dist) {
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)));
}
return true ;
}
}
return false ; return false ;
} }
void playerActions() { void playerActions(float dtime) {
SDL_Event event; SDL_Event event;
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
@ -107,7 +117,7 @@ void playerActions() {
camz += speed*cos(rot_hz)*cos(rot_vt)/10; camz += speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= speed*sin(rot_hz)*cos(rot_vt)/10; camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
camy += speed*sin(rot_vt)/10; camy += speed*sin(rot_vt)/10;
if(is_colliding()) { if(is_colliding(dtime)) {
camz -= speed*cos(rot_hz)*cos(rot_vt)/10; camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
camx += speed*sin(rot_hz)*cos(rot_vt)/10; camx += speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= speed*sin(rot_vt)/10; camy -= speed*sin(rot_vt)/10;
@ -119,7 +129,7 @@ void playerActions() {
for(int k = 0; k < 10; k++) { for(int k = 0; k < 10; k++) {
camx -= speed*cos(rot_hz)/10; camx -= speed*cos(rot_hz)/10;
camz -= speed*sin(rot_hz)/10; camz -= speed*sin(rot_hz)/10;
if(is_colliding()) { if(is_colliding(dtime)) {
camx += speed*cos(rot_hz)/10; camx += speed*cos(rot_hz)/10;
camz += speed*sin(rot_hz)/10; camz += speed*sin(rot_hz)/10;
k=11; k=11;
@ -131,7 +141,7 @@ void playerActions() {
camz -= speed*cos(rot_hz)*cos(rot_vt)/10; camz -= speed*cos(rot_hz)*cos(rot_vt)/10;
camx += speed*sin(rot_hz)*cos(rot_vt)/10; camx += speed*sin(rot_hz)*cos(rot_vt)/10;
camy -= speed*sin(rot_vt)/10; camy -= speed*sin(rot_vt)/10;
if(is_colliding()) { if(is_colliding(dtime)) {
camz += speed*cos(rot_hz)*cos(rot_vt)/10; camz += speed*cos(rot_hz)*cos(rot_vt)/10;
camx -= speed*sin(rot_hz)*cos(rot_vt)/10; camx -= speed*sin(rot_hz)*cos(rot_vt)/10;
camy += speed*sin(rot_vt)/10; camy += speed*sin(rot_vt)/10;
@ -143,7 +153,7 @@ void playerActions() {
for(int k = 0; k < 10; k++) { for(int k = 0; k < 10; k++) {
camx += speed*cos(rot_hz)/10; camx += speed*cos(rot_hz)/10;
camz += speed*sin(rot_hz)/10; camz += speed*sin(rot_hz)/10;
if(is_colliding()) { if(is_colliding(dtime)) {
camx -= speed*cos(rot_hz)/10; camx -= speed*cos(rot_hz)/10;
camz -= speed*sin(rot_hz)/10; camz -= speed*sin(rot_hz)/10;
k=11; k=11;

View File

@ -2,7 +2,7 @@
#define MOVE_H #define MOVE_H
void init_csts(); void init_csts();
bool is_colliding(); bool is_colliding(float dtime);
void playerActions(); void playerActions(float dtime);
#endif #endif

View File

@ -31,9 +31,17 @@ typedef struct teleporter {
typedef struct entity { typedef struct entity {
cube_0* pos ; cube_0* pos ;
void (*updatePos)(double x, double y, double z, double w, double h, double d, double hz_angle, double vt_angle, cube_0* ret) ; // 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, cube_0* ret) ;
// act as velocity function
void (*onHit)(float dtime, int* hp, int* dmg, cube_0* ret) ;
// triggers when object is hit
void (*onDeath)(float dtime) ;
// triggers when <hitpoints> goes negative
int damage ; int damage ;
int hitpoints ; int* hitpoints ;
} entity ; } entity ;
struct room { struct room {
@ -44,6 +52,8 @@ struct room {
int map_size ; int map_size ;
teleporter* tps ; teleporter* tps ;
int tps_size ; int tps_size ;
entity* ents ;
int ent_len ;
} ; } ;
typedef struct room room ; typedef struct room room ;
@ -92,4 +102,6 @@ extern int player_chy ;
extern int* drawOrder ; extern int* drawOrder ;
extern int coins;
#endif #endif

View File

@ -13,7 +13,13 @@ Teleporters :
[9.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 0, 255, 0; 0, 1] [9.0, 1.0, -5.0, 1.0, 2.0, 10.0, 0.0, 0.0, 0, 255, 0; 0, 1]
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] [-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]
Weight : Weight :
30 20
$ $