annotate examples/tank/tank_main.c @ 1428:43369f3314d8

Fix the issue of loading components from SVG. - Users can not switch to components in the document that was loaded from previously saved SVG file. - Scribboo can not find components with names listed in the component list. - It is because that Scribboo is fault to use node name of ns0:component instead of 'name' attribute as component name. - It is fixed by using 'name' attribute of ns0:component node.
author Thinker K.F. Li <thinker@codemud.net>
date Sun, 10 Apr 2011 15:28:33 +0800
parents 7451af5d63ec
children
rev   line source
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
1 #include <math.h>
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
2 #include <sys/time.h>
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
3 #include <string.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
4 #include <mb.h>
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 160
diff changeset
5 #include <mb_tools.h>
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
6 #include "tank.h"
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #include "svgs.h"
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
9 /*! \defgroup tank Example Tank
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
10 * @{
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
11 */
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
12 char map[12][16] = {
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD}
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 };
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
38 /* @} */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
39
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
40 /*! \ingroup tank_elf
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
41 * @{
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
42 */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
43 static tank_t *tank_new(coord_t *coord_pos,
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
44 coord_t *coord_rot,
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
45 int map_x, int map_y,
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
46 tank_rt_t *tank_rt) {
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
47 tank_t *tank;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
48 redraw_man_t *rdman;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
49
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
50 tank = O_ALLOC(tank_t);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
51 if(tank == NULL)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
52 return NULL;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
53
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
54 rdman = mb_runtime_rdman(tank_rt->mb_rt);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
55
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
56 tank->coord_pos = coord_pos;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
57 tank->coord_rot = coord_rot;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
58 tank->map_x = map_x;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
59 tank->map_y = map_y;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
60 tank->direction = TD_UP;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
61 tank->progm = NULL;
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
62 tank->bullet = NULL;
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
63 tank->tank_rt = tank_rt;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
64
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
65 memset(coord_pos->matrix, 0, sizeof(co_aix[6]));
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
66 coord_pos->matrix[0] = 1;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
67 coord_pos->matrix[2] = map_x * 50;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
68 coord_pos->matrix[4] = 1;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
69 coord_pos->matrix[5] = map_y * 50;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
70 rdman_coord_changed(rdman, coord_pos);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
71
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
72 return tank;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
73 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
74
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
75 static void tank_free(tank_t *tank, mb_rt_t *mb_rt) {
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
76 if(tank->progm) {
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
77 mb_progm_abort(tank->progm);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
78 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
79 free(tank);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
80 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
81
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
82 /*! \brief Clean program for a tank.
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
83 *
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
84 * It is called when the program is completed.
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
85 */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
86 static void clean_tank_progm_handler(event_t *event, void *arg) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
87 tank_t *tank = (tank_t *)arg;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
88
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
89 mb_progm_free(tank->progm);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
90 tank->progm = NULL;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
91 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
92
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
93 #define PI 3.1415926
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
94
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
95 void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
96 tank_move(tank_t *tank, int direction, tank_rt_t *tank_rt) {
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
97 mb_rt_t *mb_rt = tank_rt->mb_rt;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
98 redraw_man_t *rdman;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
99 mb_timer_man_t *timer_man;
1060
e415c55b4a0d Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents: 1027
diff changeset
100 observer_factory_t *factory;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
101 /* for the program */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
102 mb_progm_t *progm;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
103 subject_t *comp_sub;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
104 mb_word_t *word;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
105 mb_timeval_t start, playing;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
106 mb_timeval_t now;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
107 /* for position */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
108 co_aix sh_x, sh_y;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
109 /* for direction */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
110 float ang1, ang2;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
111 float rot_diff;
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
112 int i;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
113 static co_aix shift_xy[][2] = {{0, -50}, {50, 0}, {0, 50}, {-50, 0}};
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
114 static int map_shift[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
115 static float angles[4] = {0, PI / 2, PI , PI * 3 / 2};
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
116 static float rotations[7] = {PI / 2, PI , -PI / 2,
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
117 0, PI / 2, PI , -PI / 2};
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
118
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
119 if(tank->progm != NULL)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
120 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
121
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
122 /*
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
123 * Keep inside the map.
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
124 */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
125 if(direction == tank->direction) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
126 switch(direction) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
127 case TD_UP:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
128 if(tank->map_y == 0)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
129 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
130 if(map[tank->map_y - 1][tank->map_x] != MUD)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
131 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
132 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
133 case TD_RIGHT:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
134 if(tank->map_x >= (MAP_W - 1))
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
135 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
136 if(map[tank->map_y][tank->map_x + 1] != MUD)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
137 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
138 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
139 case TD_DOWN:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
140 if(tank->map_y >= (MAP_H - 1))
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
141 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
142 if(map[tank->map_y + 1][tank->map_x] != MUD)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
143 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
144 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
145 case TD_LEFT:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
146 if(tank->map_x == 0)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
147 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
148 if(map[tank->map_y][tank->map_x - 1] != MUD)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
149 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
150 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
151 }
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
152
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
153 tank->map_x += map_shift[direction][0];
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
154 tank->map_y += map_shift[direction][1];
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
155 for(i = 0; i < tank_rt->n_tanks; i++) {
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
156 if(tank != tank_rt->tanks[i] &&
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
157 tank->map_x == tank_rt->tanks[i]->map_x &&
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
158 tank->map_y == tank_rt->tanks[i]->map_y) {
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
159 tank->map_x -= map_shift[direction][0];
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
160 tank->map_y -= map_shift[direction][1];
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
161 return;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
162 }
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
163 }
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
164 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
165
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
166 rdman = mb_runtime_rdman(mb_rt);
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
167 timer_man = mb_runtime_timer_man(mb_rt);
1060
e415c55b4a0d Stop using ob as acronym observer
Thinker K.F. Li <thinker@codemud.net>
parents: 1027
diff changeset
168 factory = mb_runtime_observer_factory(mb_rt);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
169
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
170 progm = mb_progm_new(1, rdman);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
171 tank->progm = progm;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
172
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
173 MB_TIMEVAL_SET(&start, 0, 0);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
174 MB_TIMEVAL_SET(&playing, 0, 500000);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
175 word = mb_progm_next_word(progm, &start, &playing);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
176
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
177 if(direction == tank->direction) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
178 /* Shift/move */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
179 sh_x = shift_xy[direction][0];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
180 sh_y = shift_xy[direction][1];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
181 mb_shift_new(sh_x, sh_y, tank->coord_pos, word);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
182 } else {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
183 /* Change direction */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
184 rot_diff = rotations[3 - tank->direction + direction];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
185 ang1 = angles[tank->direction];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
186 ang2 = ang1 + rot_diff;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
187 mb_rotate_new(ang1, ang2, tank->coord_rot, word);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
188 tank->direction = direction;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
189 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
190
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
191 /* Clean program when it is completed. */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
192 comp_sub = mb_progm_get_complete(progm);
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 190
diff changeset
193 subject_add_observer(comp_sub, clean_tank_progm_handler, tank);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
194
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
195 get_now(&now);
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
196 mb_progm_start(progm, timer_man, &now);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
197 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
198
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
199 /* @} */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
200
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
201 /*! \ingroup bullet_elf
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
202 * @{
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
203 */
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
204 /*! \brief Make coord objects for bullet elfs. */
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
205 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
206 coord_t **coord_rot,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
207 coord_t **coord_center) {
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
208 coord_t *coord_back;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
209
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
210 *coord_pos = rdman_coord_new(rdman, rdman->root_coord);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
211
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
212 coord_back = rdman_coord_new(rdman, *coord_pos);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
213 coord_back->matrix[2] = 25;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
214 coord_back->matrix[5] = 25;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
215 rdman_coord_changed(rdman, coord_back);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
216
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
217 *coord_rot = rdman_coord_new(rdman, coord_back);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
218
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
219 *coord_center = rdman_coord_new(rdman, *coord_rot);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
220 (*coord_center)->matrix[2] = -5;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
221 (*coord_center)->matrix[5] = +15;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
222 rdman_coord_changed(rdman, *coord_center);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
223 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
224
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
225 static tank_bullet_t *tank_bullet_new(redraw_man_t *rdman,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
226 int map_x, int map_y,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
227 int direction) {
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
228 tank_bullet_t *bullet;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
229 coord_t *coord_center;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
230 co_aix *matrix;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
231 static float _sins[] = { 0, 1, 0, -1};
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
232 static float _coss[] = { 1, 0, -1, 0};
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
233 float _sin, _cos;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
234
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
235 bullet = O_ALLOC(tank_bullet_t);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
236 bullet->rdman = rdman;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
237 bullet->hit_tmr = -1;
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
238
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
239 make_bullet_elf_coords(rdman, &bullet->coord_pos,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
240 &bullet->coord_rot,
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
241 &coord_center);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
242 bullet->bullet_obj = bullet_new(rdman, coord_center);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
243
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
244 bullet->start_map_x = map_x;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
245 bullet->start_map_y = map_y;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
246 bullet->direction = direction;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
247 bullet->progm = NULL;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
248
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
249 matrix = bullet->coord_pos->matrix;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
250 matrix[2] = map_x * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
251 matrix[5] = map_y * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
252 rdman_coord_changed(rdman, bullet->coord_pos);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
253
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
254 _sin = _sins[direction];
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
255 _cos = _coss[direction];
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
256 matrix = bullet->coord_rot->matrix;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
257 matrix[0] = _cos;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
258 matrix[1] = -_sin;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
259 matrix[3] = _sin;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
260 matrix[4] = _cos;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
261
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
262 return bullet;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
263 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
264
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
265 static void tank_bullet_free(tank_bullet_t *bullet) {
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
266 if(bullet->hit_tmr != -1)
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
267 mb_timer_man_remove(bullet->timer_man, bullet->hit_tmr);
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
268 bullet_free(bullet->bullet_obj);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
269 rdman_coord_subtree_free(bullet->rdman, bullet->coord_pos);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
270 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
271
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
272 static void bullet_go_out_map(event_t *event, void *arg) {
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
273 tank_t *tank = (tank_t *)arg;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
274 tank_bullet_t *bullet;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
275 redraw_man_t *rdman;
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
276
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
277 bullet = tank->bullet;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
278 rdman = bullet->rdman;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
279
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
280 if(bullet->hit_tmr != -1) {
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
281 mb_timer_man_remove(bullet->timer_man, bullet->hit_tmr);
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
282 bullet->hit_tmr = -1;
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
283 }
156
2aad042b30a4 Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents: 155
diff changeset
284
2aad042b30a4 Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents: 155
diff changeset
285 coord_hide(bullet->coord_pos);
2aad042b30a4 Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents: 155
diff changeset
286 rdman_coord_changed(rdman, bullet->coord_pos);
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
287
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
288 bullet = tank->bullet;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
289 mb_progm_free(bullet->progm);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
290 tank_bullet_free(tank->bullet);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
291 tank->bullet = NULL;
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
292 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
293
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
294 static void bullet_bang(tank_bullet_t *bullet, int map_x, int map_y) {
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
295 redraw_man_t *rdman;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
296 mb_timer_man_t *timer_man;
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
297 mb_progm_t *progm;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
298 mb_word_t *word;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
299 mb_timeval_t start, playing;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
300 mb_timeval_t now;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
301 bang_t *bang;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
302 co_aix *matrix;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
303
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
304 rdman = bullet->rdman;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
305 timer_man = bullet->timer_man;
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
306
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
307 bang = bang_new(rdman, rdman->root_coord);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
308 matrix = bang->root_coord->matrix;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
309 matrix[2] = map_x * 50;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
310 matrix[5] = map_y * 50;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
311 rdman_coord_changed(rdman, bang->root_coord);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
312
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
313 progm = mb_progm_new(3, rdman);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
314
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
315 MB_TIMEVAL_SET(&start, 1, 0);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
316 MB_TIMEVAL_SET(&playing, 0, 0);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
317 word = mb_progm_next_word(progm, &start, &playing);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
318 mb_visibility_new(VIS_HIDDEN, bang->root_coord, word);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
319
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
320 MB_TIMEVAL_SET(&start, 1, 500000);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
321 word = mb_progm_next_word(progm, &start, &playing);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
322 mb_visibility_new(VIS_VISIBLE, bang->root_coord, word);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
323
193
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
324 MB_TIMEVAL_SET(&start, 2, 500000);
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
325 word = mb_progm_next_word(progm, &start, &playing);
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
326 mb_visibility_new(VIS_HIDDEN, bang->root_coord, word);
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
327
194
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 193
diff changeset
328 mb_subtree_free_new(bang->root_coord, word);
45d9a1e2764d Add mb_subtree_free animate action and fix bugs.
Thinker K.F. Li <thinker@branda.to>
parents: 193
diff changeset
329
193
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
330 mb_progm_free_completed(progm);
923d91dfb6af Hide bang before completed and remove progm after completed.
Thinker K.F. Li <thinker@branda.to>
parents: 192
diff changeset
331
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
332 get_now(&now);
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
333 mb_progm_start(progm, timer_man, &now);
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
334 }
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
335
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
336 /*! \brief To check if a bullet hits walls or tanks. */
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
337 static void bullet_hit_chk(int hdl,
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
338 const mb_timeval_t *tmo,
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
339 const mb_timeval_t *now,
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
340 void *arg) {
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
341 tank_t *tank = (tank_t *)arg;
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
342 tank_rt_t *tank_rt = tank->tank_rt;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
343 tank_t *tank_hitted;
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
344 tank_bullet_t *bullet;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
345 mb_timeval_t diff, next;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
346 mb_timeval_t unit_tm;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
347 float move_units_f;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
348 int move_units;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
349 int x, y;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
350 int dir;
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
351 int i;
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
352 static int move_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
353
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
354 bullet = tank->bullet;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
355 bullet->hit_tmr = -1; /* clear hit timer that bullet_go_out_map()
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
356 * can not remove it & currupt memory.
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
357 */
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
358
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
359 MB_TIMEVAL_CP(&diff, now);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
360 MB_TIMEVAL_DIFF(&diff, &bullet->start_time);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
361 MB_TIMEVAL_SET(&unit_tm, 0, 250000);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
362 move_units_f = MB_TIMEVAL_DIV(&diff, &unit_tm);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
363 move_units = floorl(move_units_f);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
364 dir = bullet->direction;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
365 x = bullet->start_map_x + move_adj[dir][0] * move_units;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
366 y = bullet->start_map_y + move_adj[dir][1] * move_units;
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
367
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
368 if(map[y][x] != MUD) {
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
369 mb_progm_abort(bullet->progm);
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
370 bullet_go_out_map(NULL, tank);
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
371 bullet_bang(bullet, x, y);
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
372 return;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
373 }
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
374
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
375 /*! \todo Move tanks into a list. */
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
376 for(i = 0; i < tank_rt->n_enemy; i++) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
377 tank_hitted = tank_rt->tank_enemies[i];
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
378 if(tank_hitted == tank)
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
379 continue;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
380 if(tank_hitted->map_x == x &&
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
381 tank_hitted->map_y == y) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
382 mb_progm_abort(bullet->progm);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
383 bullet_go_out_map(NULL, tank);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
384 bullet_bang(bullet, x, y);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
385 return;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
386 }
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
387 }
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
388
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
389 if(tank_rt->tank1 != tank) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
390 tank_hitted = tank_rt->tank1;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
391 if(tank_hitted->map_x == x &&
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
392 tank_hitted->map_y == y) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
393 mb_progm_abort(bullet->progm);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
394 bullet_go_out_map(NULL, tank);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
395 bullet_bang(bullet, x, y);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
396 return;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
397 }
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
398 }
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
399
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
400 if(tank_rt->tank2 != tank) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
401 tank_hitted = tank_rt->tank2;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
402 if(tank_hitted->map_x == x &&
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
403 tank_hitted->map_y == y) {
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
404 mb_progm_abort(bullet->progm);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
405 bullet_go_out_map(NULL, tank);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
406 bullet_bang(bullet, x, y);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
407 return;
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
408 }
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
409 }
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
410
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
411 MB_TIMEVAL_SET(&next, 0, 100000);
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
412 MB_TIMEVAL_ADD(&next, now);
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
413 bullet->hit_tmr = mb_timer_man_timeout(bullet->timer_man, &next,
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
414 bullet_hit_chk, arg);
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
415 }
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
416
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
417 /*! \brief To fire a bullet for a tank. */
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
418 void
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
419 tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) {
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
420 mb_rt_t *mb_rt;
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
421 redraw_man_t *rdman;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
422 int map_x, map_y;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
423 int shift_x, shift_y;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
424 int shift_len;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
425 int dir;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
426 tank_bullet_t *bullet;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
427 mb_progm_t *progm;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
428 mb_word_t *word;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
429 mb_action_t *act;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
430 mb_timeval_t start, playing;
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
431 mb_timeval_t now, next;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
432 mb_timer_man_t *timer_man;
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
433 subject_t *subject;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
434 static int map_xy_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
435
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
436 if(tank->bullet != NULL)
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
437 return;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
438
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
439 mb_rt = tank_rt->mb_rt;
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
440 rdman = mb_runtime_rdman(mb_rt);
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
441 timer_man = mb_runtime_timer_man(mb_rt);
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
442
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
443 dir = tank->direction;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
444 map_x = tank->map_x + map_xy_adj[dir][0];
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
445 map_y = tank->map_y + map_xy_adj[dir][1];
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
446 switch(dir) {
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
447 case TD_UP:
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
448 shift_len = map_y + 1;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
449 shift_x = 0;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
450 shift_y = -shift_len * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
451 break;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
452 case TD_RIGHT:
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
453 shift_len = 16 - map_x;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
454 shift_x = shift_len * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
455 shift_y = 0;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
456 break;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
457 case TD_DOWN:
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
458 shift_len = 12 - map_y;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
459 shift_x = 0;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
460 shift_y = shift_len * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
461 break;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
462 case TD_LEFT:
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
463 shift_len = map_x + 1;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
464 shift_x = -shift_len * 50;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
465 shift_y = 0;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
466 break;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
467 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
468
157
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
469 if(shift_len <= 0)
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
470 return;
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
471
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
472 tank->bullet = tank_bullet_new(rdman, map_x, map_y, dir);
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
473 bullet = tank->bullet;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
474 bullet->timer_man = timer_man;
157
5cd12609a5c7 Make sure bullet is fired with positive shift length.
Thinker K.F. Li <thinker@branda.to>
parents: 156
diff changeset
475
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
476 progm = mb_progm_new(2, rdman);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
477 MB_TIMEVAL_SET(&start, 0, 0);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
478 MB_TIMEVAL_SET(&playing, shift_len / 4, (shift_len % 4) * 250000);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
479 word = mb_progm_next_word(progm, &start, &playing);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
480 act = mb_shift_new(shift_x, shift_y, bullet->coord_pos, word);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
481 bullet->progm = progm;
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
482
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
483 /*! \todo Simplify the procdure of using observer pattern. */
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
484 subject = mb_progm_get_complete(progm);
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 190
diff changeset
485 subject_add_observer(subject, bullet_go_out_map, tank);
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
486
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
487 get_now(&now);
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
488 MB_TIMEVAL_CP(&bullet->start_time, &now);
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
489 mb_progm_start(progm, timer_man, &now);
155
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
490
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
491 MB_TIMEVAL_SET(&next, 0, 100000);
6749f6639924 Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
492 MB_TIMEVAL_ADD(&next, &now);
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
493 bullet->hit_tmr = mb_timer_man_timeout(timer_man, &next,
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
494 bullet_hit_chk, tank);
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
495 }
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
496
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
497 /* @} */
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
498
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
499 /*! \ingroup tank
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
500 * @{
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
501 */
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
502 #define CHANGE_POS(g, x, y) do { \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
503 (g)->root_coord->matrix[0] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
504 (g)->root_coord->matrix[2] = x; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
505 (g)->root_coord->matrix[4] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
506 (g)->root_coord->matrix[5] = y; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
507 rdman_coord_changed(rdman, (g)->root_coord); \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
508 } while(0)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
509
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
510 static void keyboard_handler(event_t *event, void *arg) {
1112
1993e5ae60aa Rename X_kb_event_t to mb_kb_event_t
Thinker K.F. Li <thinker@codemud.net>
parents: 1102
diff changeset
511 mb_kb_event_t *xkey = (mb_kb_event_t *)event;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
512 tank_rt_t *tank_rt = (tank_rt_t *)arg;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
513 int direction;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
514
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
515 if(xkey->event.type != EVT_KB_PRESS)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
516 return;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
517
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
518 switch(xkey->sym) {
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
519 case 0xff51: /* left */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
520 direction = TD_LEFT;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
521 tank_move(tank_rt->tank1, direction, tank_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
522 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
523
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
524 case 0xff52: /* up */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
525 direction = TD_UP;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
526 tank_move(tank_rt->tank1, direction, tank_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
527 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
528
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
529 case 0xff53: /* right */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
530 direction = TD_RIGHT;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
531 tank_move(tank_rt->tank1, direction, tank_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
532 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
533
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
534 case 0xff54: /* down */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
535 direction = TD_DOWN;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
536 tank_move(tank_rt->tank1, direction, tank_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
537 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
538
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
539 case 0x20: /* space */
154
6ce68c1f7405 Tank can fire bullet.
Thinker K.F. Li <thinker@branda.to>
parents: 153
diff changeset
540 tank_fire_bullet(tank_rt, tank_rt->tank1);
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
541 break;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
542 case 0xff0d: /* enter */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
543 default:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
544 return;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
545 }
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
546
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
547 }
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
548
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
549 static void init_keyboard(tank_rt_t *tank_rt) {
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
550 mb_rt_t *mb_rt;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
551 subject_t *kbevents;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
552 redraw_man_t *rdman;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
553
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
554 mb_rt = tank_rt->mb_rt;
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
555 kbevents = mb_runtime_kbevents(mb_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
556
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
557 rdman = mb_runtime_rdman(mb_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
558
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
559 tank_rt->kb_observer =
192
54fdc2a65242 Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents: 190
diff changeset
560 subject_add_observer(kbevents, keyboard_handler, tank_rt);
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
561 }
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
562
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
563 /*! \brief Make coord objects to decorate elfs (tanks).
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
564 *
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
565 * These coords are used to shift (move) and rotate decorated graphic.
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
566 * The coords can easy work of programmer to manipulate geometry of
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
567 * decorated graphic.
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
568 */
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
569 static void make_elf_coords(redraw_man_t *rdman, coord_t **coord_pos,
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
570 coord_t **coord_rot, coord_t **coord_center) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
571 coord_t *coord_back;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
572
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
573 *coord_pos = rdman_coord_new(rdman, rdman->root_coord);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
574
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
575 coord_back = rdman_coord_new(rdman, *coord_pos);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
576 coord_back->matrix[2] = 25;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
577 coord_back->matrix[5] = 25;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
578 rdman_coord_changed(rdman, coord_back);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
579
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
580 *coord_rot = rdman_coord_new(rdman, coord_back);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
581
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
582 *coord_center = rdman_coord_new(rdman, *coord_rot);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
583 (*coord_center)->matrix[2] = -25;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
584 (*coord_center)->matrix[5] = -25;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
585 rdman_coord_changed(rdman, *coord_center);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
586 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
587
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
588 void
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
589 initial_tank(tank_rt_t *tank_rt, mb_rt_t *mb_rt) {
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
590 redraw_man_t *rdman;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
591 /* for map areas */
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
592 mud_t *mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
593 brick_t *brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
594 rock_t *rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
595 bush_t *bush;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
596 /* for tanks */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
597 coord_t *coord_center, *coord_pos, *coord_rot;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
598 tank1_t *tank1_o;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
599 tank2_t *tank2_o;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
600 tank_en_t *tank_en_o;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
601 int i, j;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
602
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
603 rdman = mb_runtime_rdman(mb_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
604
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
605 tank_rt->mb_rt = mb_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
606 for(i = 0; i < 12; i++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
607 for(j = 0; j < 16; j++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
608 switch(map[i][j]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
609 case MUD:
130
3a4d6179e6a9 change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents: 129
diff changeset
610 mud = mud_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
611 CHANGE_POS(mud, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
612 tank_rt->map[i][j] = (void *)mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
613 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
614 case BRI:
130
3a4d6179e6a9 change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents: 129
diff changeset
615 brick = brick_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
616 CHANGE_POS(brick, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
617 tank_rt->map[i][j] = (void *)brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
618 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
619 case ROC:
130
3a4d6179e6a9 change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents: 129
diff changeset
620 rock = rock_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
621 CHANGE_POS(rock, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
622 tank_rt->map[i][j] = (void *)rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
623 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
624 case BSH:
130
3a4d6179e6a9 change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents: 129
diff changeset
625 bush = bush_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
626 CHANGE_POS(bush, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
627 tank_rt->map[i][j] = (void *)bush;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
628 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
629 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
630 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
631 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
632
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
633 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
634 tank1_o = tank1_new(rdman, coord_center);
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
635 tank_rt->tank1 = tank_new(coord_pos, coord_rot, 5, 11, tank_rt);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
636 tank_rt->tank1_o = tank1_o;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
637
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
638 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
639 tank2_o = tank2_new(rdman, coord_center);
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
640 tank_rt->tank2 = tank_new(coord_pos, coord_rot, 10, 11, tank_rt);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
641 tank_rt->tank2_o = tank2_o;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
642
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
643 for(i = 0; i < 3; i++) {
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
644 make_elf_coords(rdman, &coord_pos, &coord_rot, &coord_center);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
645 tank_en_o = tank_en_new(rdman, coord_center);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
646 tank_rt->tank_enemies[i] = tank_new(coord_pos, coord_rot,
189
257af0ed5852 When a bullet hits a tank or wall, it shows a bang animation.
Thinker K.F. Li <thinker@branda.to>
parents: 186
diff changeset
647 i * 3 + 3, 0, tank_rt);
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
648 tank_rt->tank_enemies_o[i] = tank_en_o;
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
649 tank_rt->tanks[i] = tank_rt->tank_enemies[i];
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
650 }
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
651 tank_rt->n_enemy = i;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
652
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
653 tank_rt->tanks[i++] =tank_rt->tank1;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
654 tank_rt->tanks[i++] =tank_rt->tank2;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
655 tank_rt->n_tanks = i;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
656
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
657 init_keyboard(tank_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
658 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
659
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
660 int
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
661 main(int argc, char *const argv[]) {
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
662 mb_rt_t *rt;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
663 tank_rt_t tank_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
664
1114
7451af5d63ec Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents: 1112
diff changeset
665 #ifdef CONSOLE_BACKEND
7451af5d63ec Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents: 1112
diff changeset
666 rt = mb_runtime_new(NULL, 800, 600);
7451af5d63ec Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents: 1112
diff changeset
667 #else
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
668 rt = mb_runtime_new(":0.0", 800, 600);
1114
7451af5d63ec Make tank and calculator works for console backend
Thinker K.F. Li <thinker@codemud.net>
parents: 1112
diff changeset
669 #endif
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
670
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
671 initial_tank(&tank_rt, rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
672
1102
dbea3e42bf93 Initial random number generator for tank
Thinker K.F. Li <thinker@codemud.net>
parents: 1100
diff changeset
673 init_enemies(&tank_rt);
1100
fb79175e6cc3 Add AI for enemy tanks
Thinker K.F. Li <thinker@codemud.net>
parents: 1060
diff changeset
674
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
675 mb_runtime_event_loop(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
676
1027
c693cd8c6e23 Migrate examples/tank to new definition of backend
Thinker K.F. Li <thinker@codemud.net>
parents: 195
diff changeset
677 mb_runtime_free(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
678 }
190
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
679
0a924eb9ccab Group and document code of example tank.
Thinker K.F. Li <thinker@branda.to>
parents: 189
diff changeset
680 /* @} */