comparison examples/tank/tank_main.c @ 1100:fb79175e6cc3

Add AI for enemy tanks
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 05 Dec 2010 12:23:29 +0800
parents e415c55b4a0d
children dbea3e42bf93
comparison
equal deleted inserted replaced
1096:c18ad321844d 1100:fb79175e6cc3
1 #include <math.h> 1 #include <math.h>
2 #include <sys/time.h> 2 #include <sys/time.h>
3 #include <string.h> 3 #include <string.h>
4 #include <mb.h> 4 #include <mb.h>
5 #include <mb_tools.h> 5 #include <mb_tools.h>
6 #include "tank.h"
6 #include "svgs.h" 7 #include "svgs.h"
7 8
8 /*! \defgroup tank Example Tank 9 /*! \defgroup tank Example Tank
9 * @{ 10 * @{
10 */ 11 */
11 /*! \brief Tile types in a map. */ 12 char map[12][16] = {
12 enum { MUD, ROC, BRI, BSH };
13
14 /*! \brief Map of the game. */
15 static char map[12][16] = {
16 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, 13 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
17 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, 14 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
18 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, 15 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC,
19 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, 16 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD},
20 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, 17 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD,
36 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, 33 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD,
37 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, 34 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD},
38 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, 35 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
39 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} 36 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD}
40 }; 37 };
41
42 #define MAP_W 16
43 #define MAP_H 12
44 /* @} */ 38 /* @} */
45
46 /*! \defgroup bullet_elf Bullet Elf
47 * \ingroup tank
48 * @{
49 */
50 /*! \brief Information about bullet elf
51 */
52 struct _tank_bullet {
53 redraw_man_t *rdman;
54 coord_t *coord_pos;
55 coord_t *coord_rot;
56 bullet_t *bullet_obj;
57 int start_map_x, start_map_y;
58 int direction;
59 mb_progm_t *progm;
60 mb_timeval_t start_time;
61 observer_t *observer_redraw;
62 int hit_tmr;
63 mb_timer_man_t *timer_man;
64 };
65 typedef struct _tank_bullet tank_bullet_t;
66 /*! \brief The direction a bullet is going.
67 */
68 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT };
69 /* @} */
70
71 /*! \defgroup tank_elf Tank Elf
72 * \brief Tank elf module provides control functions of tanks in game.
73 * \ingroup tank
74 * @{
75 */
76 /*! \brief Information about a tank elf. */
77 struct _tank {
78 coord_t *coord_pos; /*!< \brief coordinate for position */
79 coord_t *coord_rot; /*!< \brief coordinate for rotation */
80 coord_t *coord_center;
81 int map_x, map_y;
82 int direction;
83 mb_progm_t *progm;
84 tank_bullet_t *bullet;
85 struct _tank_rt *tank_rt; /*!< \brief for bullet to check
86 * hitting on tanks.
87 */
88 };
89 typedef struct _tank tank_t;
90 enum { TD_UP = 0, TD_RIGHT, TD_DOWN, TD_LEFT };
91
92 /* @} */
93
94 typedef struct _tank_rt tank_rt_t;
95
96 /*! \brief Runtime information for tank, this game/example.
97 */
98 struct _tank_rt {
99 tank_t *tank1;
100 tank1_t *tank1_o;
101 tank_t *tank2;
102 tank2_t *tank2_o;
103 int n_enemy;
104 tank_t *tank_enemies[10];
105 tank_en_t *tank_enemies_o[10];
106 tank_t *tanks[12];
107 int n_tanks;
108 void *map[12][16];
109 mb_rt_t *mb_rt;
110 observer_t *kb_observer;
111 };
112 39
113 /*! \ingroup tank_elf 40 /*! \ingroup tank_elf
114 * @{ 41 * @{
115 */ 42 */
116 static tank_t *tank_new(coord_t *coord_pos, 43 static tank_t *tank_new(coord_t *coord_pos,
163 tank->progm = NULL; 90 tank->progm = NULL;
164 } 91 }
165 92
166 #define PI 3.1415926 93 #define PI 3.1415926
167 94
168 static void tank_move(tank_t *tank, int direction, 95 void
169 tank_rt_t *tank_rt) { 96 tank_move(tank_t *tank, int direction, tank_rt_t *tank_rt) {
170 mb_rt_t *mb_rt = tank_rt->mb_rt; 97 mb_rt_t *mb_rt = tank_rt->mb_rt;
171 redraw_man_t *rdman; 98 redraw_man_t *rdman;
172 mb_timer_man_t *timer_man; 99 mb_timer_man_t *timer_man;
173 observer_factory_t *factory; 100 observer_factory_t *factory;
174 /* for the program */ 101 /* for the program */
486 bullet->hit_tmr = mb_timer_man_timeout(bullet->timer_man, &next, 413 bullet->hit_tmr = mb_timer_man_timeout(bullet->timer_man, &next,
487 bullet_hit_chk, arg); 414 bullet_hit_chk, arg);
488 } 415 }
489 416
490 /*! \brief To fire a bullet for a tank. */ 417 /*! \brief To fire a bullet for a tank. */
491 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) { 418 void
419 tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) {
492 mb_rt_t *mb_rt; 420 mb_rt_t *mb_rt;
493 redraw_man_t *rdman; 421 redraw_man_t *rdman;
494 int map_x, map_y; 422 int map_x, map_y;
495 int shift_x, shift_y; 423 int shift_x, shift_y;
496 int shift_len; 424 int shift_len;
736 664
737 rt = mb_runtime_new(":0.0", 800, 600); 665 rt = mb_runtime_new(":0.0", 800, 600);
738 666
739 initial_tank(&tank_rt, rt); 667 initial_tank(&tank_rt, rt);
740 668
669 /* init_enemies(&tank_rt); */
670
741 mb_runtime_event_loop(rt); 671 mb_runtime_event_loop(rt);
742 672
743 mb_runtime_free(rt); 673 mb_runtime_free(rt);
744 } 674 }
745 675