annotate examples/tank/tank_main.c @ 153:9870b049b7f6

Make mb_progm_abort() work.
author Thinker K.F. Li <thinker@branda.to>
date Sat, 27 Sep 2008 02:40:42 +0800
parents c65b30e2eda9
children 6ce68c1f7405
rev   line source
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "svgs.h"
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 enum { MUD, ROC, BRI, BSH };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 static char map[12][16] = {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD}
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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);
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
111 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
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
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
236 #define CHANGE_POS(g, x, y) do { \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
237 (g)->root_coord->matrix[0] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
238 (g)->root_coord->matrix[2] = x; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
239 (g)->root_coord->matrix[4] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
240 (g)->root_coord->matrix[5] = y; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
241 rdman_coord_changed(rdman, (g)->root_coord); \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
242 } while(0)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
255 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
256 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
257
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
258 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
259 direction = TD_UP;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
260 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
261 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
262
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
263 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
264 direction = TD_RIGHT;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
265 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
266 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
267
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
268 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
269 direction = TD_DOWN;
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
270 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
271 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
272
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
273 case 0x20: /* space */
153
9870b049b7f6 Make mb_progm_abort() work.
Thinker K.F. Li <thinker@branda.to>
parents: 132
diff changeset
274 break;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
275 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
276 default:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
277 return;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
278 }
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
279
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
280 }
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
281
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
282 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
283 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
284 subject_t *kbevents;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
285 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
286 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
287
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
288 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
289 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
290
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
291 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
292 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
293
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
294 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
295 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
296 }
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
297
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
298 /*! \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
299 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
300 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
301 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
302
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
303 *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
304
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
305 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
306 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
307 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
308 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
309
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
310 *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
311
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
312 *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
313 (*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
314 (*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
315 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
316 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
317
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
318 void
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
319 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
320 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
321 /* for map areas */
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
322 mud_t *mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
323 brick_t *brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
324 rock_t *rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
325 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
326 /* for tanks */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
327 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
328 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
329 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
330 tank_en_t *tank_en_o;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
331 int i, j;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
332
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
333 rdman = X_MB_rdman(mb_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
334
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
335 tank_rt->mb_rt = mb_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
336 for(i = 0; i < 12; i++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
337 for(j = 0; j < 16; j++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
338 switch(map[i][j]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
339 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
340 mud = mud_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
341 CHANGE_POS(mud, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
342 tank_rt->map[i][j] = (void *)mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
343 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
344 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
345 brick = brick_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
346 CHANGE_POS(brick, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
347 tank_rt->map[i][j] = (void *)brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
348 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
349 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
350 rock = rock_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
351 CHANGE_POS(rock, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
352 tank_rt->map[i][j] = (void *)rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
353 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
354 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
355 bush = bush_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
356 CHANGE_POS(bush, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
357 tank_rt->map[i][j] = (void *)bush;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
358 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
359 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
360 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
361 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
362
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
363 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
364 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
365 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
366 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
367
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
368 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
369 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
370 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
371 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
372
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
373 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
374 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
375 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
376 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
377 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
378 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
379 tank_rt->tanks[i] = tank_rt->tank_enemies[i];
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
380 }
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
381 tank_rt->n_enemy = i;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
382
132
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
383 tank_rt->tanks[i++] =tank_rt->tank1;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
384 tank_rt->tanks[i++] =tank_rt->tank2;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
385 tank_rt->n_tanks = i;
c65b30e2eda9 detect collison between tanks
Thinker K.F. Li <thinker@branda.to>
parents: 131
diff changeset
386
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
387 init_keyboard(tank_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
388 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
389
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
390 int
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
391 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
392 X_MB_runtime_t *rt;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
393 tank_rt_t tank_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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 rt = X_MB_new(":0.0", 800, 600);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
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 initial_tank(&tank_rt, rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
398
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
399 X_MB_handle_connection(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
400
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
401 X_MB_free(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
402 }