comparison linda.c @ 0:0fae5658fb0b

Initial revision
author gongo
date Thu, 02 Nov 2006 08:55:19 +0000 (2006-11-02)
parents
children 6910aeb52843
comparison
equal deleted inserted replaced
-1:000000000000 0:0fae5658fb0b
1 /*
2 * 2006/07/24 16:22:23 sugiyama
3 */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <math.h>
7 #include <libps2.h>
8 #include <ps2util.h>
9
10 #include "car.h"
11 #include "game.h"
12 #include "lindaapi.h"
13
14 #define CLIENT_MAX 2
15 #define LINDA_ASK_ID 65535
16 #define LINDA_HOST "firefly.cr.ie.u-ryukyu.ac.jp"
17
18 extern CarPtr car_init(int);
19 extern void gFont_SetString(char *, int, int);
20 extern void gFont_SetStringInt(int, int, int);
21 extern void carNode_append(CarPtr);
22 extern Game game;
23 extern FILE *main_fp;
24
25 static GAME_STATE common_state = GAME_WAIT;
26 static int my_id;
27
28 typedef struct car_info {
29 GAME_STATE state;
30 FMATRIX position;
31 int car_id;
32 int course_id;
33 int create_flg;
34 } CarInfo, *CarInfoPtr;
35
36 static CarPtr linda_carlist[CLIENT_MAX+1];
37 static CarInfo jiki;
38 static int linda_seq[CLIENT_MAX+1];
39
40 static void
41 linda_enemy_update(int id, CarPtr enemy, CarInfoPtr tpl)
42 {
43 int i,j;
44
45 if (id == 1 || tpl->state == GAME_GOAL) {
46 common_state = tpl->state;
47 game.course_id = tpl->course_id;
48 }
49
50 if (enemy && (common_state == GAME_MAIN || common_state == GAME_GOAL)) {
51 for (i=0; i<4; i++) {
52 for (j=0; j<4; j++) {
53 enemy->body->transfer[i][j] = tpl->position[i][j];
54 }
55 }
56
57 /* Ũ�����а��֤� */
58 enemy->body->transfer[3][0] -= game.jiki->location[0];
59 enemy->body->transfer[3][1] -= game.jiki->location[1];
60 enemy->body->transfer[3][2] -= game.jiki->location[2];
61 enemy->body->transfer[3][3] = 1;
62 }
63 }
64
65 /*
66 * ���֤�Ʊ���ʤ�1���֤���
67 * ��ä��ɤ������ˡ�������Ǥ�...
68 */
69 static int
70 linda_jiki_compareLocation()
71 {
72 float x,y,z;
73 double d;
74
75 if (common_state == GAME_MAIN || common_state == GAME_GOAL) {
76 x = jiki.position[3][0] - game.jiki->location[0];
77 y = jiki.position[3][1] - game.jiki->location[1];
78 z = jiki.position[3][2] - game.jiki->location[2];
79 d = sqrt(x*x+y*y+z*z);
80
81 if (d < 1.0) {
82 return 1;
83 } else {
84 return 0;
85 }
86 } else {
87 return 1;
88 }
89 }
90
91 /*
92 * �������󤬹�������Ƥ��ʤ����
93 * 0���֤�(psx_out���ʤ�)
94 */
95 static int
96 linda_jiki_compare()
97 {
98 if ((jiki.car_id == game.car_id) &&
99 (jiki.course_id == game.course_id) &&
100 (jiki.create_flg == ((game.jiki) ? 1:0)) &&
101 (jiki.state == common_state) &&
102 (linda_jiki_compareLocation())) {
103 return 0;
104 } else {
105 return 1;
106 }
107 }
108
109 static void
110 linda_jiki_update()
111 {
112 int i,j;
113
114 jiki.car_id = game.car_id;
115 jiki.course_id = game.course_id;
116 jiki.create_flg = (game.jiki) ? 1 : 0;
117 jiki.state = common_state;
118
119 if (common_state == GAME_MAIN || common_state == GAME_GOAL) {
120 for (i=0; i<3; i++) {
121 for (j=0; j<4; j++) {
122 jiki.position[i][j]
123 = game.jiki->body->transfer[i][j];
124 }
125 }
126 jiki.position[3][0] = game.jiki->location[0];
127 jiki.position[3][1] = game.jiki->location[1];
128 jiki.position[3][2] = game.jiki->location[2];
129 jiki.position[3][3] = 1;
130 }
131 }
132
133 void
134 linda_update()
135 {
136 int i;
137 int barrier = 1; // �ХꥢƱ��(?)��
138 int connect = 1;
139 char* reply;
140 CarInfoPtr p = NULL;
141 CarPtr car = NULL;
142
143 for (i=1; i<=CLIENT_MAX; i++) {
144 reply = psx_reply(linda_seq[i]);
145 if (reply) {
146 p = (CarInfoPtr)(reply+LINDA_HEADER_SIZE);
147
148 if (i != game.play_id) {
149 connect++;
150
151 car = linda_carlist[i];
152 if (common_state == GAME_MAIN && !car && p->create_flg == 1) {
153 car = car_init(p->car_id);
154 carNode_append(car);
155 linda_carlist[i] = car;
156 }
157 linda_enemy_update(i, car, p);
158 if (common_state == p->state) {
159 barrier++;
160 }
161 }
162 psx_free(reply);
163 linda_seq[i] = psx_rd(i);
164 }
165 }
166
167 if (connect == CLIENT_MAX) {
168 if (game.state == GAME_WAIT) {
169 gFont_SetString("CONNECT OK!!", 170, 300);
170 if (game.play_id == 1)
171 gFont_SetString(" PUSH START ", 170, 400);
172 }
173
174 // �����㤴���㤷�Ƥ�...
175 // �ʤ�̵�̤ʽ��������ꤽ����
176 if (game.play_id == 1) {
177 // �����Υե饰��Ω
178 if (common_state == GAME_GOAL) {
179 if (game.state == GAME_FINISH) {
180 common_state = game.state;
181 } else {
182 game.state = common_state;
183 }
184 } else if (barrier == CLIENT_MAX) {
185 common_state = game.state;
186 } else {
187 game.state = common_state;
188 }
189 } else {
190 if (game.state == GAME_GOAL) {
191 if (common_state != GAME_FINISH) {
192 common_state = game.state;
193 } else {
194 game.state = common_state;
195 }
196 } else {
197 game.state = common_state;
198 }
199 }
200 } else {
201 if (game.state == GAME_WAIT) {
202 gFont_SetString("WAITING...", 200, 100);
203 }
204 game.state = common_state;
205 }
206
207 if (linda_jiki_compare()) {
208 #ifdef DEBUG
209 // �ɤΥ����ߥ󥰤�out����Ƥ뤫����
210 // ɬ�פʤȤ�����out���ʤ��ȽŤ��Ǥ�������������
211 //fprintf(main_fp, "psx_out() jiki\n");
212 #endif
213 linda_jiki_update();
214 // ̵������
215 // �Ť���Τ�ä������ʤ������
216 // �������������¾��ͭ��Τ��ʡ�
217 // �¹Ԥ�³���Ƥ�Ⱥǽ�Ū�˷�Ťˤʤ�Τ�
218 // �������դ����꤬ͭ��Τ���������
219 // psx_free(psx_reply(psx_in(game.play_id)));
220 linda_seq[my_id]=psx_in(game.play_id);
221 psx_out(game.play_id, (char*)&jiki, sizeof(CarInfo));
222 }
223 }
224
225 static int
226 get_id()
227 {
228 unsigned char * reply;
229 int seq;
230 int id;
231
232 //�桼��ID����Ǽ����Ƥ���TUPLE Space��ID�إ�������
233 seq = psx_in(LINDA_ASK_ID);
234
235 // ID�������Ǥ���ޤǥ롼��
236 while((reply = psx_reply(seq)) == 0) psx_sync_n();
237
238 id = atoi(reply+LINDA_HEADER_SIZE);
239 psx_free(reply);
240 return id;
241 }
242
243 void
244 linda_jikiInfo_init()
245 {
246 int i, j;
247
248 jiki.car_id = 1;
249 jiki.course_id = 1;
250 jiki.create_flg = 0;
251 jiki.state = game.state;
252
253 // malloc16���������Ƥʤ�����Bus error��
254 // ps2_vu0_unit_matrix(jiki.position);
255 for (i=0; i<4; i++) {
256 for (j=0; j<4; j++) {
257 if (i==j)
258 jiki.position[i][j] = 1;
259 else
260 jiki.position[i][j] = 0;
261 }
262 }
263
264 for (i=0; i<CLIENT_MAX+1; i++) {
265 linda_carlist[i] = NULL;
266 }
267 }
268
269
270
271 int
272 linda_init()
273 {
274
275 int i;
276
277 start_linda(LINDA_HOST);
278 my_id = get_id();
279
280 for (i=1; i<=CLIENT_MAX; i++) {
281 if (i == my_id) {
282 psx_out(i, (char*)&jiki, sizeof(CarInfo));
283 }
284 linda_seq[i] = psx_rd(i);
285 }
286 psx_sync_n();
287
288 return my_id;
289 }