annotate examples/tank/tank_main.c @ 131:6a8588df68af

Tank can change direction and navigate on the mud area
author Thinker K.F. Li <thinker@branda.to>
date Wed, 17 Sep 2008 01:30:25 +0800
parents 3a4d6179e6a9
children c65b30e2eda9
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
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
53 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
54 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
55 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
56 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
57 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
58 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
59
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
60 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
61 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
62 return NULL;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
63
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
64 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
65
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
66 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
67 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
68 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
69 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
70 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
71 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
72
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
73 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
74 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
75 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
76 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
77 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
78 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
79
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
80 return tank;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
81 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
82
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
83 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
84 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
85
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
86 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
87 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
88 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
89 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
90 free(tank);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
91 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
92
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
93 /*! \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
94 *
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
95 * 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
96 */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
97 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
98 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
99
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
100 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
101 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
102 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
103
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
104 #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
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_move(tank_t *tank, int direction,
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
107 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
108 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
109 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
110 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
111 /* 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
112 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
113 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
114 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
115 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
116 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
117 /* for position */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
118 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
119 /* for direction */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
120 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
121 float rot_diff;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
122 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
123 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
124 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
125 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
126 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
127
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
128 if(tank->progm != NULL)
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
129 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
130
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
131 /*
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
132 * 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
133 */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
134 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
135 switch(direction) {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
136 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
137 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
138 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
139 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
140 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
141 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
142 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
143 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
144 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
145 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
146 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
147 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
148 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
149 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
150 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
151 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
152 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
153 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
154 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
155 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
156 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
157 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
158 return;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
159 break;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
160 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
161 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
162
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
163 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
164 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
165 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
166
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
167 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
168 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
169
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
170 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
171 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
172 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
173
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
174 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
175 /* Shift/move */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
176 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
177 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
178 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
179 tank->map_x += map_shift[direction][0];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
180 tank->map_y += map_shift[direction][1];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
181 } else {
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
182 /* Change direction */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
183 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
184 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
185 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
186 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
187 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
188 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
189
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
190 /* 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
191 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
192 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
193 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
194
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
195 get_now(&now);
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
196 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
197 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
198
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
199 /* @} */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
200
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
201 typedef struct _tank_rt tank_rt_t;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
202
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
203 struct _tank_rt {
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
204 tank_t *tank1;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
205 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
206 tank_t *tank2;
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
207 tank2_t *tank2_o;
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
208 int n_enemy;
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
209 tank_t *tank_enemies[10];
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
210 tank_en_t *tank_enemies_o[10];
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
211 void *map[12][16];
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
212 X_MB_runtime_t *mb_rt;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
213 observer_t *kb_observer;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
214 };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
215
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
216 #define CHANGE_POS(g, x, y) do { \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
217 (g)->root_coord->matrix[0] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
218 (g)->root_coord->matrix[2] = x; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
219 (g)->root_coord->matrix[4] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
220 (g)->root_coord->matrix[5] = y; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
221 rdman_coord_changed(rdman, (g)->root_coord); \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
222 } while(0)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
223
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
224 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
225 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
226 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
227 int direction;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
228
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
229 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
230 return;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
231
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
232 switch(xkey->sym) {
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
233 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
234 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
235 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
236
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
237 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
238 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
239 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
240
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
241 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
242 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
243 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
244
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
245 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
246 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
247 break;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
248
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
249 case 0x20: /* space */
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
250 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
251 default:
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
252 return;
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
253 }
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
254
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
255 tank_move(tank_rt->tank1, direction, tank_rt->mb_rt);
129
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
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
258 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
259 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
260 subject_t *kbevents;
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
261 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
262 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
263
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
264 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
265 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
266
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
267 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
268 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
269
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
270 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
271 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
272 }
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 /*! \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
275 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
276 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
277 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
278
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
279 *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
280
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
281 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
282 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
283 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
284 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
285
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
286 *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
287
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
288 *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
289 (*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
290 (*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
291 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
292 }
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
293
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
294 void
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
295 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
296 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
297 /* for map areas */
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
298 mud_t *mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
299 brick_t *brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
300 rock_t *rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
301 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
302 /* for tanks */
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
303 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
304 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
305 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
306 tank_en_t *tank_en_o;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
307 int i, j;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
308
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
309 rdman = X_MB_rdman(mb_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
310
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
311 tank_rt->mb_rt = mb_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
312 for(i = 0; i < 12; i++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
313 for(j = 0; j < 16; j++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
314 switch(map[i][j]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
315 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
316 mud = mud_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
317 CHANGE_POS(mud, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
318 tank_rt->map[i][j] = (void *)mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
319 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
320 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
321 brick = brick_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
322 CHANGE_POS(brick, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
323 tank_rt->map[i][j] = (void *)brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
324 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
325 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
326 rock = rock_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
327 CHANGE_POS(rock, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
328 tank_rt->map[i][j] = (void *)rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
329 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
330 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
331 bush = bush_new(rdman, rdman->root_coord);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
332 CHANGE_POS(bush, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
333 tank_rt->map[i][j] = (void *)bush;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
334 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
335 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
336 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
337 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
338
131
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
339 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
340 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
341 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
342 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
343
6a8588df68af Tank can change direction and navigate on the mud area
Thinker K.F. Li <thinker@branda.to>
parents: 130
diff changeset
344 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
345 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
346 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
347 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
348
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
349 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
350 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
351 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
352 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
353 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
354 tank_rt->tank_enemies_o[i] = tank_en_o;
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
355 }
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
356 tank_rt->n_enemy = i;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
357
129
ba581d8a4b9b Now, tank1 can be controlled by user with keyboard
Thinker K.F. Li <thinker@branda.to>
parents: 122
diff changeset
358 init_keyboard(tank_rt);
115
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 int
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
362 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
363 X_MB_runtime_t *rt;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
364 tank_rt_t tank_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
365
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
366 rt = X_MB_new(":0.0", 800, 600);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
367
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
368 initial_tank(&tank_rt, rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
369
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
370 X_MB_handle_connection(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
371
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
372 X_MB_free(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
373 }