Revision 375
scoreboard logic for zombies finalized
proj/src/data/scoreboard.txt | ||
---|---|---|
1 |
06/01/20 15:01:27 - 6 - 38 |
|
2 |
06/01/20 13:01:24 - 0 - 0 |
|
3 |
06/01/20 16:01:54 - 0 - 0 |
|
0 | 4 |
proj/src/project/include/scoreboards.h | ||
---|---|---|
48 | 48 |
* @param path Path to file to be written |
49 | 49 |
*/ |
50 | 50 |
void (highscores_save)(const highscores_t *p, const char *path); |
51 |
|
|
51 | 52 |
/** |
53 |
* @brief Checks if score is higher than any of the top 3, and if so replaces the highscores. |
|
54 |
* @param p Pointer to highscores. |
|
55 |
* @param score New score obtained. |
|
56 |
* @param time_played Time played |
|
57 |
*/ |
|
58 |
void (check_new_score)(highscores_t *p, int score, int time_played); |
|
59 |
/** |
|
52 | 60 |
* @brief Destruct menu. |
53 | 61 |
* @param p Pointer to menu to destruct |
54 | 62 |
*/ |
proj/src/project/include/zombies.h | ||
---|---|---|
10 | 10 |
* @{ |
11 | 11 |
*/ |
12 | 12 |
|
13 |
#include "scoreboards.h" |
|
14 |
|
|
13 | 15 |
/** |
14 | 16 |
* @brief Zombies. |
15 | 17 |
* @return SUCCESS if operation was successful, other value otherwise |
16 | 18 |
*/ |
17 |
int (zombies)(void);
|
|
19 |
int (zombies)(highscores_t *zombies_highscore);
|
|
18 | 20 |
|
19 | 21 |
/** |
22 |
* @brief Zombies ranking. |
|
23 |
* @param zombies_highscore Highscores. |
|
24 |
*/ |
|
25 |
void (zombies_ranking)(highscores_t *zombie_highscore); |
|
26 |
|
|
27 |
/** |
|
20 | 28 |
* @} |
21 | 29 |
*/ |
22 | 30 |
|
proj/src/project/src/proj.c | ||
---|---|---|
41 | 41 |
#include "singleplayer.h" |
42 | 42 |
static int (multiplayer)(void); |
43 | 43 |
#include "chat.h" |
44 |
|
|
45 |
#include "scoreboards.h" |
|
44 | 46 |
int(proj_main_loop)(int argc, char *argv[]) { |
45 | 47 |
(void)argc; (void)argv; |
46 | 48 |
|
proj/src/project/src/proj_func.c | ||
---|---|---|
245 | 245 |
text_set_string(p->text, buffer); |
246 | 246 |
} |
247 | 247 |
void (text_timer_reset)(text_timer_t *p){ |
248 |
p->time = 0; |
|
248 | 249 |
text_set_string(p->text, "000s"); |
249 | 250 |
} |
250 | 251 |
void (text_timer_dtor)(text_timer_t *p){ |
proj/src/project/src/scoreboards.c | ||
---|---|---|
9 | 9 |
#include "rtc.h" |
10 | 10 |
#include "errors.h" |
11 | 11 |
|
12 |
#include <stdio.h> |
|
13 |
|
|
14 | 12 |
#define MAX_HIGHSCORES 3 /** @brief Number of scores saved */ |
15 | 13 |
|
16 | 14 |
struct score_info { |
... | ... | |
37 | 35 |
score_info_t *ret = (score_info_t*)malloc(sizeof(score_info_t)); |
38 | 36 |
if (ret == NULL) return NULL; |
39 | 37 |
uint8_t score_date[4]; |
40 |
if (rtc_read_date(score_date)) return NULL;
|
|
38 |
if (rtc_read_time(score_date)) return NULL;
|
|
41 | 39 |
ret->sec = score_date[0]; |
42 | 40 |
ret->min = score_date[1]; |
43 | 41 |
ret->hour = score_date[2]; |
44 | 42 |
|
45 |
if (rtc_read_time(score_date)) return NULL;
|
|
43 |
if (rtc_read_date(score_date)) return NULL;
|
|
46 | 44 |
|
47 | 45 |
ret->day = score_date[1]; |
48 | 46 |
ret->month = score_date[2]; |
... | ... | |
62 | 60 |
if(fnt == NULL || path == NULL) return NULL; |
63 | 61 |
|
64 | 62 |
FILE *f = fopen(path, "r"); |
65 |
if (f == NULL) return NULL;
|
|
63 |
if (f == NULL) { printf("NULL\n");return NULL;}
|
|
66 | 64 |
highscores_t *ret = (highscores_t*)malloc(sizeof(highscores_t)); |
67 | 65 |
if (ret == NULL) return NULL; |
68 |
char line[30]; |
|
69 | 66 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
70 |
fgets(line, 30, f); |
|
71 | 67 |
score_info_t *score = (score_info_t*)malloc(sizeof(score_info_t)); |
68 |
memset(score, 0, sizeof(score_info_t)); |
|
72 | 69 |
|
73 |
sscanf(line, "%d/%d/%d %d:%d:%d - %d - %d\n",
|
|
70 |
fscanf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n",
|
|
74 | 71 |
&(score->day), &(score->month), &(score->year), |
75 |
&(score->hour), &(score->month), &(score->sec),
|
|
72 |
&(score->hour), &(score->min), &(score->sec),
|
|
76 | 73 |
&(score->score), &(score->time_played)); |
77 | 74 |
ret->highscores[i] = score; |
78 | 75 |
} |
... | ... | |
81 | 78 |
return ret; |
82 | 79 |
} |
83 | 80 |
|
81 |
void (check_new_score)(highscores_t *p, int score, int time_played){ |
|
82 |
score_info_t *new_score = score_ctor(score, time_played); |
|
83 |
if (new_score == NULL) return; |
|
84 |
if (score > (p->highscores[0])->score) { // rank 1 |
|
85 |
score_info_t *s = p->highscores[0]; |
|
86 |
p->highscores[0] = new_score; |
|
87 |
new_score = s; |
|
88 |
s = p->highscores[1]; |
|
89 |
p->highscores[1] = new_score; |
|
90 |
new_score = s; |
|
91 |
s = p->highscores[2]; |
|
92 |
p->highscores[2] = new_score; |
|
93 |
score_dtor(s); |
|
94 |
} else if (score > (p->highscores[1])->score) { // rank 2 |
|
95 |
score_info_t *s = p->highscores[1]; |
|
96 |
p->highscores[1] = new_score; |
|
97 |
new_score = s; |
|
98 |
s = p->highscores[2]; |
|
99 |
p->highscores[2] = new_score; |
|
100 |
score_dtor(s); |
|
101 |
} else if (score > (p->highscores[2])->score) { // rank 3 |
|
102 |
score_info_t *s = p->highscores[2]; |
|
103 |
p->highscores[2] = new_score; |
|
104 |
score_dtor(s); |
|
105 |
} else { |
|
106 |
score_dtor(new_score); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
84 | 110 |
void (highscores_save)(const highscores_t *p, const char *path) { |
85 | 111 |
if(p == NULL || path == NULL) return; |
86 | 112 |
|
... | ... | |
88 | 114 |
if (f == NULL) return; |
89 | 115 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
90 | 116 |
score_info_t *score = p->highscores[i]; |
91 |
fprintf(f, "%d/%d/%d %d:%d:%d - %d - %d\n",
|
|
117 |
fprintf(f, "%02d/%02d/%02d %02d:%02d:%02d - %d - %d\n",
|
|
92 | 118 |
(score->day), (score->month), (score->year), |
93 | 119 |
(score->hour), (score->month), (score->sec), |
94 | 120 |
(score->score), (score->time_played)); |
... | ... | |
97 | 123 |
} |
98 | 124 |
|
99 | 125 |
void (highscores_dtor)(highscores_t *p) { |
126 |
if (p == NULL) return; |
|
100 | 127 |
for (int i = 0; i < MAX_HIGHSCORES; i++) { |
101 | 128 |
score_dtor(p->highscores[i]); |
102 | 129 |
} |
130 |
free(p); |
|
103 | 131 |
} |
proj/src/project/src/singleplayer.c | ||
---|---|---|
21 | 21 |
menu_t *main_menu = menu_ctor(font_get_default()); |
22 | 22 |
menu_add_item(main_menu, "Campaign"); |
23 | 23 |
menu_add_item(main_menu, "Zombies"); |
24 |
menu_add_item(main_menu, "Zombies Ranking"); |
|
24 | 25 |
menu_add_item(main_menu, "Back"); |
25 | 26 |
|
27 |
highscores_t *zombies_highscore = highscores_ctor(font_get_default(), "/home/lcom/labs/proj/src/data/scoreboard.txt"); |
|
28 |
|
|
26 | 29 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE; |
27 | 30 |
uint8_t last_lb = 0; |
28 | 31 |
struct packet pp; |
... | ... | |
46 | 49 |
switch(menu_update_state(main_menu, click)){ |
47 | 50 |
case -1: break; |
48 | 51 |
case 0: campaign(); break; |
49 |
case 1: zombies(); break; |
|
50 |
case 2: good = false; break; |
|
52 |
case 1: zombies(zombies_highscore); break; |
|
53 |
case 2: zombies_ranking(zombies_highscore); break; |
|
54 |
case 3: good = false; break; |
|
51 | 55 |
} |
52 | 56 |
menu_draw(main_menu); |
53 | 57 |
|
... | ... | |
75 | 79 |
} |
76 | 80 |
} |
77 | 81 |
|
82 |
highscores_save(zombies_highscore, "/home/lcom/labs/proj/src/data/scoreboard.txt"); |
|
83 |
highscores_dtor(zombies_highscore); |
|
84 |
|
|
85 |
menu_dtor(main_menu); |
|
86 |
|
|
78 | 87 |
return 0; |
79 | 88 |
} |
proj/src/project/src/zombies.c | ||
---|---|---|
14 | 14 |
|
15 | 15 |
#define ZOMBIES_NUM 5 |
16 | 16 |
#define ZOMBIE_HEALTH_FACTOR 1.1 |
17 |
int (zombies)(void){
|
|
17 |
int (zombies)(highscores_t *zombies_highscore){
|
|
18 | 18 |
|
19 | 19 |
int r; |
20 | 20 |
|
... | ... | |
43 | 43 |
uint64_t int_vector = 0; |
44 | 44 |
int good = true; |
45 | 45 |
int dead = false; |
46 |
/// Kills |
|
47 |
int kills = -ZOMBIES_NUM; |
|
48 |
text_t *text_kills = text_ctor(font_get_default(), "Kills: 0"); |
|
49 |
text_set_color(text_kills, TEXT_COLOR); |
|
50 |
text_set_pos(text_kills, 200, 0); |
|
46 | 51 |
|
47 | 52 |
int health = 50; |
48 | 53 |
|
... | ... | |
86 | 91 |
health *= ZOMBIE_HEALTH_FACTOR; |
87 | 92 |
get_random_spawn(map1, zombie, shooter_list); |
88 | 93 |
list_push_back(shooter_list, zombie); |
94 |
kills++; |
|
95 |
char buffer[20]; |
|
96 |
sprintf(buffer, "Kills: %d", kills); |
|
97 |
text_set_string(text_kills, buffer); |
|
89 | 98 |
} |
90 | 99 |
|
91 | 100 |
graph_clear_screen(); |
92 | 101 |
map_draw (map1); |
93 | 102 |
bullet_draw_list(bullet_list); |
94 | 103 |
gunner_draw_list(shooter_list); |
95 |
|
|
96 | 104 |
text_draw(in_game_timer->text); |
105 |
text_draw(text_kills); |
|
97 | 106 |
|
98 | 107 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
99 | 108 |
sprite_draw(sp_crosshair); |
... | ... | |
135 | 144 |
} |
136 | 145 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
137 | 146 |
|
138 |
if(dead){ |
|
139 |
printf("YOU DIED\n"); |
|
140 |
} |
|
147 |
check_new_score(zombies_highscore, kills, in_game_timer->time); |
|
141 | 148 |
|
149 |
text_dtor(text_kills); |
|
150 |
|
|
142 | 151 |
text_timer_dtor(in_game_timer); in_game_timer = NULL; |
143 | 152 |
|
144 | 153 |
return SUCCESS; |
145 | 154 |
} |
155 |
|
|
156 |
void (zombies_ranking)(highscores_t *zombies_highscore) { |
|
157 |
|
|
158 |
} |
proj/src/Makefile | ||
---|---|---|
12 | 12 |
SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c zombies.c campaign.c singleplayer.c chat.c scoreboards.c |
13 | 13 |
IPATHS=-I./project/include -I./libs -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm |
14 | 14 |
|
15 |
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO -D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros |
|
15 |
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros
|
|
16 | 16 |
|
17 | 17 |
DPADD += ${LIBLCF} |
18 | 18 |
LDADD += -llcf |
Also available in: Unified diff