0
|
1 /*
|
|
2 * $Id$
|
|
3 */
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include "libps2.h"
|
|
7 #include "ps2util.h"
|
|
8 #include "col.h"
|
|
9 #include "field.h"
|
|
10 #include "car.h"
|
|
11 #include "mytype.h"
|
|
12 #include "quotanion.h"
|
|
13 #include "game.h"
|
|
14
|
|
15 #define DEFAULT_SET (EFFECT_TEXTURE_USE | EFFECT_ALPHABLEND_UNUSE | EFFECT_SHADING_FLAT)
|
|
16 #define BUFSIZE 256
|
|
17
|
|
18 /* $B=i4|0LCV$H=i4|J}8~(B */
|
|
19 static FVECTOR location = {0, 0, 0, 1};
|
|
20 static FVECTOR direction = {0, 0, 1, 1};
|
|
21
|
|
22 extern FILE* main_fp;
|
|
23
|
|
24 /* --- field.c --- */
|
|
25 extern int field_rap_increment(int);
|
|
26 extern void field_set_actual(FieldPtr);
|
|
27 extern FieldPtr field_get_actual();
|
|
28
|
|
29 static int *check;
|
|
30 static int check_value;
|
|
31
|
|
32 static CarPtr
|
|
33 car_create(int car_id, char *filename, char *texname, float speed_accel,
|
|
34 float speed_max, float rot, float brake)
|
|
35 {
|
|
36 CarPtr car;
|
|
37 OBJECT *body;
|
|
38 TEXTURE* tex;
|
|
39 void *free_addr;
|
|
40
|
|
41 body = ps2util_obj_Create_fromXML(filename);
|
|
42 ps2util_obj_Set_effect(body, DEFAULT_SET);
|
|
43
|
|
44 if (malloc_align16(&free_addr, &car, sizeof(Car)) == -1) {
|
|
45 fprintf(main_fp, "car.c: malloc_align16 error\n");
|
|
46 exit(EXIT_FAILURE);
|
|
47 }
|
|
48 car->body = body;
|
|
49 car->next = NULL;
|
|
50 car->speed = 0.0;
|
|
51 car->speed_accel = speed_accel;
|
|
52 car->speed_max = speed_max;
|
|
53 car->brake = brake;
|
|
54 car->rotation_angle = rot;
|
|
55 car->y_angle = 0.0;
|
|
56 car->free_addr = free_addr;
|
|
57
|
|
58 ps2_vu0_copy_vector(car->direction, direction);
|
|
59 ps2_vu0_copy_vector(car->location, location);
|
|
60 INIT_VECTOR(car->vertical, 0, -1, 0, 1);
|
|
61 INIT_VECTOR(car->body->xyz, 0, 0, 0, 1);
|
|
62 INIT_VECTOR(car->body->angle, 0, 0, 0, 1);
|
|
63
|
|
64 ps2util_obj_Renew_transMatrix(car->body);
|
|
65 ps2util_obj_Set_effect(car->body, DEFAULT_SET);
|
|
66
|
|
67 if (*texname != 'n') {
|
|
68 tex = read_png_file(texname);
|
|
69 ps2util_tex_Set(tex);
|
|
70 ps2util_obj_Set_texture(car->body, tex);
|
|
71 }
|
|
72
|
|
73 #ifdef DEBUG
|
|
74 fprintf(main_fp, "malloc car addr = %x\n", (int)free_addr);
|
|
75 #endif
|
|
76
|
|
77 return car;
|
|
78 }
|
|
79
|
|
80 static CarPtr
|
|
81 car_new_readCSV(FILE *fp, int id)
|
|
82 {
|
|
83 CarPtr new;
|
|
84 int car_id;
|
|
85 float speed_accel, speed_max, brake, rot;
|
|
86 char buff[BUFSIZE], *bufp;
|
|
87 char carImg[BUFSIZE], texImg[BUFSIZE];
|
|
88 Bool flag;
|
|
89
|
|
90 flag = FALSE;
|
|
91
|
|
92 while ((bufp = fgets(buff, BUFSIZE, fp)) != NULL) {
|
|
93 bufp++;
|
|
94
|
|
95 switch (buff[0]) {
|
|
96 case 'n':
|
|
97 if (flag == FALSE) {
|
|
98 sscanf(bufp, " %d\n", &car_id);
|
|
99 if (id == car_id) {
|
|
100 flag = TRUE;
|
|
101 }
|
|
102 }
|
|
103 break;
|
|
104 case 't':
|
|
105 if (flag == TRUE) {
|
|
106 sscanf(bufp, " %s %s %f %f %f %f\n",
|
|
107 carImg, texImg, &speed_accel, &speed_max, &rot, &brake);
|
|
108 new = car_create(id, carImg, texImg,
|
|
109 speed_accel, speed_max, rot, brake);
|
|
110 return new;
|
|
111 }
|
|
112 break;
|
|
113 default:
|
|
114 break;
|
|
115 }
|
|
116 }
|
|
117
|
|
118 /* $B$3$3$^$GC)$jCe$$$?$iFI$_9~$_<:GT(B */
|
|
119 fprintf(main_fp, "error - car_new_readCSV\n");
|
|
120 exit(EXIT_FAILURE);
|
|
121 }
|
|
122
|
|
123 CarPtr
|
|
124 car_init(int id)
|
|
125 {
|
|
126 CarPtr new;
|
|
127 FILE *fp;
|
|
128 char *filename = "car/car.dat";
|
|
129
|
|
130 if (!(fp = fopen(filename, "r"))) {
|
|
131 fprintf(main_fp, "error read file %s\n", filename);
|
|
132 exit(EXIT_FAILURE);
|
|
133 }
|
|
134 new = car_new_readCSV(fp, id);
|
|
135 fclose(fp);
|
|
136
|
|
137 return new;
|
|
138 }
|
|
139
|
|
140 /*---------------------------
|
|
141 car$B$r(BY$B<4$G2sE>$5$;$k(B
|
|
142 flg: $B2sE>J}8~(B 1:$B1&(B, -1:$B:8(B
|
|
143 ---------------------------*/
|
|
144 void
|
|
145 car_swerve(CarPtr car, int flg)
|
|
146 {
|
|
147 FMATRIX rot;
|
|
148 FVECTOR v;
|
|
149
|
|
150 car->y_angle += (float)flg*car->rotation_angle;
|
|
151 car->y_angle += (car->y_angle < 0) ? 360.0 : 0;
|
|
152 car->y_angle += (car->y_angle > 360.0) ? -360.0 : 0;
|
|
153
|
|
154 ps2_vu0_unit_matrix(rot);
|
|
155 ps2_vu0_rot_matrix_y(rot, rot, degree2radian((float)flg*car->rotation_angle));
|
|
156 ps2_vu0_copy_vector(v, car->direction);
|
|
157 ps2_vu0_apply_matrix(car->direction, rot, v);
|
|
158 }
|
|
159
|
|
160 /*----------------------------
|
|
161 car$B$r2CB.!&8:B.$9$k(B
|
|
162 (flg == 1) ? $B2CB.(B : $B8:B.(B
|
|
163 ---------------------------*/
|
|
164 void
|
|
165 car_accelerate(CarPtr car, int flg)
|
|
166 {
|
|
167 car->speed += (flg == 1) ? car->speed_accel : -car->brake;
|
|
168 car->speed = (car->speed < 0) ? 0 : car->speed;
|
|
169 car->speed = (car->speed > car->speed_max) ? car->speed_max : car->speed;
|
|
170 }
|
|
171
|
|
172
|
|
173 /*---------------------------------
|
|
174 $B?J9T8e$N(Bcar$B$KBP$7$F%3!<%9LL$NFb30H=Dj$r9T$$!"(B
|
|
175 : $B8=:_$N%3!<%9(B
|
|
176 : $B8=:_$N%3!<%9$N<!$N%3!<%9(B
|
|
177 : $B8=:_$N%3!<%9$NA0$N%3!<%9(B
|
|
178 $B$KB8:_$9$l$P(BTRUE$B$rJV$9(B
|
|
179 $B$I$N%3!<%9$K$b5o$J$$(B($BJI$K>WFM$7$?(B)$B>l9g$O(B
|
|
180 FALSE$B$rJV$9!#(B
|
|
181 ---------------------------------*/
|
|
182 static Bool
|
|
183 car_field_check(CarPtr car)
|
|
184 {
|
|
185 FieldPtr p;
|
|
186 FieldPtr f = field_get_actual();
|
|
187
|
|
188 // $B8=:_$N%3!<%9$G>WFM!&Fb30H=Dj(B
|
|
189 p = f;
|
|
190 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
191 goto FIELD_CHECK_OK;
|
|
192 }
|
|
193
|
|
194 // $B8=:_$N%3!<%9$N<!$KNY@\$9$k%3!<%9$G>WFM!&Fb30H=Dj(B
|
|
195 p = f->next;
|
|
196 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
197 if (field_rap_increment(1)) {
|
|
198 game.rap++;
|
|
199 }
|
|
200 goto FIELD_CHECK_OK;
|
|
201 }
|
|
202
|
|
203 // $B8=:_$N%3!<%9$NA0$KNY@\$9$k%3!<%9$G>WFM!&Fb30H=Dj(B
|
|
204 p = f->prev;
|
|
205 if (col_detect(&p->colface, car->vertical, car->location) == TRUE) {
|
|
206 field_rap_increment(-1);
|
|
207 goto FIELD_CHECK_OK;
|
|
208 }
|
|
209
|
|
210 // $B$I$N%3!<%9>e$K$b5o$J$$(B($BJI$K>WFM(B)
|
|
211 return FALSE;
|
|
212
|
|
213 FIELD_CHECK_OK:
|
|
214 field_set_actual(p);
|
|
215 return TRUE;
|
|
216 }
|
|
217
|
|
218
|
|
219 /*------------------------------
|
|
220 $BB.EY(Bspeed$B$H8~$-(Bdirection$B$+$i(B
|
|
221 $B?J9T8e$N0LCV(Blocation$B$r5a$a$k!#(B
|
|
222 $B$^$?!"?J9T8e$N>WFMH=Dj$b9T$&!#(B
|
|
223 ------------------------------*/
|
|
224 static void
|
|
225 car_move(CarPtr car)
|
|
226 {
|
|
227 FVECTOR mov, prev_location;
|
|
228
|
|
229 ps2_vu0_scale_vector(mov, car->direction, car->speed);
|
|
230 ps2_vu0_copy_vector(prev_location, car->location);
|
|
231 ps2_vu0_add_vector(car->location, car->location, mov);
|
|
232
|
|
233 if (car_field_check(car) == FALSE) {
|
|
234 ps2_vu0_copy_vector(car->location, prev_location);
|
|
235 car->speed = car->speed*0.5;
|
|
236 }
|
|
237 }
|
|
238
|
|
239
|
|
240 static void
|
|
241 car_axis_rotation(CarPtr car)
|
|
242 {
|
|
243 FMATRIX pose, yrot, yrotinv;
|
|
244
|
|
245 ps2_vu0_unit_matrix(yrot);
|
|
246 ps2_vu0_rot_matrix_y(yrot, yrot, -degree2radian(car->y_angle));
|
|
247 ps2_vu0_unit_matrix(yrotinv);
|
|
248 ps2_vu0_rot_matrix_y(yrotinv, yrotinv, degree2radian(car->y_angle));
|
|
249
|
|
250 {
|
|
251 FVECTOR yd = {0, -1, 0, 1};
|
|
252 FVECTOR rotaxis;
|
|
253 FVECTOR q;
|
|
254
|
|
255 ps2_vu0_outer_product(rotaxis, car->vertical, yd);
|
|
256 {
|
|
257 float scale=1/NORM(rotaxis);
|
|
258 rotaxis[0] *= scale;
|
|
259 rotaxis[1] *= scale;
|
|
260 rotaxis[2] *= scale;
|
|
261 }
|
|
262
|
|
263 quotanion(q, rotaxis, acos(ps2_vu0_inner_product(yd, car->vertical)/(NORM(yd)*NORM(car->vertical))));
|
|
264 quotanion_rotmatrix(pose, q);
|
|
265 }
|
|
266
|
|
267 ps2_vu0_mul_matrix(pose, yrot, pose);
|
|
268 ps2_vu0_mul_matrix(pose, pose, yrotinv);
|
|
269 ps2_vu0_copy_matrix(car->body->transfer, pose);
|
|
270 ps2_vu0_rot_matrix_y(car->body->transfer, car->body->transfer,
|
|
271 degree2radian(car->y_angle));
|
|
272 }
|
|
273
|
|
274 void
|
|
275 car_update(CarPtr car)
|
|
276 {
|
|
277 // $B0\F0(B
|
|
278 car_move(car);
|
|
279
|
|
280 // $BK`;$$K$h$kB.EY8:>/(B
|
|
281 car->speed -= 0.005;
|
|
282 car->speed = (car->speed < 0) ? 0 : car->speed;
|
|
283
|
|
284 // $B79$-(B
|
|
285 car_axis_rotation(car);
|
|
286 }
|
|
287
|
|
288 void
|
|
289 car_destroy(CarPtr p)
|
|
290 {
|
|
291 TEXTURE *t;
|
|
292
|
|
293 if ((t = p->body->surfaces->texture)) {
|
|
294 ps2util_tex_Exclude(t);
|
|
295 free_texture(t);
|
|
296 }
|
|
297 ps2util_obj_Free(p->body);
|
|
298 free(p->free_addr);
|
|
299
|
|
300 #ifdef DEBUG
|
|
301 fprintf(main_fp, "free car addr = %x\n", (int)p->free_addr);
|
|
302 #endif
|
|
303 }
|