levels can now be saved/loaded with external files

This commit is contained in:
Alexandre 2024-08-21 13:11:04 +02:00
parent 996347d70a
commit 7b65934d00
9 changed files with 228 additions and 3 deletions

Binary file not shown.

14
level.csv Normal file
View File

@ -0,0 +1,14 @@
1,0,0,0,0[2,]
2,1,0,-100,1[3,]
3,1,0,-200,1[4,13,]
4,1,0,-300,1[5,]
5,2,100,-300,0[6,]
6,1,200,-300,1[7,]
7,10,200,-400,0[8,]
8,2,100,-400,0[9,]
9,1,0,-400,1[10,11,]
10,3,0,-500,0[]
11,3,-100,-400,0[12,]
12,2,-100,-300,0[]
13,1,100,-200,1[14,]
14,2,200,-200,0[6,]
1 1,0,0,0,0[2,]
2 2,1,0,-100,1[3,]
3 3,1,0,-200,1[4,13,]
4 4,1,0,-300,1[5,]
5 5,2,100,-300,0[6,]
6 6,1,200,-300,1[7,]
7 7,10,200,-400,0[8,]
8 8,2,100,-400,0[9,]
9 9,1,0,-400,1[10,11,]
10 10,3,0,-500,0[]
11 11,3,-100,-400,0[12,]
12 12,2,-100,-300,0[]
13 13,1,100,-200,1[14,]
14 14,2,200,-200,0[6,]

View File

