1 /* $Id: wall-11.c,v 1.8 2004/12/12 16:13:15 oohara Exp $ */
2 
3 #include <stdio.h>
4 /* malloc, rand */
5 #include <stdlib.h>
6 
7 #include "const.h"
8 #include "tenm_object.h"
9 #include "tenm_graphic.h"
10 #include "tenm_primitive.h"
11 #include "util.h"
12 #include "tenm_table.h"
13 #include "tenm_math.h"
14 #include "normal-shot.h"
15 
16 #include "wall-11.h"
17 
18 #define NEAR_ZERO 0.0001
19 
20 static int wall_11_move(tenm_object *my, double turn_per_frame);
21 static int wall_11_act(tenm_object *my, const tenm_object *player);
22 static int wall_11_draw(tenm_object *my, int priority);
23 
24 tenm_object *
wall_11_new(double x,double y,double speed,int theta,int t_shoot)25 wall_11_new(double x, double y, double speed, int theta, int t_shoot)
26 {
27   tenm_primitive **p = NULL;
28   tenm_object *new = NULL;
29   int *count = NULL;
30   double *count_d = NULL;
31   int i;
32   double result[2];
33   double v[2];
34   double c[10];
35   double d[10];
36 
37   /* sanity check */
38   if (speed < NEAR_ZERO)
39   {
40     fprintf(stderr, "wall_11_new: speed is too small (%f)\n", speed);
41     return NULL;
42   }
43   if (t_shoot <= 0)
44   {
45     fprintf(stderr, "wall_11_new: t_shoot is non-positive (%d)\n", t_shoot);
46     return NULL;
47   }
48 
49   c[0] = 30.0;
50   c[1] = 0.0;
51   c[2] = 20.0;
52   c[3] = 10.0;
53   c[4] = -10.0;
54   c[5] = 10.0;
55   c[6] = -10.0;
56   c[7] = -10.0;
57   c[8] = 20.0;
58   c[9] = -10.0;
59 
60   for (i = 0; i < 5; i++)
61   {
62     v[0] = c[i * 2 + 0];
63     v[1] = c[i * 2 + 1];
64     result[0] = v[0];
65     result[1] = v[1];
66     vector_rotate(result, v, theta);
67     d[i * 2 + 0] = result[0];
68     d[i * 2 + 1] = result[1];
69   }
70 
71   p = (tenm_primitive **) malloc(sizeof(tenm_primitive *) * 1);
72   if (p == NULL)
73   {
74     fprintf(stderr, "wall_11_new: malloc(p) failed\n");
75     return NULL;
76   }
77 
78   p[0] = (tenm_primitive *) tenm_polygon_new(5,
79                                              x + d[0], y + d[1],
80                                              x + d[2], y + d[3],
81                                              x + d[4], y + d[5],
82                                              x + d[6], y + d[7],
83                                              x + d[8], y + d[9]);
84   if (p[0] == NULL)
85   {
86     fprintf(stderr, "wall_11_new: cannot set p[0]\n");
87     free(p);
88     return NULL;
89   }
90 
91   count = (int *) malloc(sizeof(int) * 4);
92   if (count == NULL)
93   {
94     fprintf(stderr, "wall_11_new: malloc(count) failed\n");
95     (p[0])->delete(p[0]);
96     free(p);
97     return NULL;
98   }
99   count_d = (double *) malloc(sizeof(double) * 12);
100   if (count_d == NULL)
101   {
102     fprintf(stderr, "wall_11_new: malloc(count_d) failed\n");
103     free(count);
104     (p[0])->delete(p[0]);
105     free(p);
106     return NULL;
107   }
108 
109   /* list of count
110    * [0] timer
111    * [1] escape time
112    * [2] shoot time
113    * [3] theta
114    */
115   /* list of count_d
116    * [0] speed x
117    * [1] speed y
118    * [2 -- 11] mass vertexes
119    */
120 
121   count[0] = 0;
122   count[1] = ((int) (900.0 / speed)) + 1;
123   if (count[1] < 1)
124     count[1] = 1;
125   count[2] = t_shoot;
126   count[3] = theta;
127 
128   count_d[0] = speed * tenm_cos(theta);
129   count_d[1] = speed * tenm_sin(theta);
130   for (i = 0; i < 10; i++)
131     count_d[2 + i] = d[i];
132 
133   new = tenm_object_new("wall 11", ATTR_OBSTACLE, 0,
134                         1, x, y,
135                         4, count, 12, count_d, 1, p,
136                         (int (*)(tenm_object *, double))
137                         (&wall_11_move),
138                         (int (*)(tenm_object *, tenm_object *))
139                         NULL,
140                         (int (*)(tenm_object *, const tenm_object *))
141                         (&wall_11_act),
142                         (int (*)(tenm_object *, int))
143                         (&wall_11_draw));
144 
145   if (new == NULL)
146   {
147     fprintf(stderr, "wall_11_new: tenm_object_new failed\n");
148     if (count_d != NULL)
149       free(count_d);
150     if (count != NULL)
151       free(count);
152     (p[0])->delete(p[0]);
153     free(p);
154     return NULL;
155   }
156 
157   return new;
158 }
159 
160 static int
wall_11_move(tenm_object * my,double turn_per_frame)161 wall_11_move(tenm_object *my, double turn_per_frame)
162 {  double dx_temp;
163   double dy_temp;
164 
165   /* sanity check */
166   if (my == NULL)
167   {
168     fprintf(stderr, "wall_11_move: my is NULL\n");
169     return 0;
170   }
171   if (turn_per_frame <= 0.5)
172   {
173     fprintf(stderr, "wall_11_move: strange turn_per_frame (%f)\n",
174             turn_per_frame);
175     return 0;
176   }
177 
178   dx_temp = my->count_d[0] / turn_per_frame;
179   dy_temp = my->count_d[1] / turn_per_frame;
180   my->x += dx_temp;
181   my->y += dy_temp;
182   if (my->mass != NULL)
183     tenm_move_mass(my->mass, dx_temp, dy_temp);
184 
185   return 0;
186 }
187 
188 static int
wall_11_act(tenm_object * my,const tenm_object * player)189 wall_11_act(tenm_object *my, const tenm_object *player)
190 {
191   /* sanity check */
192   if (my == NULL)
193   {
194     fprintf(stderr, "wall_8_act: my is NULL\n");
195     return 0;
196   }
197   if (player == NULL)
198     return 0;
199 
200   (my->count[0])++;
201   if (my->count[0] >= my->count[1])
202     return 1;
203 
204   if (my->count[0] % my->count[2] == 0)
205   {
206     tenm_table_add(normal_shot_angle_new(my->x, my->y, 4.5,
207                                          my->count[3] + 150, 4));
208     tenm_table_add(normal_shot_angle_new(my->x, my->y, 4.5,
209                                          my->count[3] - 150, 4));
210   }
211 
212   return 0;
213 }
214 
215 static int
wall_11_draw(tenm_object * my,int priority)216 wall_11_draw(tenm_object *my, int priority)
217 {
218   int i;
219   int status = 0;
220   tenm_color color;
221 
222   /* sanity check */
223   if (my == NULL)
224   {
225     fprintf(stderr, "wall_11_draw: my is NULL\n");
226     return 0;
227   }
228 
229   if (priority != 0)
230     return 0;
231 
232   /* body */
233   color = tenm_map_color(95, 13, 68);
234 
235   for (i = 0; i < 5; i++)
236   {
237     if (tenm_draw_line(((int) (my->x + my->count_d[2 + (i * 2 + 0) % 10])),
238                        ((int) (my->y + my->count_d[2 + (i * 2 + 1) % 10])),
239                        ((int) (my->x + my->count_d[2 + (i * 2 + 2) % 10])),
240                        ((int) (my->y + my->count_d[2 + (i * 2 + 3) % 10])),
241                        3, color) != 0)
242       status = 1;
243   }
244 
245   return status;
246 }
247