root / proj / src / proj.c @ 308
History | View | Annotate | Download (25.3 KB)
1 | 144 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/proj.h> |
||
3 | #include <lcom/liblm.h> |
||
4 | 185 | up20180655 | #include <math.h> |
5 | 144 | up20180655 | |
6 | 149 | up20180655 | #include "proj_macros.h" |
7 | 179 | up20180642 | #include "proj_func.h" |
8 | 147 | up20180655 | |
9 | 149 | up20180655 | #include "kbc.h" |
10 | #include "timer.h" |
||
11 | #include "keyboard.h" |
||
12 | 150 | up20180655 | #include "mouse.h" |
13 | 189 | up20180655 | #include "graph.h" |
14 | 291 | up20180642 | #include "menu.h" |
15 | 234 | up20180655 | #include "rtc.h" |
16 | 287 | up20180642 | #include "hltp.h" |
17 | 153 | up20180655 | #include "interrupts_func.h" |
18 | 270 | up20180655 | #include "makecode_map.h" |
19 | 149 | up20180655 | |
20 | 179 | up20180642 | #include "graph.h" |
21 | #include "rectangle.h" |
||
22 | 182 | up20180642 | #include "font.h" |
23 | 192 | up20180642 | #include "ent.h" |
24 | 168 | up20180642 | |
25 | 192 | up20180642 | #include "crosshair.h" |
26 | #include "shooter.h" |
||
27 | 303 | up20180642 | #include "zombie.h" |
28 | 192 | up20180642 | #include "pistol.h" |
29 | #include "nothing.h" |
||
30 | 237 | up20180642 | #include "bullet.h" |
31 | 216 | up20180642 | #include "map1.h" |
32 | 183 | up20180642 | |
33 | 294 | up20180642 | #include "errors.h" |
34 | |||
35 | 226 | up20180642 | #include "list.h" |
36 | |||
37 | 144 | up20180655 | int main(int argc, char* argv[]) { |
38 | |||
39 | lcf_set_language("EN-US");
|
||
40 | |||
41 | 235 | up20180642 | lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
42 | 144 | up20180655 | |
43 | 189 | up20180655 | lcf_log_output("/home/lcom/labs/proj/output.txt");
|
44 | 144 | up20180655 | |
45 | if (lcf_start(argc, argv)) return 1; |
||
46 | |||
47 | lcf_cleanup(); |
||
48 | |||
49 | return 0; |
||
50 | } |
||
51 | 147 | up20180655 | |
52 | 294 | up20180642 | font_t *consolas = NULL;
|
53 | basic_sprite_t *bsp_crosshair = NULL;
|
||
54 | basic_sprite_t *bsp_shooter = NULL;
|
||
55 | 301 | up20180642 | basic_sprite_t *bsp_zombie = NULL;
|
56 | 294 | up20180642 | basic_sprite_t *bsp_pistol = NULL;
|
57 | basic_sprite_t *bsp_nothing = NULL;
|
||
58 | basic_sprite_t *bsp_bullet = NULL;
|
||
59 | map_t *map1 = NULL;
|
||
60 | sprite_t *sp_crosshair = NULL;
|
||
61 | |||
62 | 300 | up20180642 | static int (singleplayer)(void); |
63 | 308 | up20180655 | //static int (multiplayer)(void);
|
64 | 300 | up20180642 | static int (chat)(void); |
65 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
66 | 170 | up20180642 | |
67 | int r;
|
||
68 | |||
69 | 294 | up20180642 | consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
|
70 | 261 | up20180642 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
71 | 192 | up20180642 | |
72 | 261 | up20180642 | /// subscribe interrupts
|
73 | if (subscribe_all()) { return 1; } |
||
74 | 170 | up20180642 | |
75 | 297 | up20180642 | /// initialize graphics
|
76 | if(graph_init(GRAPH_MODE)){
|
||
77 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
78 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
79 | return 1; |
||
80 | } |
||
81 | 152 | up20180642 | |
82 | 261 | up20180642 | /// Load stuff
|
83 | { |
||
84 | 297 | up20180642 | graph_clear_screen(); |
85 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
86 | text_draw(txt); |
||
87 | text_dtor(txt); |
||
88 | graph_draw(); |
||
89 | 183 | up20180642 | |
90 | 261 | up20180642 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
91 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
92 | 303 | up20180642 | bsp_zombie = get_zombie (); if(bsp_zombie == NULL) printf("Failed to get zombie\n"); |
93 | 261 | up20180642 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
94 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
95 | bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
||
96 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
||
97 | 226 | up20180642 | |
98 | 261 | up20180642 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
99 | } |
||
100 | 226 | up20180642 | |
101 | 297 | up20180642 | menu_t *main_menu = menu_ctor(consolas); |
102 | 299 | up20180642 | menu_add_item(main_menu, "Single player");
|
103 | menu_add_item(main_menu, "Multiplayer");
|
||
104 | 297 | up20180642 | menu_add_item(main_menu, "Chat");
|
105 | menu_add_item(main_menu, "Exit");
|
||
106 | 234 | up20180655 | |
107 | 297 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
108 | uint8_t last_lb = 0;
|
||
109 | struct packet pp;
|
||
110 | keys_t *keys = get_key_presses(); |
||
111 | |||
112 | 261 | up20180642 | /// loop stuff
|
113 | 297 | up20180642 | int click = 0; |
114 | 307 | up20180642 | uint32_t int_vector = 0;
|
115 | 295 | up20180642 | int good = true; |
116 | while (good) {
|
||
117 | 261 | up20180642 | /* Get a request message. */
|
118 | 307 | up20180642 | if((r = get_interrupts_vector(&int_vector))) return r; |
119 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
120 | if (int_vector & n) {
|
||
121 | interrupt_handler(i); |
||
122 | switch (i) {
|
||
123 | case TIMER0_IRQ:
|
||
124 | 192 | up20180642 | |
125 | 307 | up20180642 | graph_clear_screen(); |
126 | switch(menu_update_state(main_menu, click)){
|
||
127 | case -1: break; |
||
128 | case 0: singleplayer(); break; //campaign(); break; |
||
129 | case 1: break; |
||
130 | case 2: chat(); break; |
||
131 | case 3: good = false; break; |
||
132 | } |
||
133 | menu_draw(main_menu); |
||
134 | 192 | up20180642 | |
135 | 307 | up20180642 | click = 0;
|
136 | 202 | up20180655 | |
137 | 307 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
138 | sprite_draw(sp_crosshair); |
||
139 | graph_draw(); |
||
140 | 231 | up20180655 | |
141 | 307 | up20180642 | break;
|
142 | case KBC_IRQ:
|
||
143 | if ((scancode[0]) == ESC_BREAK_CODE) good = false; |
||
144 | case MOUSE_IRQ:
|
||
145 | if (counter_mouse_ih >= 3) { |
||
146 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
147 | update_mouse(&pp); |
||
148 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
||
149 | last_lb = keys->lb_pressed; |
||
150 | counter_mouse_ih = 0;
|
||
151 | 261 | up20180642 | } |
152 | 307 | up20180642 | break;
|
153 | case COM1_IRQ: nctp_ih(); break; |
||
154 | 305 | up20180642 | } |
155 | 147 | up20180655 | } |
156 | } |
||
157 | 261 | up20180642 | } |
158 | 149 | up20180655 | |
159 | 261 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
160 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
161 | 307 | up20180642 | basic_sprite_dtor (bsp_zombie ); bsp_zombie = NULL;
|
162 | 261 | up20180642 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
163 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
164 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
165 | map_dtor (map1 ); map1 = NULL;
|
||
166 | font_dtor (consolas ); consolas = NULL;
|
||
167 | 243 | up20180642 | |
168 | 261 | up20180642 | // Unsubscribe interrupts
|
169 | if (unsubscribe_all()) {
|
||
170 | if (cleanup())
|
||
171 | 305 | up20180642 | printf("%s: failed to cleanup.\n", __func__);
|
172 | 261 | up20180642 | return 1; |
173 | } |
||
174 | 188 | up20180642 | |
175 | 261 | up20180642 | if (cleanup()) {
|
176 | printf("%s: failed to cleanup.\n", __func__);
|
||
177 | return 1; |
||
178 | } |
||
179 | 155 | up20180655 | |
180 | 149 | up20180655 | return 0; |
181 | 147 | up20180655 | } |
182 | 294 | up20180642 | |
183 | 308 | up20180655 | typedef struct __attribute__((packed)) { |
184 | // player 1
|
||
185 | double player1_x;
|
||
186 | double player1_y;
|
||
187 | double player1_health;
|
||
188 | double player1_current_health;
|
||
189 | |||
190 | // player 2
|
||
191 | double player2_x;
|
||
192 | double player2_y;
|
||
193 | double player2_health;
|
||
194 | double player2_current_health;
|
||
195 | |||
196 | // bullets
|
||
197 | size_t no_bullets; |
||
198 | double *bullets_x;
|
||
199 | double *bullets_y;
|
||
200 | double *bullets_vx;
|
||
201 | double *bullets_vy;
|
||
202 | bool *bullet_shooter; // 0 for player 1, otherwise 1 for player 2 |
||
203 | } host_info; |
||
204 | |||
205 | typedef struct __attribute__((packed)) { |
||
206 | keys_t client_keys_pressed; |
||
207 | int32_t client_mouse_x; |
||
208 | int32_t client_mouse_y; |
||
209 | size_t bullets_shot; |
||
210 | double *bullets_x;
|
||
211 | double *bullets_y;
|
||
212 | } client_info; |
||
213 | /*
|
||
214 | static void host_to_client_process(const uint8_t *p, const size_t sz) {
|
||
215 | |||
216 | }
|
||
217 | |||
218 | static void client_to_host_process(const uint8_t *p, const size_t sz) {
|
||
219 | }
|
||
220 | |||
221 | static int (multiplayer)(void) {
|
||
222 | |||
223 | //int r;
|
||
224 | |||
225 | ent_set_scale(DEFAULT_SCALE);
|
||
226 | //text_timer_t *in_game_timer = timer_ctor(consolas);
|
||
227 | |||
228 | list_t *shooter_list = list_ctor();
|
||
229 | |||
230 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
|
||
231 | gunner_set_spawn(shooter1, 75, 75);
|
||
232 | |||
233 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
|
||
234 | gunner_set_spawn(shooter2, 975, 75);
|
||
235 | |||
236 | list_insert(shooter_list, list_end(shooter_list), shooter1);
|
||
237 | list_insert(shooter_list, list_end(shooter_list), shooter2);
|
||
238 | |||
239 | do {
|
||
240 | get_random_spawn(map1, shooter1, shooter_list);
|
||
241 | get_random_spawn(map1, shooter2, shooter_list);
|
||
242 | } while (distance_gunners(shooter1, shooter2) < 700);
|
||
243 | |||
244 | list_t *bullet_list = list_ctor();
|
||
245 | |||
246 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
|
||
247 | gunner_get_y(shooter1)-ent_get_YLength()/2.0);
|
||
248 | |||
249 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
250 | uint8_t last_lb = 0;
|
||
251 | struct packet pp;
|
||
252 | keys_t *keys = get_key_presses();
|
||
253 | |||
254 | /// loop stuff
|
||
255 | uint32_t int_vector = 0;
|
||
256 | int good = true;
|
||
257 | |||
258 | // incomplete
|
||
259 | |||
260 | return 0;
|
||
261 | }*/
|
||
262 | |||
263 | 300 | up20180642 | static int (campaign)(void); |
264 | 301 | up20180642 | static int (zombies)(void); |
265 | 300 | up20180642 | static int (singleplayer)(void) { |
266 | |||
267 | 295 | up20180642 | int r;
|
268 | |||
269 | 300 | up20180642 | menu_t *main_menu = menu_ctor(consolas); |
270 | menu_add_item(main_menu, "Campaign");
|
||
271 | menu_add_item(main_menu, "Zombies");
|
||
272 | menu_add_item(main_menu, "Back");
|
||
273 | |||
274 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
||
275 | uint8_t last_lb = 0;
|
||
276 | struct packet pp;
|
||
277 | keys_t *keys = get_key_presses(); |
||
278 | |||
279 | /// loop stuff
|
||
280 | int click = 0; |
||
281 | 304 | up20180642 | uint32_t int_vector = 0;
|
282 | 300 | up20180642 | int good = true; |
283 | while (good) {
|
||
284 | /* Get a request message. */
|
||
285 | 304 | up20180642 | if((r = get_interrupts_vector(&int_vector))) return r; |
286 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
287 | if (int_vector & n) {
|
||
288 | interrupt_handler(i); |
||
289 | switch (i) {
|
||
290 | 305 | up20180642 | case TIMER0_IRQ:
|
291 | 300 | up20180642 | |
292 | 304 | up20180642 | graph_clear_screen(); |
293 | switch(menu_update_state(main_menu, click)){
|
||
294 | case -1: break; |
||
295 | case 0: campaign(); break; |
||
296 | case 1: zombies(); break; |
||
297 | case 2: good = false; break; |
||
298 | } |
||
299 | menu_draw(main_menu); |
||
300 | 300 | up20180642 | |
301 | 304 | up20180642 | click = 0;
|
302 | 300 | up20180642 | |
303 | 304 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
304 | sprite_draw(sp_crosshair); |
||
305 | graph_draw(); |
||
306 | 300 | up20180642 | |
307 | 304 | up20180642 | break;
|
308 | 305 | up20180642 | case KBC_IRQ:
|
309 | 304 | up20180642 | if ((scancode[0]) == ESC_BREAK_CODE) good = false; |
310 | 305 | up20180642 | case MOUSE_IRQ:
|
311 | 304 | up20180642 | if (counter_mouse_ih >= 3) { |
312 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
313 | update_mouse(&pp); |
||
314 | if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
||
315 | last_lb = keys->lb_pressed; |
||
316 | counter_mouse_ih = 0;
|
||
317 | 300 | up20180642 | } |
318 | break;
|
||
319 | 305 | up20180642 | case COM1_IRQ: nctp_ih(); break; |
320 | 304 | up20180642 | } |
321 | 300 | up20180642 | } |
322 | } |
||
323 | } |
||
324 | |||
325 | return 0; |
||
326 | } |
||
327 | |||
328 | static int (campaign)(void){ |
||
329 | |||
330 | int r;
|
||
331 | |||
332 | 295 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
333 | text_timer_t *in_game_timer = timer_ctor(consolas); |
||
334 | |||
335 | list_t *shooter_list = list_ctor(); |
||
336 | |||
337 | 302 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
338 | 295 | up20180642 | gunner_set_spawn(shooter1, 75, 75); |
339 | gunner_set_pos(shooter1, 75, 75); |
||
340 | |||
341 | 302 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
|
342 | 295 | up20180642 | gunner_set_spawn(shooter2, 975, 75); |
343 | gunner_set_pos(shooter2, 775, 75); |
||
344 | |||
345 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
346 | list_insert(shooter_list, list_end(shooter_list), shooter2); |
||
347 | |||
348 | list_t *bullet_list = list_ctor(); |
||
349 | |||
350 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
351 | 305 | up20180642 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
352 | 295 | up20180642 | |
353 | 305 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
354 | uint8_t last_lb = 0;
|
||
355 | struct packet pp;
|
||
356 | keys_t *keys = get_key_presses(); |
||
357 | 295 | up20180642 | |
358 | /// loop stuff
|
||
359 | 304 | up20180642 | uint32_t int_vector = 0;
|
360 | 296 | up20180642 | int good = true; |
361 | while (good) {
|
||
362 | 304 | up20180642 | /* Get a request message. */
|
363 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
364 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
365 | 305 | up20180642 | if (int_vector & n) {
|
366 | interrupt_handler(i); |
||
367 | switch (i) {
|
||
368 | case TIMER0_IRQ:
|
||
369 | 295 | up20180642 | |
370 | 305 | up20180642 | if (no_interrupts % 60 == 0) timer_update(in_game_timer); |
371 | update_movement(map1, shooter1, keys, shooter_list); |
||
372 | 295 | up20180642 | |
373 | 305 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
374 | 295 | up20180642 | |
375 | 305 | up20180642 | //update_scale();
|
376 | double angle = get_mouse_angle(shooter1);
|
||
377 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
378 | 295 | up20180642 | |
379 | 305 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
380 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
381 | 295 | up20180642 | |
382 | 305 | up20180642 | graph_clear_screen(); |
383 | map_draw (map1); |
||
384 | bullet_draw_list(bullet_list); |
||
385 | gunner_draw_list(shooter_list); |
||
386 | 295 | up20180642 | |
387 | 305 | up20180642 | text_draw(in_game_timer->text); |
388 | 295 | up20180642 | |
389 | 305 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
390 | sprite_draw(sp_crosshair); |
||
391 | graph_draw(); |
||
392 | 295 | up20180642 | |
393 | 305 | up20180642 | break;
|
394 | case KBC_IRQ:
|
||
395 | if ((scancode[0]) == ESC_BREAK_CODE) { |
||
396 | good = false;
|
||
397 | // reset game
|
||
398 | while(list_size(bullet_list) > 0){ |
||
399 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
400 | bullet_dtor(p); |
||
401 | } |
||
402 | list_node_t *it = list_begin(shooter_list); |
||
403 | while (it != list_end(shooter_list)) {
|
||
404 | gunner_t *p = *(gunner_t**)list_node_val(it); |
||
405 | 307 | up20180642 | get_random_spawn(map1, p, shooter_list); |
406 | 305 | up20180642 | gunner_set_curr_health(p, gunner_get_health(p)); |
407 | it = list_node_next(it); |
||
408 | } |
||
409 | timer_reset(in_game_timer); |
||
410 | } |
||
411 | break;
|
||
412 | case MOUSE_IRQ:
|
||
413 | if (counter_mouse_ih >= 3) { |
||
414 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
415 | update_mouse(&pp); |
||
416 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
||
417 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
418 | last_lb = keys->lb_pressed; |
||
419 | counter_mouse_ih = 0;
|
||
420 | 304 | up20180642 | |
421 | 305 | up20180642 | } |
422 | break;
|
||
423 | case COM1_IRQ: nctp_ih(); break; |
||
424 | } |
||
425 | } |
||
426 | 304 | up20180642 | } |
427 | 295 | up20180642 | } |
428 | |||
429 | while(list_size(shooter_list) > 0){ |
||
430 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
431 | gunner_dtor(p); |
||
432 | } |
||
433 | 296 | up20180642 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
434 | 295 | up20180642 | |
435 | 301 | up20180642 | while(list_size(bullet_list) > 0){ |
436 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
437 | bullet_dtor(p); |
||
438 | } |
||
439 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
440 | |||
441 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
||
442 | |||
443 | return SUCCESS;
|
||
444 | } |
||
445 | |||
446 | #define ZOMBIES_NUM 5 |
||
447 | #define ZOMBIE_HEALTH_FACTOR 1.1 |
||
448 | |||
449 | static int (zombies)(void){ |
||
450 | |||
451 | int r;
|
||
452 | |||
453 | ent_set_scale(DEFAULT_SCALE); |
||
454 | text_timer_t *in_game_timer = timer_ctor(consolas); |
||
455 | |||
456 | list_t *shooter_list = list_ctor(); |
||
457 | |||
458 | 302 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
459 | 305 | up20180642 | gunner_set_spawn(shooter1, 980, 790); |
460 | gunner_set_pos(shooter1, 980, 790); |
||
461 | 301 | up20180642 | |
462 | list_insert(shooter_list, list_end(shooter_list), shooter1); |
||
463 | |||
464 | list_t *bullet_list = list_ctor(); |
||
465 | |||
466 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
||
467 | 305 | up20180642 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
468 | 301 | up20180642 | |
469 | 305 | up20180642 | //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
470 | uint8_t last_lb = 0;
|
||
471 | struct packet pp;
|
||
472 | keys_t *keys = get_key_presses(); |
||
473 | 301 | up20180642 | |
474 | /// loop stuff
|
||
475 | 304 | up20180642 | uint32_t int_vector = 0;
|
476 | 301 | up20180642 | int good = true; |
477 | 302 | up20180642 | int dead = false; |
478 | 301 | up20180642 | |
479 | int health = 50; |
||
480 | |||
481 | 302 | up20180642 | while (good && !dead) {
|
482 | 304 | up20180642 | /* Get a request message. */
|
483 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
484 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
485 | if (int_vector & n) {
|
||
486 | 305 | up20180642 | interrupt_handler(i); |
487 | switch (i) {
|
||
488 | case TIMER0_IRQ:
|
||
489 | if (no_interrupts % 60 == 0) timer_update(in_game_timer); |
||
490 | 301 | up20180642 | |
491 | 305 | up20180642 | update_movement(map1, shooter1, keys, shooter_list); |
492 | 301 | up20180642 | |
493 | 305 | up20180642 | update_game_state(map1, shooter_list, bullet_list); |
494 | 302 | up20180642 | |
495 | 305 | up20180642 | if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
|
496 | good = false;
|
||
497 | dead = true;
|
||
498 | break;
|
||
499 | } |
||
500 | 301 | up20180642 | |
501 | 305 | up20180642 | double angle = get_mouse_angle(shooter1);
|
502 | gunner_set_angle(shooter1, angle - M_PI_2); |
||
503 | 301 | up20180642 | |
504 | 305 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
505 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
506 | 301 | up20180642 | |
507 | 305 | up20180642 | while(list_size(shooter_list) < ZOMBIES_NUM){
|
508 | gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
|
||
509 | gunner_set_health(zombie, health); |
||
510 | gunner_set_curr_health(zombie, health); |
||
511 | health *= ZOMBIE_HEALTH_FACTOR; |
||
512 | 307 | up20180642 | get_random_spawn(map1, zombie, shooter_list); |
513 | 305 | up20180642 | list_push_back(shooter_list, zombie); |
514 | } |
||
515 | 301 | up20180642 | |
516 | 305 | up20180642 | graph_clear_screen(); |
517 | map_draw (map1); |
||
518 | bullet_draw_list(bullet_list); |
||
519 | gunner_draw_list(shooter_list); |
||
520 | 301 | up20180642 | |
521 | 305 | up20180642 | text_draw(in_game_timer->text); |
522 | 301 | up20180642 | |
523 | 305 | up20180642 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
524 | sprite_draw(sp_crosshair); |
||
525 | graph_draw(); |
||
526 | 304 | up20180642 | |
527 | 305 | up20180642 | break;
|
528 | case KBC_IRQ:
|
||
529 | if ((scancode[0]) == ESC_BREAK_CODE) { |
||
530 | good = false;
|
||
531 | } |
||
532 | break;
|
||
533 | case MOUSE_IRQ:
|
||
534 | if (counter_mouse_ih >= 3) { |
||
535 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
536 | update_mouse(&pp); |
||
537 | if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
||
538 | shoot_bullet(shooter1, bullet_list, bsp_bullet); |
||
539 | last_lb = keys->lb_pressed; |
||
540 | counter_mouse_ih = 0;
|
||
541 | |||
542 | } |
||
543 | break;
|
||
544 | case COM1_IRQ: nctp_ih(); break; |
||
545 | } |
||
546 | } |
||
547 | } |
||
548 | 301 | up20180642 | } |
549 | |||
550 | while(list_size(shooter_list) > 0){ |
||
551 | gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
||
552 | gunner_dtor(p); |
||
553 | } |
||
554 | if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
||
555 | |||
556 | 295 | up20180642 | while(list_size(bullet_list) > 0){ |
557 | bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
||
558 | bullet_dtor(p); |
||
559 | } |
||
560 | if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
||
561 | |||
562 | 302 | up20180642 | if(dead){
|
563 | printf("YOU DIED\n");
|
||
564 | } |
||
565 | |||
566 | 295 | up20180642 | timer_dtor(in_game_timer); in_game_timer = NULL;
|
567 | |||
568 | 294 | up20180642 | return SUCCESS;
|
569 | } |
||
570 | 296 | up20180642 | |
571 | 298 | up20180642 | #define CHAT_MAX_SIZE 75 |
572 | #define CHAT_MAX_NUM 19 |
||
573 | 297 | up20180642 | text_t *t_text[CHAT_MAX_NUM] = {NULL};
|
574 | rectangle_t *r_text = NULL;
|
||
575 | static void chat_process(const uint8_t *p, const size_t sz){ |
||
576 | char buffer2[CHAT_MAX_NUM+3]; |
||
577 | void *dest = NULL; |
||
578 | hltp_type tp = hltp_interpret(p, sz, &dest); |
||
579 | switch(tp){
|
||
580 | case hltp_type_string:
|
||
581 | 305 | up20180642 | strcpy(buffer2, dest); |
582 | strncat(buffer2, " <", 2); |
||
583 | for(size_t i = CHAT_MAX_NUM-1; i; --i) |
||
584 | text_set_text(t_text[i], text_get_string(t_text[i-1]));
|
||
585 | text_set_text(t_text[0], buffer2);
|
||
586 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
||
587 | if(text_get_string(t_text[i])[0] == '>'){ |
||
588 | text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
||
589 | text_set_halign(t_text[i], text_halign_left); |
||
590 | }else{
|
||
591 | text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
||
592 | text_set_halign(t_text[i], text_halign_right); |
||
593 | 297 | up20180642 | } |
594 | 305 | up20180642 | } |
595 | break;
|
||
596 | 297 | up20180642 | default: break; |
597 | } |
||
598 | } |
||
599 | 300 | up20180642 | static int (chat)(void){ |
600 | 297 | up20180642 | int r;
|
601 | |||
602 | 298 | up20180642 | nctp_dump(); |
603 | 297 | up20180642 | nctp_set_processor(chat_process); |
604 | |||
605 | struct packet pp;
|
||
606 | |||
607 | 298 | up20180642 | char buffer[CHAT_MAX_SIZE] = ""; |
608 | 297 | up20180642 | rectangle_t *r_buffer = NULL; {
|
609 | 298 | up20180642 | r_buffer = rectangle_ctor(0,0,900,70); |
610 | 297 | up20180642 | rectangle_set_pos(r_buffer, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
611 | 305 | up20180642 | graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2); |
612 | 297 | up20180642 | rectangle_set_fill_color (r_buffer, GRAPH_BLACK); |
613 | rectangle_set_outline_width(r_buffer, 2);
|
||
614 | rectangle_set_outline_color(r_buffer, GRAPH_WHITE); |
||
615 | rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT); |
||
616 | } |
||
617 | text_t *t_buffer = NULL; {
|
||
618 | t_buffer = text_ctor(consolas, "");
|
||
619 | text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
|
||
620 | 305 | up20180642 | rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
|
621 | 297 | up20180642 | text_set_halign(t_buffer, text_halign_left); |
622 | text_set_valign(t_buffer, text_valign_center); |
||
623 | text_set_color (t_buffer, TEXT_COLOR); |
||
624 | } |
||
625 | 298 | up20180642 | text_t *t_size = NULL; {
|
626 | t_size = text_ctor(consolas, "");
|
||
627 | text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
|
||
628 | 305 | up20180642 | rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
|
629 | 298 | up20180642 | text_set_halign(t_size, text_halign_right); |
630 | text_set_valign(t_size, text_valign_bottom); |
||
631 | text_set_color (t_size, TEXT_COLOR); |
||
632 | text_set_size (t_size, 18);
|
||
633 | char buffer2[20]; |
||
634 | sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
||
635 | text_set_text(t_size, buffer2); |
||
636 | } |
||
637 | 297 | up20180642 | |
638 | /** r_text */ {
|
||
639 | 305 | up20180642 | r_text = rectangle_ctor(0,0,900,550); |
640 | rectangle_set_pos(r_text, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
||
641 | graph_get_YRes()*0.09); |
||
642 | rectangle_set_fill_color (r_text, GRAPH_BLACK); |
||
643 | rectangle_set_outline_width(r_text, 2);
|
||
644 | rectangle_set_outline_color(r_text, GRAPH_WHITE); |
||
645 | rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT); |
||
646 | } |
||
647 | /** t_text */ {
|
||
648 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
||
649 | t_text[i] = text_ctor(consolas, " ");
|
||
650 | text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
|
||
651 | rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i); |
||
652 | text_set_halign(t_text[i], text_halign_left); |
||
653 | text_set_valign(t_text[i], text_valign_bottom); |
||
654 | text_set_color (t_text[i], TEXT_COLOR); |
||
655 | } |
||
656 | } |
||
657 | 297 | up20180642 | |
658 | 305 | up20180642 | /// loop stuff
|
659 | uint32_t int_vector = 0;
|
||
660 | int good = true; |
||
661 | while (good) {
|
||
662 | /* Get a request message. */
|
||
663 | if((r = get_interrupts_vector(&int_vector))) return r; |
||
664 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
||
665 | if (int_vector & n) {
|
||
666 | interrupt_handler(i); |
||
667 | switch (i) {
|
||
668 | 304 | up20180642 | case TIMER0_IRQ:
|
669 | 305 | up20180642 | graph_clear_screen(); |
670 | sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
||
671 | 297 | up20180642 | |
672 | 305 | up20180642 | rectangle_draw(r_buffer); |
673 | text_draw(t_buffer); |
||
674 | text_draw(t_size); |
||
675 | 297 | up20180642 | |
676 | 305 | up20180642 | rectangle_draw(r_text); |
677 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]); |
||
678 | 297 | up20180642 | |
679 | 305 | up20180642 | sprite_draw(sp_crosshair); |
680 | graph_draw(); |
||
681 | break;
|
||
682 | 304 | up20180642 | case KBC_IRQ:
|
683 | 305 | up20180642 | if (scancode[0] == ESC_BREAK_CODE) good = false; |
684 | else if (scancode[0] == ENTER_MAKE_CODE) { |
||
685 | hltp_send_string(buffer); |
||
686 | char buffer2[CHAT_MAX_SIZE+3] = "> "; |
||
687 | strncat(buffer2, buffer, strlen(buffer)); |
||
688 | for(size_t i = CHAT_MAX_NUM-1; i; --i) |
||
689 | text_set_text(t_text[i], text_get_string(t_text[i-1]));
|
||
690 | text_set_text(t_text[0], buffer2);
|
||
691 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
||
692 | if(text_get_string(t_text[i])[0] == '>'){ |
||
693 | text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
||
694 | text_set_halign(t_text[i], text_halign_left); |
||
695 | }else{
|
||
696 | text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
||
697 | text_set_halign(t_text[i], text_halign_right); |
||
698 | 297 | up20180642 | } |
699 | } |
||
700 | 305 | up20180642 | buffer[0] = '\0'; |
701 | } else if(scancode[0] == BACKSPACE_MAKE_CODE){ |
||
702 | buffer[strlen(buffer)-1] = '\0'; |
||
703 | } else {
|
||
704 | char c = map_makecode(scancode[0]); |
||
705 | if (c == ERROR_CODE) break; |
||
706 | if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1); |
||
707 | else printf("Char limit exceeded\n"); |
||
708 | } |
||
709 | text_set_text(t_buffer, buffer); |
||
710 | char buffer2[20]; |
||
711 | sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
||
712 | text_set_text(t_size, buffer2); |
||
713 | 304 | up20180642 | case MOUSE_IRQ:
|
714 | 305 | up20180642 | if (counter_mouse_ih >= 3) { |
715 | mouse_parse_packet(packet_mouse_ih, &pp); |
||
716 | update_mouse(&pp); |
||
717 | counter_mouse_ih = 0;
|
||
718 | } |
||
719 | break;
|
||
720 | 304 | up20180642 | case COM1_IRQ: nctp_ih(); break; |
721 | 297 | up20180642 | } |
722 | } |
||
723 | } |
||
724 | 305 | up20180642 | } |
725 | 297 | up20180642 | |
726 | 305 | up20180642 | rectangle_dtor(r_buffer); |
727 | text_dtor (t_buffer); |
||
728 | 297 | up20180642 | |
729 | 305 | up20180642 | rectangle_dtor(r_text); |
730 | for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]); |
||
731 | 297 | up20180642 | |
732 | 305 | up20180642 | nctp_set_processor(NULL);
|
733 | 298 | up20180642 | |
734 | 305 | up20180642 | return SUCCESS;
|
735 | 296 | up20180642 | } |