@ -37,6 +37,7 @@ imgs letters ;
void init_empty_board() {
start = malloc(sizeof(cell));
start->id = 0 ;
start->unique_id = 1 ;
start->coord_x = 0 ;
start->coord_y = 0 ;
start->star_allowed = false ;
@ -434,6 +435,10 @@ void destroy_everything() {
free_digits(digits);
free_digits(letters);
if(n_loaded != -1) {
free(all_loaded_cells);
};
printf("Destruction complete!...\n\n");
}

View File

@ -76,3 +76,43 @@ bool is_star_allowed(int ctype) {
bool is_hidden_block_allowed(int ctype) {
return ((CELLTYPE)ctype == BLUE || (CELLTYPE)ctype == RED) ;
}
int line_count(char* filename) {
FILE* ptr = fopen(filename, "r");
char c = 'd';
int n = 0 ;
while(c != EOF) {
if(c == '\n') {
n += 1;
};
c = fgetc(ptr);
};
fclose(ptr);
return (n+1);
}
int get_integer(FILE* ptr) {
char c = fgetc(ptr);
int res = 0 ;
int sign = 1;
if(c == '-') {
sign = -1;
c = fgetc(ptr);
};
while(is_an_integer(c)) {
res = 10*res + (int)c - 48;
c = fgetc(ptr);
};
return (res*sign);
}
void terminate_line(FILE* ptr) {
char c = fgetc(ptr);
while(c != '\n' && c != EOF) {
c = fgetc(ptr);
}
}

View File

@ -23,4 +23,10 @@ bool is_star_allowed(int ctype);
bool is_hidden_block_allowed(int ctype);
int line_count(char* filename);
int get_integer(FILE* ptr);
void terminate_line(FILE* ptr);
#endif

View File

@ -11,9 +11,11 @@ typedef enum CELLTYPE {
CHANCE_TIME,
BOO,
STAR,
BANK
BANK,
ITEM_SHOP
} CELLTYPE ;
static int n_cell_type = 10 ;
static int n_cell_type = 11 ;
// if you want to add another type, add it AT THE VERY END. otherwise it may corrupt existing save files
typedef enum ITEM {
DOUBLE_DICE,
@ -33,7 +35,8 @@ typedef enum ITEM {
static int n_items = 13 ;
typedef struct cell {
int id ; // mostly for parkour
int id ; // mostly for drawing
int unique_id ; // save/load
int coord_x ;
int coord_y ;
bool star_allowed ;
@ -110,4 +113,9 @@ static int tile_size = 50 ;
extern cell* current ;
extern int edit_cell_count ;
extern cell** all_loaded_cells ;
extern int n_loaded ;
#endif

View File

@ -138,6 +138,8 @@ long cell_colors(cell* cl) {
return (long)(255) + (long)(256)*(long)(255) + (long)(256*256)*(long)(32);
case BANK:
return (long)(255) + (long)(256)*(long)(255) + (long)(256*256)*(long)(32);
case ITEM_SHOP:
return (long)(220) + (long)(256)*(long)(220) + (long)(256*256)*(long)(220);
}
}

View File

@ -16,6 +16,11 @@
#include "display.h"
#include "edit.h"
int edit_cell_count = 2;
cell** all_loaded_cells;
int n_loaded = -1;
void link_cells(cell* parent, cell* child) {
if(parent != NULL) {
if(parent->next_1 == NULL) {
@ -93,6 +98,7 @@ cell* redirect_cell(cell* cl) {
void append_cell(int cell_type, int X, int Y, cell* parent) {
cell* new = malloc(sizeof(cell));
new->id = parent->id; // mostly for parkour
new->unique_id = edit_cell_count;
new->coord_x = X;
new->coord_y = Y;
new->star_allowed = is_star_allowed(cell_type);
@ -107,6 +113,8 @@ void append_cell(int cell_type, int X, int Y, cell* parent) {
new->next_4 = NULL;
char* meta = "none";
edit_cell_count += 1;
link_cells(parent, new);
cell_count[cell_type] += 1;
}
@ -329,6 +337,17 @@ void playerEditActions(bool* finished) {
*finished = true;
};
break;
case SDLK_s:
save("level.csv");
printf("Saved level\n");
break;
case SDLK_q:
load("level.csv");
printf("loaded level\n");
break;
case SDLK_e:
current->type = (CELLTYPE)(((int)current->type +1)%n_cell_type);
break;
}
}
}
@ -345,6 +364,7 @@ void draw_types(SDL_Renderer* renderer) {
drawStringToRenderer(renderer, letters, "boo", __width__ - 4 * 75/2, 400, 75/2, 105/2);
drawStringToRenderer(renderer, letters, "star", __width__ - 5 * 75/2, 450, 75/2, 105/2);
drawStringToRenderer(renderer, letters, "bank", __width__ - 5 * 75/2, 500, 75/2, 105/2);
drawStringToRenderer(renderer, letters, "shop", __width__ - 5 * 75/2, 550, 75/2, 105/2);
}
void draw_directions(SDL_Renderer* renderer) {
@ -385,3 +405,123 @@ void edit_main(SDL_Renderer* renderer) {
usleep(20000);
}
}
// save/load level
void reassign_id_cell(cell* cl, int* current, int common) {
if(cl != NULL && cl->id != 60000) {
cl->unique_id = *current;
*current += 1;
cl->id = 60000;
reassign_id_cell(cl->next_1, current, common);
reassign_id_cell(cl->next_2, current, common);
reassign_id_cell(cl->next_3, current, common);
reassign_id_cell(cl->next_4, current, common);
}
}
void reassign_ids() {
int p = 1 ;
reassign_id_cell(start, &p, start->id);
}
void write_cell(FILE* ptr, cell* cl, int global_id) {
if(cl != NULL && cl->id == global_id) {
printf("saving cell %d\n", cl->unique_id);
cl->id = global_id+1;
fprintf(ptr, "%d,%d,%d,%d,%d[", cl->unique_id, (int)cl->type, cl->coord_x, cl->coord_y, (int)cl->star_allowed);
printf("%d,%d,%d,%d,%d[", cl->unique_id, (int)cl->type, cl->coord_x, cl->coord_y, (int)cl->star_allowed);
if(cl->next_1 != NULL) {
fprintf(ptr, "%d,", cl->next_1->unique_id);
printf("%d,", cl->next_1->unique_id);
};
if(cl->next_2 != NULL) {
fprintf(ptr, "%d,", cl->next_2->unique_id);
printf("%d,", cl->next_2->unique_id);
};
if(cl->next_3 != NULL) {
fprintf(ptr, "%d,", cl->next_3->unique_id);
printf("%d,", cl->next_3->unique_id);
};
if(cl->next_4 != NULL) {
fprintf(ptr, "%d,", cl->next_4->unique_id);
printf("%d,", cl->next_4->unique_id);
};
fprintf(ptr, "]\n");
printf("]\n");
write_cell(ptr, cl->next_1, global_id);
write_cell(ptr, cl->next_2, global_id);
write_cell(ptr, cl->next_3, global_id);
write_cell(ptr, cl->next_4, global_id);
}
}
void save(char* filename) {
reassign_ids();
FILE* ptr = fopen(filename, "w");
write_cell(ptr, start, start->id);
fclose(ptr);
}
// fprintf(ptr, "%d,%d,%d,%d,%d[", cl->unique_id, (int)cl->type, cl->coord_x, cl->coord_y, (int)cl->star_allowed);
void load(char* filename) {
n_loaded = line_count(filename) -1;
all_loaded_cells = malloc(sizeof(cell*)*n_loaded);
all_loaded_cells[0] = start;
for(int i = 0; i < n_loaded; i++) {
if(i != 0) {
all_loaded_cells[i] = malloc(sizeof(cell));
};
all_loaded_cells[i]->next_1 = NULL;
all_loaded_cells[i]->next_2 = NULL;
all_loaded_cells[i]->next_3 = NULL;
all_loaded_cells[i]->next_4 = NULL;
all_loaded_cells[i]->prev_1 = NULL;
all_loaded_cells[i]->prev_2 = NULL;
all_loaded_cells[i]->prev_3 = NULL;
all_loaded_cells[i]->prev_4 = NULL;
};
FILE* ptr = fopen(filename, "r");
char burn;
printf("Loading %d lines...\n", n_loaded);
for(int i = 0; i < n_loaded; i++) {
printf("%d\n", i);
int unique = get_integer(ptr);
int a_type = get_integer(ptr);
int cx = get_integer(ptr);
int cy = get_integer(ptr);
int ok_star = get_integer(ptr);
//printf(" %d, %d, %d, %d, %d\n", unique, a_type, cx, cy, ok_star);
all_loaded_cells[i]->unique_id = unique;
all_loaded_cells[i]->type = (CELLTYPE)a_type;
all_loaded_cells[i]->coord_x = cx;
all_loaded_cells[i]->coord_y = cy;
all_loaded_cells[i]->star_allowed = (bool)ok_star;
int current_link = get_integer(ptr);
while(current_link > 0) {
link_cells(all_loaded_cells[i], all_loaded_cells[current_link-1]);
//printf(" linking %d <--> %d", i, current_link -1);
current_link = get_integer(ptr);
};
if(i != n_loaded -1) {
terminate_line(ptr);
}
};
current = start;
edit_cell_count = n_loaded;
fclose(ptr);
}

View File

@ -19,4 +19,14 @@ void draw_directions(SDL_Renderer* renderer);
void edit_main(SDL_Renderer* renderer);
void reassign_id_cell(cell* cl, int* current, int common);
void reassign_ids();
void write_cell(FILE* ptr, cell* cl, int global_id);
void save(char* filename);
void load(char* filename);
#endif