Mercurial > MadButterfly
annotate examples/tank/tank_main.c @ 152:2b316b5d65f9
Refactory code snippets for making coords dirty.
add_dirty_coord() is the function to making coords dirty.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Fri, 26 Sep 2008 17:56:08 +0800 |
parents | c65b30e2eda9 |
children | 9870b049b7f6 |
rev | line source |
---|---|
120 | 1 #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
|
2 #include <string.h> |
115 | 3 #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
|
4 #include <mb/tools.h> |
115 | 5 #include "svgs.h" |
6 | |
7 enum { MUD, ROC, BRI, BSH }; | |
8 | |
9 static char map[12][16] = { | |
10 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
11 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
12 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, | |
13 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, | |
14 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, | |
15 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD}, | |
16 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
17 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
18 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI, | |
19 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD}, | |
20 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD, | |
21 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
22 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
23 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD}, | |
24 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
25 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD}, | |
26 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD, | |
27 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI}, | |
28 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
29 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD}, | |
30 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, | |
31 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, | |
32 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
33 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} | |
34 }; | |
35 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
36 #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
|
37 #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
|
38 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
39 /*! \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
|
40 * \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
|
41 * @{ |
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 struct _tank { |
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_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
|
45 coord_t *coord_rot; /*!< \brief coordinate for rotation */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
46 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
|
47 int direction; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
48 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
|
49 }; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
50 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
|
51 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
|
52 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
53 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
54 /* @} */ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
55 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
56 typedef struct _tank_rt tank_rt_t; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
57 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
58 struct _tank_rt { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
59 tank_t *tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
60 tank1_t *tank1_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
61 tank_t *tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
62 tank2_t *tank2_o; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
63 int n_enemy; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
64 tank_t *tank_enemies[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
65 tank_en_t *tank_enemies_o[10]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
66 tank_t *tanks[12]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
67 int n_tanks; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
68 void *map[12][16]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
69 X_MB_runtime_t *mb_rt; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
70 observer_t *kb_observer; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
71 }; |
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 /*! \ingroup tank_elf |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
74 * @{ |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
75 */ |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
83 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
|
84 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
|
85 return NULL; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
86 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
87 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
|
88 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
103 return tank; |
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 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
106 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
|
107 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
|
108 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
109 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
|
110 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
|
111 mb_progm_abort(tank->progm, tman); |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
112 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
113 free(tank); |
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 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
116 /*! \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
|
117 * |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
118 * 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
|
119 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
120 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
|
121 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
|
122 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
123 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
|
124 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
|
125 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
126 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
127 #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
|
128 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
129 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
|
130 tank_rt_t *tank_rt) { |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 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
|
135 /* 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
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 /* for position */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
142 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
|
143 /* for direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
144 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
|
145 float rot_diff; |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
153 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
|
154 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
155 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
156 /* |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
157 * 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
|
158 */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
159 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
|
160 switch(direction) { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
161 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
|
162 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
|
163 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
164 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
|
165 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
166 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
167 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
|
168 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
|
169 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
170 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
|
171 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
172 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
173 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
|
174 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
|
175 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
176 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
|
177 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
178 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
179 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
|
180 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
|
181 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
182 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
|
183 return; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
184 break; |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
185 } |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
186 |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
187 tank->map_x += map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
188 tank->map_y += map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
189 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
|
190 if(tank != tank_rt->tanks[i] && |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
191 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
|
192 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
|
193 tank->map_x -= map_shift[direction][0]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
194 tank->map_y -= map_shift[direction][1]; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
195 return; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
196 } |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
197 } |
131
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 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
|
201 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
|
202 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
|
203 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
204 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
|
205 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
|
206 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
207 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
|
208 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
|
209 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
|
210 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
211 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
|
212 /* Shift/move */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 } else { |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
217 /* Change direction */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
224 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
225 /* 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
|
226 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
|
227 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
|
228 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
|
229 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
230 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
|
231 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
|
232 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
233 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
234 /* @} */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
235 |
115 | 236 #define CHANGE_POS(g, x, y) do { \ |
237 (g)->root_coord->matrix[0] = 1.0; \ | |
238 (g)->root_coord->matrix[2] = x; \ | |
239 (g)->root_coord->matrix[4] = 1.0; \ | |
240 (g)->root_coord->matrix[5] = y; \ | |
241 rdman_coord_changed(rdman, (g)->root_coord); \ | |
242 } while(0) | |
243 | |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
244 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
|
245 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
|
246 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
|
247 int direction; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
248 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
249 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
|
250 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
251 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
252 switch(xkey->sym) { |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
253 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
|
254 direction = TD_LEFT; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
255 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
256 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
257 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
|
258 direction = TD_UP; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
259 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
260 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
261 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
|
262 direction = TD_RIGHT; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
263 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
264 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
265 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
|
266 direction = TD_DOWN; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
267 break; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
268 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
269 case 0x20: /* space */ |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
270 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
|
271 default: |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
272 return; |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
273 } |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
274 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
275 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
|
276 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
277 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
278 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
|
279 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
|
280 subject_t *kbevents; |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
281 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
|
282 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
|
283 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
284 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
|
285 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
|
286 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
287 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
|
288 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
|
289 |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
290 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
|
291 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
|
292 } |
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
293 |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
294 /*! \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
|
295 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
|
296 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
|
297 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
|
298 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
299 *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
|
300 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
301 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
|
302 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
|
303 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
|
304 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
|
305 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
306 *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
|
307 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
308 *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
|
309 (*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
|
310 (*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
|
311 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
|
312 } |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
313 |
115 | 314 void |
315 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) { | |
316 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
|
317 /* for map areas */ |
115 | 318 mud_t *mud; |
319 brick_t *brick; | |
320 rock_t *rock; | |
321 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
|
322 /* for tanks */ |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 tank_en_t *tank_en_o; |
115 | 327 int i, j; |
328 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
329 rdman = X_MB_rdman(mb_rt); |
115 | 330 |
331 tank_rt->mb_rt = mb_rt; | |
332 for(i = 0; i < 12; i++) { | |
333 for(j = 0; j < 16; j++) { | |
334 switch(map[i][j]) { | |
335 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
|
336 mud = mud_new(rdman, rdman->root_coord); |
115 | 337 CHANGE_POS(mud, j * 50, i * 50); |
338 tank_rt->map[i][j] = (void *)mud; | |
339 break; | |
340 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
|
341 brick = brick_new(rdman, rdman->root_coord); |
115 | 342 CHANGE_POS(brick, j * 50, i * 50); |
343 tank_rt->map[i][j] = (void *)brick; | |
344 break; | |
345 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
|
346 rock = rock_new(rdman, rdman->root_coord); |
115 | 347 CHANGE_POS(rock, j * 50, i * 50); |
348 tank_rt->map[i][j] = (void *)rock; | |
349 break; | |
350 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
|
351 bush = bush_new(rdman, rdman->root_coord); |
115 | 352 CHANGE_POS(bush, j * 50, i * 50); |
353 tank_rt->map[i][j] = (void *)bush; | |
354 break; | |
355 } | |
356 } | |
357 } | |
358 | |
131
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
359 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
|
360 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
|
361 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
|
362 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
|
363 |
6a8588df68af
Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
364 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
|
365 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
|
366 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
|
367 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
|
368 |
117 | 369 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
|
370 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
|
371 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
|
372 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
|
373 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
|
374 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
|
375 tank_rt->tanks[i] = tank_rt->tank_enemies[i]; |
117 | 376 } |
377 tank_rt->n_enemy = i; | |
120 | 378 |
132
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
379 tank_rt->tanks[i++] =tank_rt->tank1; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
380 tank_rt->tanks[i++] =tank_rt->tank2; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
381 tank_rt->n_tanks = i; |
c65b30e2eda9
detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents:
131
diff
changeset
|
382 |
129
ba581d8a4b9b
Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
383 init_keyboard(tank_rt); |
115 | 384 } |
385 | |
386 int | |
387 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
|
388 X_MB_runtime_t *rt; |
115 | 389 tank_rt_t tank_rt; |
390 | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
391 rt = X_MB_new(":0.0", 800, 600); |
115 | 392 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
393 initial_tank(&tank_rt, rt); |
115 | 394 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
395 X_MB_handle_connection(rt); |
115 | 396 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
120
diff
changeset
|
397 X_MB_free(rt); |
115 | 398 } |