changeset 44:3b71b39411b1

*** empty log message ***
author gongo
date Wed, 08 Nov 2006 05:21:58 +0000
parents a1908b83f8fa
children d7520785d8a6
files col.c game.c game.h linda.c linda.h schedule.c schedule.h
diffstat 7 files changed, 446 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/col.c	Mon Nov 06 06:13:36 2006 +0000
+++ b/col.c	Wed Nov 08 05:21:58 2006 +0000
@@ -102,10 +102,6 @@
     malloc_align16(&hd->free_addr, &colface, sizeof(ColFace)*face->siz/3);
 
     hd->facenum = face->siz/3;
-
-    fprintf(main_fp, "(ColFace)*face->size/3 = %d\n", sizeof(ColFace)*face->siz/3);
-    fprintf(main_fp, "          face->size/3 = %d\n", face->siz/3);
-    fprintf(main_fp, "(ColFace)              = %d\n", sizeof(ColFace));
     
     for (i=0; i<hd->facenum; i++) {
 	memcpy(colface[i].p0, ((FVECTOR*)face->pnts)[i*3], sizeof(FVECTOR));
--- a/game.c	Mon Nov 06 06:13:36 2006 +0000
+++ b/game.c	Wed Nov 08 05:21:58 2006 +0000
@@ -32,6 +32,7 @@
 game_main()
 {
     Bool flg = TRUE;
+    Bool linda_flg = TRUE;
     set_schedule(sche_game_init);
 
     game.play_id = linda_init();
@@ -44,7 +45,6 @@
 	schedule();
 
 #ifdef LINDA
-	linda_update();
 	psx_sync_n();
 #endif
     }
--- a/game.h	Mon Nov 06 06:13:36 2006 +0000
+++ b/game.h	Wed Nov 08 05:21:58 2006 +0000
@@ -2,23 +2,9 @@
  * $Id$
  */
 
-typedef enum game_state {
-    GAME_INIT,
-    GAME_OPENING,
-    GAME_SELECT_CAR,
-    GAME_WAIT,
-    GAME_SELECT_COURSE,
-    GAME_READY,
-    GAME_MAIN_INIT,
-    GAME_MAIN,
-    GAME_PAUSE,
-    GAME_GOAL,
-    GAME_FINISH
-} GAME_STATE;
-
 typedef struct game {
-    GAME_STATE state;
     void* (*exec)();
+    void* (*linda_exec)();
     CarPtr jiki;
     int course_id;   // $B%3!<%9(BID
     int car_id;      // $B<+5!(BID
--- a/linda.c	Mon Nov 06 06:13:36 2006 +0000
+++ b/linda.c	Wed Nov 08 05:21:58 2006 +0000
@@ -10,23 +10,320 @@
 #include "car.h"
 #include "game.h"
 #include "lindaapi.h"
+#include "linda.h"
 
-#define CLIENT_MAX 2
-#define LINDA_ASK_ID 65535
-#define LINDA_HOST "firefly.cr.ie.u-ryukyu.ac.jp"
+#define PLAYER_1P 1
 
-extern CarPtr car_init(int);
-extern void gFont_SetString(char *, int, int);
-extern void gFont_SetStringInt(int, int, int);
 extern void carNode_append(CarPtr);
 extern FILE *main_fp;
-
 static int my_id;
-
 static CarPtr linda_carlist[CLIENT_MAX+1];
 static int linda_seq[CLIENT_MAX+1];
+static void* sche;
 
 static void
+set_header(unsigned int data, char *pkt, int offset)
+{
+    int *ipkt;
+    int ndata = htonl(data);
+
+    ipkt = (int*)pkt;
+    ipkt[offset/4] = ndata;
+}
+
+static unsigned int
+get_header(char *pkt, int offset)
+{
+    int *ipkt;
+    int data;
+    unsigned int header;
+
+    ipkt   = (int *)pkt;
+    data   = ipkt[offset/4];
+    header = (unsigned int)ntohl(data);
+
+    return header;
+}
+
+static char*
+make_packet(unsigned int mode,     unsigned int carid,
+	    unsigned int courseid, char *data)
+{
+    char *packet;
+    unsigned int len = 0;
+
+    if (data) len += sizeof(FMATRIX);
+
+    packet = (char*)malloc(PKT_HEADER_SIZE+len);
+
+    set_header(mode,     packet, PKT_MODE_OFFSET);
+    set_header(carid,    packet, PKT_CARID_OFFSET);
+    set_header(courseid, packet, PKT_COURSEID_OFFSET);
+
+    if (data)
+	memcpy(packet+PKT_DATA_OFFSET, data, len);
+
+    return packet;
+}
+
+
+static void
+send_packet(unsigned int dest, unsigned int mode, unsigned int car,
+	    unsigned int course,  char *data)
+{
+    char *pkt;
+    int len = PKT_HEADER_SIZE;
+
+    if (data) len += sizeof(FMATRIX);
+
+    pkt = make_packet(mode, car, course, data);
+    psx_out(dest, pkt, len);
+    free(pkt);
+}
+
+static void
+get_packet(int id, int *flg, int *mode, int *car, int *course, char *data)
+{
+    char *reply, *pkt;
+
+    reply = psx_reply(linda_seq[id]);
+    if (reply) {
+	pkt    = reply+LINDA_HEADER_SIZE;
+
+	if (flg)    flg = 1;
+	if (mode)   mode   = get_header(pkt, PKT_MODE_OFFSET);
+	if (car)    car    = get_header(pkt, PKT_CARID_OFFSET);
+	if (course) course = get_header(pkt, PKT_COURSEID_OFFSET);
+	if (data)   data   = get_header(pkt, PKT_DATA_OFFSET);
+
+	psx_free(reply);
+	linda_seq[id] = psx_rd(id);
+    }    
+}
+
+static void
+linda_set_schedule(void *func, int mode)
+{
+    linda_seq[game.play_id] = psx_in(game.play_id);
+    game.linda_exec = func;
+    send_packet(game.play_id, mode, 1, 1, NULL);
+}
+
+static Bool
+linda_sche_wait0()
+{
+    int i, flg=0;
+    int id = game.player_id;
+    int connect = 1; // 接続済みユーザ数
+    char *reply = NULL;
+
+    for (i=1; i<=CLIENT_MAX+1; i++) {
+	if (i == id) continue;
+
+	get_packet(i, &flg, NULL, NULL, NULL, NULL);
+
+	if (flg == 1) connect++;
+    }
+
+    if (connect == CLIENT_MAX) {
+	linda_set_schedule(linda_sche_wait_ready0, MODE_WAIT_READY);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+
+}
+
+static Bool
+linda_sche_wait1()
+{
+    int mode;
+
+    get_packet(PLAYER_1P, NULL, &mode, NULL, NULL, NULL);
+
+    if (mode == MODE_WAIT_READY) {
+	linda_set_schedule(linda_sche_wait_ready1, MODE_WAIT_READY);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static Bool
+linda_sche_wait_ready0()
+{
+    int i, flg, mode;
+    int id = game.player_id;
+    int connect = 1;
+
+    for (i=1; i<=CLIENT_MAX+1; i++) {
+	if (i == id) continue;
+
+	get_packet(i, &flg, &mode, NULL, NULL, NULL);
+	if (flg == 1 && mode == MODE_WAIT_READY)
+	    connect++;
+    }
+
+    if (connect == CLIENT_MAX) {
+	linda_set_schedule(linda_sche_opening0, MODE_OPENING);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+
+}
+
+static void
+linda_sche_wait_ready1()
+{
+    int mode;
+
+    get_packet(PLAYER_1P, NULL, &mode, NULL, NULL, NULL);
+
+    if (mode == MODE_OPENING) {
+	linda_set_schedule(linda_sche_opening1, MODE_OPENING);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static void
+linda_sche_opening0()
+{
+    int i, flg, mode;
+    int id = game.player_id;
+    int connect = 1;
+
+    for (i=1; i<=CLIENT_MAX+1; i++) {
+	if (i == id) continue;
+
+	get_packet(i, &flg, &mode, NULL, NULL, NULL);
+	if (flg == 1 && mode == MODE_OPENING)
+	    connect++;
+    }
+
+    if (connect == CLIENT_MAX) {
+	linda_set_schedule(linda_sche_select_car0, MODE_SELECT_CAR);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static void
+linda_sche_opening1()
+{
+    int mode;
+
+    get_packet(PLAYER_1P, NULL, &mode, NULL, NULL, NULL);
+
+    if (mode == MODE_SELECT_CAR) {
+	linda_set_schedule(linda_sche_select_car1, MODE_SELECT_CAR);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static Bool 
+linda_sche_select_car0()
+{
+    int i, flg, mode;
+    int id = game.player_id;
+    int connect = 1;
+
+    for (i=1; i<=CLIENT_MAX+1; i++) {
+	if (i == id) continue;
+
+	get_packet(i, &flg, &mode, NULL, NULL, NULL);
+	if (flg == 1 && mode == MODE_SELECT_CAR)
+	    connect++;
+    }
+
+    if (connect == CLIENT_MAX) {
+	linda_set_schedule(linda_sche_select_course0, MODE_SELECT_COURSE);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static Bool
+linda_sche_select_car1()
+{
+    int mode;
+
+    get_packet(PLAYER_1P, NULL, &mode, NULL, NULL, NULL);
+
+    if (mode == MODE_SELECT_COURSE) {
+	linda_set_schedule(linda_sche_select_course1, MODE_SELECT_COURSE);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static Bool
+linda_sche_select_course0()
+{
+    static int course_id = 1;
+
+    if (course_id != game.course_id) {
+	course_id = game.course_id;
+	send_packet(game.player_id, MODE_SELECT_COURSE, 0, course_id, NULL);
+	return TRUE;
+    }
+
+    for (i=1; i<=CLIENT_MAX+1; i++) {
+	if (i == id) continue;
+
+	get_packet(i, &flg, &mode, NULL, NULL, NULL);
+	if (flg == 1 && mode == MODE_SELECT_COURSE)
+	    connect++;
+    }
+
+    if (connect == CLIENT_MAX) {
+	linda_set_schedule(linda_sche_ready0, MODE_READY);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+static Bool
+linda_sche_select_course1()
+{
+    int mode, course_id;
+
+    get_packet(PLAYER_1P, NULL, &mode, NULL, &course_id, NULL);
+
+    if (mode == MODE_SELECT_COURSE) {
+	game.course_id = course_id;
+	return FALSE;
+    } else if (mode == MODE_READY) {
+	linda_set_schedule(linda_sche_ready1, MODE_READY);
+	return TRUE;
+    } else {
+	return FALSE;
+    }
+}
+
+
+static Bool
+linda_sche_ready0()
+{
+    return TRUE;
+}
+
+static Bool
+linda_sche_ready1()
+{
+    return FALSE;
+}
+
+#if 0
+static void
 linda_enemy_update(int id, CarPtr enemy, CarInfoPtr tpl)
 {
     int i,j;
@@ -77,6 +374,7 @@
     }
 }
 
+
 /*
  * 自機情報が更新されていなければ
  * 0を返す(psx_outしない)
@@ -99,6 +397,7 @@
 linda_jiki_update()
 {
     int i,j;
+    
 
     jiki.car_id     = game.car_id;
     jiki.course_id  = game.course_id;
@@ -125,15 +424,19 @@
     int i;
     int barrier = 1; // バリア同期(?)用
     int connect = 1;
+    int mode, id, flg;
     char* reply;
-    CarInfoPtr p = NULL;
+    char* pkt;
     CarPtr car = NULL;
 
     for (i=1; i<=CLIENT_MAX; i++) {
 	reply = psx_reply(linda_seq[i]);
 	if (reply) {
-	    p = (CarInfoPtr)(reply+LINDA_HEADER_SIZE);
-
+	    pkt  = reply+LINDA_HEADER_SIZE;
+	    mode = get_header(pkt, PKT_MODE_OFFSET);
+	    id   = get_header(pkt, PKT_ID_OFFSET);
+	    flg  = get_header(pkt, PKT_FLG_OFFSET);
+	    
 	    if (i != game.play_id) {
 		connect++;
 		
@@ -210,6 +513,7 @@
 	psx_out(game.play_id, (char*)&jiki, sizeof(CarInfo));
     }
 }
+#endif
 
 static int
 get_id()
@@ -252,11 +556,16 @@
 
     for (i=1; i<=CLIENT_MAX; i++) {
 	if (i == my_id) {
-	    psx_out(i, (char*)&jiki, sizeof(CarInfo));
+	    send_packet(i, MODE_WAIT, i, 0, NULL);
 	}
 	linda_seq[i] = psx_rd(i);
     }
     psx_sync_n();
 
+    if (my_id == PLAYER_1P)
+	sche = linda_set_schedule(linda_sche_wait0, MODE_WAIT);
+    else
+	sche = linda_set_schedule(linda_sche_wait1, MODE_WAIT);
+
     return my_id;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/linda.h	Wed Nov 08 05:21:58 2006 +0000
@@ -0,0 +1,39 @@
+/*
+  +-------+-------+-------+-----------+
+  | game  | car   | course| data      |
+  | mode  |    id |    id | (fmatrix) |
+  +-------+-------+-------+-----------+
+
+       mode : ゲームの状態(オープニング、車選択等)
+     car id : ユーザの選んだ車
+  course id : 走るコース(1Pのみ選択可能)
+       data : ユーザの車の状態(location、angle等)
+
+       ユーザのIDはLinda_IDで判断する
+       linda_seq[i]: ユーザID=i
+*/
+#define PKT_MODE_OFFSET      0
+#define PKT_CARID_OFFSET     4
+#define PKT_COURSEID_OFFSET  8
+#define PKT_DATA_OFFSET     12
+
+#define PKT_HEADER_SIZE     12
+
+
+#define CLIENT_MAX 2
+#define LINDA_ASK_ID 65535
+#define LINDA_HOST "firefly.cr.ie.u-ryukyu.ac.jp"
+
+#define MODE_INIT             0
+#define MODE_WAIT             1
+#define MODE_WAIT_READY       2
+#define MODE_OPENING          3
+#define MODE_SELECT_CAR       4
+#degine MODE_SELECT_COURSE    5
+#degine MODE_READY            6
+#degine MODE_MAIN_INIT        7
+#degine MODE_MAIN             8
+#degine MODE_MAIN_PAUSE       9
+#degine MODE_MAIN_GOAL       10
+#degine MODE_MAIN_FINISH     11
+#degine MODE_FINISH          12
--- a/schedule.c	Mon Nov 06 06:13:36 2006 +0000
+++ b/schedule.c	Wed Nov 08 05:21:58 2006 +0000
@@ -16,6 +16,7 @@
 #include "light.h"
 #include "title_scene.h"
 #include "game_time.h"
+#include "scheduler.h"
 
 
 #define MAXCAR   3 // 選択可能機体
@@ -71,18 +72,6 @@
     field_init(game.course_id);
 }
 
-void sche_game_init();
-static void sche_game_opening();
-static void sche_game_select_car();
-static void sche_game_select_course();
-static void sche_game_ready();
-static void sche_game_main_init();
-static void sche_game_main();
-static void sche_game_main_pause();
-static void sche_game_main_goal();
-static void sche_game_main_finish();
-static void sche_game_main_finish2();
-
 static void
 game_env_init()
 {
@@ -110,6 +99,38 @@
     set_schedule(sche_game_opening);
 }
 
+/**
+ * 通信対戦専用
+ * 全ユーザが接続するまで待つ
+ */
+static void
+sche_game_wait()
+{
+    gFont_SetString("WAITING...", 200, 100);
+    if (game.linda_exec() == TRUE)
+	set_schedule(sche_game_wait_ready);
+}
+
+/**
+ * 通信対戦専用
+ * 全ユーザの接続を確認したら呼び出す
+ */
+static void
+sche_game_wait_ready()
+{
+    gFont_SetString("CONNECT OK!!", 170, 300);
+    if (game.play_id == 1) {
+	gFont_SetString(" PUSH START ", 170, 400);
+	if (pad.st != 1) { goto WAIT_READY; }
+    }
+
+    if (game.linda_exec() == TRUE)
+	set_schedule(sche_game_wait_opening);
+    
+WAIT_READY:
+    return;
+}
+
 void
 sche_game_opening()
 {
@@ -119,11 +140,15 @@
 	    gFont_SetString("PUSH START !!", 170, 380);
 	}
 	blink_count = (blink_count > 70) ? 0 : blink_count + 1;
+
+	if (pad.st != 1) { goto LINDA_OPENING; }
     }
 
-    if (pad.st == 1) {
+    if (game.linda_exec() == TRUE)
 	set_schedule(sche_game_select_car);
-    }
+
+OPENING:
+    return;
 
     /*
     if (game.play_id == 1) {
@@ -158,9 +183,15 @@
 	game.car_id =
 	    (game.car_id < 2) ? MAXCAR : game.car_id - 1;
     }
-    if (pad.circle == 1) {
+
+    if (game.play_id == 1)
+	if (pad.circle != 1) { goto SELECT_CAR; }
+
+    if (game.linda_exec() == TRUE)
 	set_schedule(sche_game_select_course);
-    }
+    
+SELECT_CAR:
+    return;
 }
 
 void
@@ -171,18 +202,30 @@
     gSprite_PutSprite(25, 120, 300);
 
     gFont_SetString("SELECT COURSE", 150, 50);
-      
-    if (pad.right == 1) {
-	game.course_id =
-	    (game.course_id > MAXFIELD-1) ? 1 : game.course_id + 1;
+
+    if (game.player_id == 1) {
+	if (pad.circle != 1) { goto SELECT_COURSE; }
+
+	if (pad.right == 1) {
+	    game.course_id =
+		(game.course_id > MAXFIELD-1) ? 1 : game.course_id + 1;
+	    game.linda_exec();
+	    goto SELECT_COURSE;
+	}
+	if (pad.left == 1) {
+	    game.course_id =
+		(game.course_id < 2) ? MAXFIELD : game.course_id - 1;
+	    game.linda_exec();
+	    goto SELECT_COURSE;
+	}
     }
-    if (pad.left == 1) {
-	game.course_id =
-	    (game.course_id < 2) ? MAXFIELD : game.course_id - 1;
-    }
-    if (pad.circle == 1) {
-	set_schedule(sche_game_ready);
-    }
+
+    if (game.linda_exec() == TRUE)
+	set_schedule(sche_game_select_course);
+
+SELECT_COURSE:
+    return;
+
 }
 
 void
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/schedule.h	Wed Nov 08 05:21:58 2006 +0000
@@ -0,0 +1,14 @@
+extern void set_schedule();
+extern void sche_game_init();
+extern void sche_game_wait();
+extern void sche_game_wait_ready();
+extern void sche_game_opening();
+extern void sche_game_select_car();
+extern void sche_game_select_course();
+extern void sche_game_ready();
+extern void sche_game_main_init();
+extern void sche_game_main();
+extern void sche_game_main_pause();
+extern void sche_game_main_goal();
+extern void sche_game_main_finish();
+extern void sche_game_main_finish2();