Mercurial > MadButterfly
annotate examples/tank/tank_main.c @ 156:2aad042b30a4
Bullet would be detected to hit a wall (not mud).
1. It will hide bullet before remove it.
2. The program should not be removed until shapes are redrawed.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 01 Oct 2008 14:58:27 +0800 |
parents | 6749f6639924 |
children | 5cd12609a5c7 |
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 | 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> |
115 | 4 #include <mb/mb.h> |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
5 #include <mb/tools.h> |
115 | 6 #include "svgs.h" |
7 | |
8 enum { MUD, ROC, BRI, BSH }; | |
9 | |
10 static char map[12][16] = { | |
11 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
12 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
13 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, | |
14 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, | |
15 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, | |
16 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD}, | |
17 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
18 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
19 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI, | |
20 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD}, | |
21 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD, | |
22 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
23 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
24 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD}, | |
25 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
26 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD}, | |
27 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD, | |
28 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI}, | |
29 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
30 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD}, | |
31 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, | |
32 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, | |
33 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
34 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} | |
35 }; | |
36 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
37 #define MAP_W 16 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
38 #define MAP_H 12 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
39 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
40 /*! \defgroup tank_elf Tank Elf |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
41 * \brief Tank elf module provides control functions of tanks in game. |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
42 * @{ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
43 */ |
154 | 44 struct _tank_bullet { |
45 redraw_man_t *rdman; | |
46 coord_t *coord_pos; | |
47 coord_t *coord_rot; | |
48 bullet_t *bullet_obj; | |
49 int start_map_x, start_map_y; | |
50 int direction; | |
51 mb_progm_t *progm; | |
52 mb_timeval_t start_time; | |
53 observer_t *ob_redraw; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
54 mb_timer_t *hit_tmr; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
55 mb_tman_t *tman; |
154 | 56 }; |
57 typedef struct _tank_bullet tank_bullet_t; | |
58 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT }; | |
59 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
60 struct _tank { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
61 coord_t *coord_pos; /*!< \brief coordinate for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
62 coord_t *coord_rot; /*!< \brief coordinate for rotation */ |
154 | 63 coord_t *coord_center; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
64 int map_x, map_y; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
65 int direction; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
66 mb_progm_t *progm; |
154 | 67 tank_bullet_t *bullet; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
68 }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
69 typedef struct _tank tank_t; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
70 enum { TD_UP = 0, TD_RIGHT, TD_DOWN, TD_LEFT }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
71 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
72 /* @} */ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
73 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
74 typedef struct _tank_rt tank_rt_t; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
75 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
76 struct _tank_rt { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
77 tank_t *tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
78 tank1_t *tank1_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
79 tank_t *tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
80 tank2_t *tank2_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
81 int n_enemy; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
82 tank_t *tank_enemies[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
83 tank_en_t *tank_enemies_o[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
84 tank_t *tanks[12]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
85 int n_tanks; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
86 void *map[12][16]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
87 X_MB_runtime_t *mb_rt; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
88 observer_t *kb_observer; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
89 }; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
90 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
91 /*! \ingroup tank_elf |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
92 * @{ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
93 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
94 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
|
95 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
|
96 int map_x, int map_y, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
97 X_MB_runtime_t *mb_rt) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
98 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
|
99 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
|
100 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
101 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
|
102 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
|
103 return NULL; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
104 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
105 rdman = X_MB_rdman(mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
106 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 tank->progm = NULL; |
154 | 113 tank->bullet = NULL; |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
114 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
122 return tank; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
123 } |
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 static void tank_free(tank_t *tank, X_MB_runtime_t *xmb_rt) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
126 mb_tman_t *tman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
127 |
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->progm) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
129 tman = X_MB_tman(xmb_rt); |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
130 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
|
131 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
132 free(tank); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
133 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
134 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
135 /*! \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
|
136 * |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
137 * 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
|
138 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
139 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
|
140 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
|
141 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
142 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
|
143 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
|
144 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
145 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
146 #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
|
147 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
148 static void tank_move(tank_t *tank, int direction, |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
149 tank_rt_t *tank_rt) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
150 X_MB_runtime_t *xmb_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
|
151 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
|
152 mb_tman_t *tman; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
153 ob_factory_t *factory; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
154 /* 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 /* for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
161 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
|
162 /* for direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
163 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
|
164 float rot_diff; |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
172 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
|
173 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
174 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
175 /* |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
176 * 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
|
177 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
178 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
|
179 switch(direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
180 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
|
181 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
|
182 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
183 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
|
184 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
185 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
186 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
|
187 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
|
188 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
189 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
|
190 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
191 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
192 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
|
193 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
|
194 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
195 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
|
196 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
197 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
198 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
|
199 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
|
200 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
201 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
|
202 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
203 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
204 } |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
205 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
206 tank->map_x += map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
207 tank->map_y += map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
208 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
|
209 if(tank != tank_rt->tanks[i] && |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
210 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
|
211 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
|
212 tank->map_x -= map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
213 tank->map_y -= map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
214 return; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
215 } |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
216 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
217 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
218 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
219 rdman = X_MB_rdman(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
220 tman = X_MB_tman(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
221 factory = X_MB_ob_factory(xmb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
222 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
223 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
|
224 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
|
225 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
230 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
|
231 /* Shift/move */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
232 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
|
233 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
|
234 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
|
235 } else { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
236 /* Change direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
243 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
244 /* 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
|
245 comp_sub = mb_progm_get_complete(progm); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
246 subject_add_observer(factory, comp_sub, |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
247 clean_tank_progm_handler, tank); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
248 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
249 get_now(&now); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
250 mb_progm_start(progm, tman, &now); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
251 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
252 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
253 /* @} */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
254 |
154 | 255 /*! \brief Make coord objects for bullet elfs. */ |
256 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, | |
257 coord_t **coord_rot, | |
258 coord_t **coord_center) { | |
259 coord_t *coord_back; | |
260 | |
261 *coord_pos = rdman_coord_new(rdman, rdman->root_coord); | |
262 | |
263 coord_back = rdman_coord_new(rdman, *coord_pos); | |
264 coord_back->matrix[2] = 25; | |
265 coord_back->matrix[5] = 25; | |
266 rdman_coord_changed(rdman, coord_back); | |
267 | |
268 *coord_rot = rdman_coord_new(rdman, coord_back); | |
269 | |
270 *coord_center = rdman_coord_new(rdman, *coord_rot); | |
271 (*coord_center)->matrix[2] = -5; | |
272 (*coord_center)->matrix[5] = +15; | |
273 rdman_coord_changed(rdman, *coord_center); | |
274 } | |
275 | |
276 static tank_bullet_t *tank_bullet_new(redraw_man_t *rdman, | |
277 int map_x, int map_y, | |
278 int direction) { | |
279 tank_bullet_t *bullet; | |
280 coord_t *coord_center; | |
281 co_aix *matrix; | |
282 static float _sins[] = { 0, 1, 0, -1}; | |
283 static float _coss[] = { 1, 0, -1, 0}; | |
284 float _sin, _cos; | |
285 | |
286 bullet = O_ALLOC(tank_bullet_t); | |
287 bullet->rdman = rdman; | |
288 | |
289 make_bullet_elf_coords(rdman, &bullet->coord_pos, | |
290 &bullet->coord_rot, | |
291 &coord_center); | |
292 bullet->bullet_obj = bullet_new(rdman, coord_center); | |
293 | |
294 bullet->start_map_x = map_x; | |
295 bullet->start_map_y = map_y; | |
296 bullet->direction = direction; | |
297 bullet->progm = NULL; | |
298 | |
299 matrix = bullet->coord_pos->matrix; | |
300 matrix[2] = map_x * 50; | |
301 matrix[5] = map_y * 50; | |
302 rdman_coord_changed(rdman, bullet->coord_pos); | |
303 | |
304 _sin = _sins[direction]; | |
305 _cos = _coss[direction]; | |
306 matrix = bullet->coord_rot->matrix; | |
307 matrix[0] = _cos; | |
308 matrix[1] = -_sin; | |
309 matrix[3] = _sin; | |
310 matrix[4] = _cos; | |
311 | |
312 return bullet; | |
313 } | |
314 | |
315 static void tank_bullet_free(tank_bullet_t *bullet) { | |
316 bullet_free(bullet->bullet_obj); | |
317 rdman_coord_subtree_free(bullet->rdman, bullet->coord_pos); | |
318 } | |
319 | |
320 static void bullet_go_out_map_and_redraw(event_t *event, void *arg) { | |
321 tank_t *tank = (tank_t *)arg; | |
322 tank_bullet_t *bullet; | |
323 ob_factory_t *factory; | |
324 subject_t *redraw; | |
325 | |
326 bullet = tank->bullet; | |
327 mb_progm_free(bullet->progm); | |
328 rdman_force_clean(bullet->rdman); | |
329 factory = rdman_get_ob_factory(bullet->rdman); | |
330 redraw = rdman_get_redraw_subject(bullet->rdman); | |
331 subject_remove_observer(factory, redraw, bullet->ob_redraw); | |
332 tank_bullet_free(tank->bullet); | |
333 tank->bullet = NULL; | |
334 } | |
335 | |
336 static void bullet_go_out_map(event_t *event, void *arg) { | |
337 tank_t *tank = (tank_t *)arg; | |
338 tank_bullet_t *bullet; | |
339 redraw_man_t *rdman; | |
340 subject_t *redraw; | |
341 ob_factory_t *factory; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
342 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
343 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
|
344 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
|
345 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
346 if(bullet->hit_tmr != NULL) |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
347 mb_tman_remove(bullet->tman, bullet->hit_tmr); |
156
2aad042b30a4
Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents:
155
diff
changeset
|
348 |
2aad042b30a4
Bullet would be detected to hit a wall (not mud).
Thinker K.F. Li <thinker@branda.to>
parents:
155
diff
changeset
|
349 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
|
350 rdman_coord_changed(rdman, bullet->coord_pos); |
154 | 351 |
352 /*! \todo Simplify the procdure of using observer pattern. */ | |
353 factory = rdman_get_ob_factory(rdman); | |
354 redraw = rdman_get_redraw_subject(rdman); | |
355 bullet->ob_redraw = | |
356 subject_add_observer(factory, redraw, | |
357 bullet_go_out_map_and_redraw, tank); | |
358 } | |
359 | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
360 static void bullet_bang(tank_bullet_t *bullet, int map_x, int map_y) { |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
361 } |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
362 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
363 static void bullet_hit_chk(const mb_timeval_t *tmo, |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
364 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
|
365 void *arg) { |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
366 tank_t *tank = (tank_t *)arg; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 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
|
371 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
|
372 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
|
373 int dir; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
374 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
|
375 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
386 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
|
387 bullet->hit_tmr = NULL; |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
388 mb_progm_abort(bullet->progm); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
389 bullet_go_out_map(NULL, arg); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
390 bullet_bang(bullet, x, y); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
391 } else { |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
392 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
|
393 MB_TIMEVAL_ADD(&next, now); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
394 bullet->hit_tmr = mb_tman_timeout(bullet->tman, &next, |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
395 bullet_hit_chk, arg); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
396 } |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
397 } |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
398 |
154 | 399 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) { |
400 X_MB_runtime_t *xmb_rt; | |
401 redraw_man_t *rdman; | |
402 int map_x, map_y; | |
403 int shift_x, shift_y; | |
404 int shift_len; | |
405 int dir; | |
406 tank_bullet_t *bullet; | |
407 mb_progm_t *progm; | |
408 mb_word_t *word; | |
409 mb_action_t *act; | |
410 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
|
411 mb_timeval_t now, next; |
154 | 412 ob_factory_t *factory; |
413 mb_tman_t *tman; | |
414 subject_t *subject; | |
415 static int map_xy_adj[][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; | |
416 | |
417 if(tank->bullet != NULL) | |
418 return; | |
419 | |
420 xmb_rt = tank_rt->mb_rt; | |
421 rdman = X_MB_rdman(xmb_rt); | |
422 tman = X_MB_tman(xmb_rt); | |
423 | |
424 dir = tank->direction; | |
425 map_x = tank->map_x + map_xy_adj[dir][0]; | |
426 map_y = tank->map_y + map_xy_adj[dir][1]; | |
427 tank->bullet = tank_bullet_new(rdman, map_x, map_y, dir); | |
428 bullet = tank->bullet; | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
429 bullet->tman = tman; |
154 | 430 |
431 switch(dir) { | |
432 case TD_UP: | |
433 shift_len = map_y + 1; | |
434 shift_x = 0; | |
435 shift_y = -shift_len * 50; | |
436 break; | |
437 case TD_RIGHT: | |
438 shift_len = 16 - map_x; | |
439 shift_x = shift_len * 50; | |
440 shift_y = 0; | |
441 break; | |
442 case TD_DOWN: | |
443 shift_len = 12 - map_y; | |
444 shift_x = 0; | |
445 shift_y = shift_len * 50; | |
446 break; | |
447 case TD_LEFT: | |
448 shift_len = map_x + 1; | |
449 shift_x = -shift_len * 50; | |
450 shift_y = 0; | |
451 break; | |
452 } | |
453 | |
454 progm = mb_progm_new(2, rdman); | |
455 MB_TIMEVAL_SET(&start, 0, 0); | |
456 MB_TIMEVAL_SET(&playing, shift_len / 4, (shift_len % 4) * 250000); | |
457 word = mb_progm_next_word(progm, &start, &playing); | |
458 act = mb_shift_new(shift_x, shift_y, bullet->coord_pos, word); | |
459 bullet->progm = progm; | |
460 | |
461 subject = mb_progm_get_complete(progm); | |
462 factory = rdman_get_ob_factory(rdman); | |
463 subject_add_observer(factory, subject, bullet_go_out_map, tank); | |
464 | |
465 get_now(&now); | |
466 MB_TIMEVAL_CP(&bullet->start_time, &now); | |
467 mb_progm_start(progm, tman, &now); | |
155
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
468 |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
469 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
|
470 MB_TIMEVAL_ADD(&next, &now); |
6749f6639924
Fix bug for STAILQ that fail to remove a node.
Thinker K.F. Li <thinker@branda.to>
parents:
154
diff
changeset
|
471 bullet->hit_tmr = mb_tman_timeout(tman, &next, bullet_hit_chk, tank); |
154 | 472 } |
473 | |
115 | 474 #define CHANGE_POS(g, x, y) do { \ |
475 (g)->root_coord->matrix[0] = 1.0; \ | |
476 (g)->root_coord->matrix[2] = x; \ | |
477 (g)->root_coord->matrix[4] = 1.0; \ | |
478 (g)->root_coord->matrix[5] = y; \ | |
479 rdman_coord_changed(rdman, (g)->root_coord); \ | |
480 } while(0) | |
481 | |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
482 static void keyboard_handler(event_t *event, void *arg) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
483 X_kb_event_t *xkey = (X_kb_event_t *)event; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
484 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
|
485 int direction; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
486 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
487 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
|
488 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
489 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
490 switch(xkey->sym) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
491 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
|
492 direction = TD_LEFT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
493 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
|
494 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
495 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
496 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
|
497 direction = TD_UP; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
498 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
|
499 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
500 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
501 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
|
502 direction = TD_RIGHT; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
503 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
|
504 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
505 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
506 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
|
507 direction = TD_DOWN; |
153
9870b049b7f6
Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents:
132
diff
changeset
|
508 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
|
509 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
510 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
511 case 0x20: /* space */ |
154 | 512 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
|
513 break; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
514 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
|
515 default: |
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 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
518 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
519 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
520 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
521 static void init_keyboard(tank_rt_t *tank_rt) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
522 X_MB_runtime_t *mb_rt; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
523 subject_t *kbevents; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
524 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
|
525 ob_factory_t *factory; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
526 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
527 mb_rt = tank_rt->mb_rt; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
528 kbevents = X_MB_kbevents(mb_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
529 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
530 rdman = X_MB_rdman(mb_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
531 factory = rdman_get_ob_factory(rdman); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
532 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
533 tank_rt->kb_observer = |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
534 subject_add_observer(factory, kbevents, keyboard_handler, tank_rt); |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
535 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
536 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
537 /*! \brief Make coord objects for elfs (tanks). */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
538 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
|
539 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
|
540 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
|
541 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
542 *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
|
543 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
544 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
|
545 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
|
546 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
|
547 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
|
548 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
549 *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
|
550 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
551 *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
|
552 (*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
|
553 (*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
|
554 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
|
555 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
556 |
115 | 557 void |
558 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) { | |
559 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
|
560 /* for map areas */ |
115 | 561 mud_t *mud; |
562 brick_t *brick; | |
563 rock_t *rock; | |
564 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
|
565 /* for tanks */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
566 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
|
567 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
|
568 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
|
569 tank_en_t *tank_en_o; |
115 | 570 int i, j; |
571 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
572 rdman = X_MB_rdman(mb_rt); |
115 | 573 |
574 tank_rt->mb_rt = mb_rt; | |
575 for(i = 0; i < 12; i++) { | |
576 for(j = 0; j < 16; j++) { | |
577 switch(map[i][j]) { | |
578 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
|
579 mud = mud_new(rdman, rdman->root_coord); |
115 | 580 CHANGE_POS(mud, j * 50, i * 50); |
581 tank_rt->map[i][j] = (void *)mud; | |
582 break; | |
583 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
|
584 brick = brick_new(rdman, rdman->root_coord); |
115 | 585 CHANGE_POS(brick, j * 50, i * 50); |
586 tank_rt->map[i][j] = (void *)brick; | |
587 break; | |
588 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
|
589 rock = rock_new(rdman, rdman->root_coord); |
115 | 590 CHANGE_POS(rock, j * 50, i * 50); |
591 tank_rt->map[i][j] = (void *)rock; | |
592 break; | |
593 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
|
594 bush = bush_new(rdman, rdman->root_coord); |
115 | 595 CHANGE_POS(bush, j * 50, i * 50); |
596 tank_rt->map[i][j] = (void *)bush; | |
597 break; | |
598 } | |
599 } | |
600 } | |
601 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
602 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
|
603 tank1_o = tank1_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
|
604 tank_rt->tank1 = tank_new(coord_pos, coord_rot, 5, 11, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
605 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
|
606 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
607 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
|
608 tank2_o = tank2_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
|
609 tank_rt->tank2 = tank_new(coord_pos, coord_rot, 10, 11, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
610 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
|
611 |
117 | 612 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
|
613 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
|
614 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
|
615 tank_rt->tank_enemies[i] = tank_new(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
|
616 i * 3 + 3, 0, mb_rt); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
617 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
|
618 tank_rt->tanks[i] = tank_rt->tank_enemies[i]; |
117 | 619 } |
620 tank_rt->n_enemy = i; | |
120 | 621 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
622 tank_rt->tanks[i++] =tank_rt->tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
623 tank_rt->tanks[i++] =tank_rt->tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
624 tank_rt->n_tanks = i; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
625 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
626 init_keyboard(tank_rt); |
115 | 627 } |
628 | |
629 int | |
630 main(int argc, char *const argv[]) { | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
631 X_MB_runtime_t *rt; |
115 | 632 tank_rt_t tank_rt; |
633 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
634 rt = X_MB_new(":0.0", 800, 600); |
115 | 635 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
636 initial_tank(&tank_rt, rt); |
115 | 637 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
638 X_MB_handle_connection(rt); |
115 | 639 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
640 X_MB_free(rt); |
115 | 641 } |