/*
 ******************************************************************************
 *
 *	メッセージの管理処理
 *      $Id: msgManager.c,v 1.1 2003/05/30 21:25:28 tong Exp $
 ******************************************************************************
 */
#define  MSGMANAGERp
#include "main.h"
#include "actorManager.h"
#include "utility.h"
#include "castNameOBJ.h"
#include "msgManager.h"
#if JAPAN
#include "unit_message_JPNc.h"
#endif
#if AMERICA
#include "unit_message_USAc.h"
#endif
#if EUROPE
#include "unit_message_EURc.h"
#endif
#include "dbgController.h"

#include "messageData.h"

/************************************************************************
 *	通常ブートのヘッダー						*
 ************************************************************************/
NORMALBOOT_ACTOR(message)

#if !JAPAN
/************************************************************************
 *	各フォントサイズデータ						*
 ************************************************************************/
#if AMERICA
unsigned char FontSizeData[] = {
/*      ,   ＄  （  ）  .   ％  「  」  ＋  ”  ＜  ＞  ＆  ~       */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  8,  8,  8,  8,  8,  8,
/*  ０  １  ２  ３  ４  ５  ６  ７  ８  ９  Ａ  Ｂ  Ｃ  Ｄ  Ｅ  Ｆ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,
/*  Ｇ  Ｈ  Ｉ  Ｊ  Ｋ  Ｌ  Ｍ  Ｎ  Ｏ  Ｐ  Ｑ  Ｒ  Ｓ  Ｔ  Ｕ  Ｖ  */
     7,  7,  6,  7,  7,  7,  8,  8,  7,  7,  7,  8,  7,  7,  7,  7,
/*  Ｗ  Ｘ  Ｙ  Ｚ  ！  ”  ＃   ’ ＊      ，  ー  。  ／  ：  ＝  */
     7,  7,  7,  7,  5,  5,  7,  3,  7,  1,  8,  8,  8,  7,  8,  7,
/*  ？  ＠  。  ”  ’  ァ  ィ  ゥ  ェ  ォ  ャ  ッ  ュ  ョ  ヲ  ン  */
     6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ア  イ  ウ  エ  オ  カ  キ  ク  ケ  コ  サ  シ  ス  セ  ソ  タ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  チ  ツ  テ  ト  ナ  ニ  ヌ  ネ  ノ  ハ  ヒ  フ  ヘ  ホ  マ  ミ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ム  メ  モ  ヤ  ユ  ヨ  ラ  リ  ル  レ  ロ  ワ  ガ  ギ  グ  ゲ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ゴ  ザ  ジ  ズ  ゼ  ゾ  ダ  ヂ  ヅ  デ  ド  バ  ビ  ブ  ベ  ボ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  パ  ピ  プ  ペ  ポ  ａ  ｂ  ｃ  ｄ  ｅ  ｆ  ｇ  ｈ  ｉ  ｊ  ｋ  */
     8,  8,  8,  8,  8,  7,  7,  7,  7,  6,  6,  7,  7,  4,  7,  7,
/*  ｌ  ｍ  ｎ  ｏ  ｐ  ｑ  ｒ  ｓ  ｔ  ｕ  ｖ  ｗ  ｘ  ｙ  ｚ  ぁ  */
     5,  8,  7,  8,  8,  8,  7,  6,  7,  7,  7,  8,  6,  7,  7,  8,
/*  ぃ  ぅ  ぇ  ぉ  っ  ゃ  ゅ  ょ  を  ん  あ  い  う  え  お  か  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  き  く  け  こ  さ  し  す  せ  そ  た  ち  つ  て  と  な  に  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ぬ  ね  の  は  ひ  ふ  へ  ほ  ま  み  む  め  も  や  ゆ  よ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ら  り  る  れ  ろ  わ  が  ぎ  ぐ  げ  ご  ざ  じ  ず  ぜ  ぞ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  だ  ぢ  づ  で  ど  ば  び  ぶ  べ  ぼ  ぱ  ぴ  ぷ  ぺ  ぽ  ヴ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
#endif
#if EUROPE
unsigned char FontSizeData[] = {
/*      ,   ＄  （  ）  .   ％  「  」  ＋  ”  ＜  ＞  ＆  ~       */
     8,  8,  8,  5,  4,  8,  8,  8,  8,  7,  8,  8,  8,  8,  8,  8,
/*  ０  １  ２  ３  ４  ５  ６  ７  ８  ９  Ａ  Ｂ  Ｃ  Ｄ  Ｅ  Ｆ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  7,  7,  7,  7,  7,  7,
/*  Ｇ  Ｈ  Ｉ  Ｊ  Ｋ  Ｌ  Ｍ  Ｎ  Ｏ  Ｐ  Ｑ  Ｒ  Ｓ  Ｔ  Ｕ  Ｖ  */
     7,  7,  6,  7,  7,  7,  8,  8,  7,  7,  7,  8,  7,  7,  7,  7,
/*  Ｗ  Ｘ  Ｙ  Ｚ  ！  ”  ＃   ’ ＊      ，  ー  。  ／  ：  ＝  */
     7,  7,  7,  7,  5,  5,  7,  3,  7,  1,  8,  8,  8,  7,  8,  7,
/*  ？  ＠  。  ”  ’  ァ  ィ  ゥ  ェ  ォ  ャ  ッ  ュ  ョ  ヲ  ン  */
     6,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ア  イ  ウ  エ  オ  カ  キ  ク  ケ  コ  サ  シ  ス  セ  ソ  タ  */
     6,  7,  6,  7,  7,  6,  4,  7,  8,  4,  6,  7,  7,  8,  8,  8,
/*  チ  ツ  テ  ト  ナ  ニ  ヌ  ネ  ノ  ハ  ヒ  フ  ヘ  ホ  マ  ミ  */
     7,  7,  8,  7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ム  メ  モ  ヤ  ユ  ヨ  ラ  リ  ル  レ  ロ  ワ  ガ  ギ  グ  ゲ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ゴ  ザ  ジ  ズ  ゼ  ゾ  ダ  ヂ  ヅ  デ  ド  バ  ビ  ブ  ベ  ボ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  パ  ピ  プ  ペ  ポ  ａ  ｂ  ｃ  ｄ  ｅ  ｆ  ｇ  ｈ  ｉ  ｊ  ｋ  */
     8,  8,  8,  8,  8,  7,  7,  7,  7,  6,  6,  7,  7,  4,  7,  7,
/*  ｌ  ｍ  ｎ  ｏ  ｐ  ｑ  ｒ  ｓ  ｔ  ｕ  ｖ  ｗ  ｘ  ｙ  ｚ  ぁ  */
     5,  8,  7,  8,  8,  8,  7,  6,  7,  7,  7,  8,  6,  7,  7,  8,
/*  ぃ  ぅ  ぇ  ぉ  っ  ゃ  ゅ  ょ  を  ん  あ  い  う  え  お  か  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  き  く  け  こ  さ  し  す  せ  そ  た  ち  つ  て  と  な  に  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ぬ  ね  の  は  ひ  ふ  へ  ほ  ま  み  む  め  も  や  ゆ  よ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  ら  り  る  れ  ろ  わ  が  ぎ  ぐ  げ  ご  ざ  じ  ず  ぜ  ぞ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
/*  だ  ぢ  づ  で  ど  ば  び  ぶ  べ  ぼ  ぱ  ぴ  ぷ  ぺ  ぽ  ヴ  */
     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
#endif
};
#endif

/************************************************************************
 *	プロトタイプ宣言						*
 ************************************************************************/
static	void	SetUp_MsgAction(ACTOR *, t_st);
static	void	SetUp_MsgXAction(ACTOR *, t_st);

static char exist_msg = False;//メッセージボードの複数発生を防ぐ為

/************************************************************************
 *	誕生待ち							*
 ************************************************************************/
static	void	birth_message(ACTOR *Actor)
{
    /* regist_soonBirthEntry用(MsgXは使えない) */
    SetUp_MsgAction(Actor, MSG_WAIT);	//actStatusでなくstatusなので注意
}

/************************************************************************
 *	イメージデータを１個,読み込む					*
 ************************************************************************/
#if JAPAN
static	void	read_msgImg(t_utimg **readImg, unshort readNo)
{
    unshort	size = ORG_WSIZE * ORG_HSIZE;

    readrom_data(*readImg, (ut_msg + (readNo * (size / 2))), (unint)size);
}
#else
static	void	read_msgImg(t_utimg **readImg, unshort readNo, int utMsgFg)
{
    unshort	size = ORG_WSIZE * ORG_HSIZE;

    switch(utMsgFg){
    case 0:
	readrom_data(*readImg, (ut_msg + (readNo * (size / 2))), (unint)size);
	break;
    case 1:
	readrom_data(*readImg, (ut_msg2 + (readNo * (size / 2))), (unint)size);
	break;
    case 2:
	readrom_data(*readImg, (ut_msg3 + (readNo * (size / 2))), (unint)size);
	break;
    }
}
#endif

/************************************************************************
 *	イメージデータを１個セットする					*
 ************************************************************************/
#if JAPAN
static	void	oneMsg_set(t_utimg *dispImg, t_utimg *readImg,
			   unshort msgNo, t_chsz Wsize, t_chsz one_Wsize)
{
    int		x, y;
    int		dispYpos, readYpos;

    read_msgImg(&readImg, msgNo);

    for( y = 0; y < ORG_HSIZE; y++ ){
	dispYpos = y * Wsize;
	readYpos = y * ORG_WSIZE;
	for( x = 0; x < one_Wsize; x++ ){
	    *(dispImg + dispYpos + x) = *(readImg + readYpos + x);
	}
    }
}
#else
static	void	oneMsg_set(t_utimg *dispImg, t_utimg *readImg,
			   unshort msgNo, t_chsz Wsize, t_chsz one_Wsize,
			   int utMsgFg)
{
    int		x, y;
    int		dispYpos, readYpos;

    read_msgImg(&readImg, msgNo, utMsgFg);

    for( y = 0; y < ORG_HSIZE; y++ ){
	dispYpos = y * Wsize;
	readYpos = y * ORG_WSIZE;
	for( x = 0; x < one_Wsize; x++ ){
	    *(dispImg + dispYpos + x) = *(readImg + readYpos + x);
	}
    }
}
#endif

/************************************************************************
 *	イメージバッファに１行セットする				*
 ************************************************************************/
#if JAPAN
int	oneLineSet(t_utimg *dispImg, t_utimg *fontSzDt,
		   unshort *msgData, t_chsz Wsize)
{
    char	readImg_works[8 + (ORG_WSIZE * ORG_HSIZE)];
    int		idx = 0;
    int		offSet = 0;
    char	offset_cnt;
    unshort	msgNo;
    t_utimg	*readImg;

    Wsize /= 2;			/* 4bit TEST */

    /* 読み込むイメージデータの領域確保と初期化 */
    readImg = (t_utimg *)to_8ByteAlign(readImg_works);
    fillArea_c(dispImg, (size_t)(Wsize * ORG_HSIZE), 0x00);

    while( (msgNo = *(msgData+idx)) < MSGCODE ){
	offset_cnt = *(fontSzDt + (unint)msgNo);
	if( msgNo != MSG_NUK ){
	    oneMsg_set((dispImg + offSet), readImg,
		       msgNo, Wsize, (t_chsz)offset_cnt);
	}
	offSet += (int)offset_cnt;
	idx++;
    }
    return idx;
}
#else
int	oneLineSet(t_utimg *dispImg, t_utimg *fontSzDt,
		   unshort *msgData, t_chsz Wsize, int utMsgFg)
{
    char	readImg_works[8 + (ORG_WSIZE * ORG_HSIZE)];
    int		idx = 0;
    int		offSet = 0;
    char	offset_cnt;
    unshort	msgNo;
    t_utimg	*readImg;

    Wsize /= 2;			/* 4bit TEST */

    /* 読み込むイメージデータの領域確保と初期化 */
    readImg = (t_utimg *)to_8ByteAlign(readImg_works);
    fillArea_c(dispImg, (size_t)(Wsize * ORG_HSIZE), 0x00);

    while( (msgNo = *(msgData+idx)) < MSGCODE ){
	offset_cnt = *(fontSzDt + (unint)msgNo);
	if( msgNo != MSG_NUK ){
	    oneMsg_set((dispImg + offSet), readImg,
		       msgNo, Wsize, (t_chsz)offset_cnt, utMsgFg);
	}
	offSet += (int)offset_cnt;
	idx++;
    }
    return idx;
}
#endif

#if !JAPAN
/************************************************************************
 *	イメージバッファに１行セットする (外部からのコール用)		*
 ************************************************************************/
void	oneLineWrite(ACTOR *Actor, unshort *msgData, unint Xpos, unint Ypos)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    t_utimg	*image = misc->image;
    t_utimg	*image2 = misc->image2;
    t_utimg	*image3 = misc->image3;
    unint	Wsize = (unint)(Actor->size[WW] / 2) * Ypos;

    if( misc->kushaFlg ){
	oneLineSet((image2 + (Xpos + Wsize)), misc->fontSizeData,
		   msgData, Actor->size[WW], 1);
	oneLineSet((image3 + (Xpos + Wsize)), misc->fontSizeData,
		   msgData, Actor->size[WW], 2);
    }
    oneLineSet((image + (Xpos + Wsize)), misc->fontSizeData,
	       msgData, Actor->size[WW], 0);
}
#endif

/************************************************************************
 *	１文字の書き換え						*
 ************************************************************************/
void	oneMsgChenge(ACTOR *Actor, unshort msgNo,
		     unint Xpos, unint Ypos)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    t_utimg	*dispImg = misc->image;
    t_utimg	*dispImg2 = misc->image2;
    t_utimg	*dispImg3 = misc->image3;
    t_utimg	*readImg;
//    t_chsz	Wsize = Actor->size[WW];
    t_chsz	Wsize = (t_chsz)(Actor->size[WW] / 2);	/* 4bit TEST */
    char	readImg_works[8 + (ORG_WSIZE * ORG_HSIZE)];
    char	offset_cnt = *(MSG_FNTSZDT(Actor) + (unint)msgNo);

    /* 読み込むイメージデータの領域確保と初期化 */
    readImg = (t_utimg *)to_8ByteAlign(readImg_works);

#if JAPAN
    oneMsg_set((dispImg + (Xpos + ((unint)Wsize * Ypos))), readImg,
	       msgNo, Wsize, (t_chsz)offset_cnt);
    if( misc->kushaFlg ){
	oneMsg_set((dispImg2 + (Xpos + ((unint)Wsize * Ypos))), readImg,
		   msgNo, Wsize, (t_chsz)offset_cnt);
	oneMsg_set((dispImg3 + (Xpos + ((unint)Wsize * Ypos))), readImg,
		   msgNo, Wsize, (t_chsz)offset_cnt);
    }
#else
    oneMsg_set((dispImg + (Xpos + ((unint)Wsize * Ypos))), readImg,
	       msgNo, Wsize, (t_chsz)offset_cnt, 0);
    if( misc->kushaFlg ){
	oneMsg_set((dispImg2 + (Xpos + ((unint)Wsize * Ypos))), readImg,
		   msgNo, Wsize, (t_chsz)offset_cnt, 1);
	oneMsg_set((dispImg3 + (Xpos + ((unint)Wsize * Ypos))), readImg,
		   msgNo, Wsize, (t_chsz)offset_cnt, 2);
    }
#endif

    refreshPrio(ON);                  /* 表示優先順位処理をON */
}

/************************************************************************
 *	一番手前に持ってくる						*
 ************************************************************************/
static	void	MsgX_setpos(ACTOR *Actor)
{
    Actor->posf[XX] = Actor->wposf[XX] = get_framePosfLT_X();
    Actor->posf[YY] = Actor->wposf[YY] = get_framePosfLT_Y();
    Actor->posf[ZZ] = Actor->wposf[ZZ] = get_framePosfLT_Z()+0.01F;

    get_ratioOnZ(Actor) = -0.582193F;
    get_fovyOrgOnZ_X(Actor) = -434.822845F;
    get_fovyOrgOnZ_Y(Actor) =  -326.117126F;

#if 0
#if DEBUG
//    if(get_category(Actor->name) != IAM_MSG3) {
//	return;
//    }
    
    if(tool_chkButton(RIGHT)) {
	get_angOffsetX(Actor) += 1.0F;
//	get_fovyOrgOnZ_X(Actor) += 1.0F;
    }
    else if(tool_chkButton(LEFT)){
	get_angOffsetX(Actor) -= 1.0F;
//	get_fovyOrgOnZ_X(Actor) -= 1.0F;
    }
    if(tool_chkButton(UP)) {
	get_angOffsetY(Actor) += 1.0F;
//	get_fovyOrgOnZ_Y(Actor) += 1.0F;
    }
    else if(tool_chkButton(DOWN)){
	get_angOffsetY(Actor) -= 1.0F;
//	get_fovyOrgOnZ_Y(Actor) -= 1.0F;
    }
    if(tool_chkButton(C_BTN)) {
	get_scaleX(Actor) -= 0.01F;
    }
    else if(tool_chkButton(F_BTN)){
	get_scaleX(Actor) += 0.01F;
    }
    if(tool_chkButton(D_BTN)) {
	get_scaleY(Actor) -= 0.01F;
    }
    else if(tool_chkButton(E_BTN)){
	get_scaleY(Actor) += 0.01F;
    }
    if(tool_chkButton(START)) {
//	PRINTF("%f\n", get_ratioOnZ(Actor));
//	PRINTF("%f, %f\n", get_fovyOrgOnZ_X(Actor), get_fovyOrgOnZ_Y(Actor));
	PRINTF("offsetXY scaleXY %f, %f, %f, %f\n",
	       get_angOffsetX(Actor), get_angOffsetY(Actor),
	       get_scaleX(Actor), get_scaleY(Actor));
//	PRINTF("%d, %d\n",
//	       get_rposX(Actor), get_rposY(Actor));
//	get_rposX(Actor) = 100;
//	get_rposY(Actor) = 100;
//	PRINTF("%d, %d\n",
//	       get_rposX(Actor), get_rposY(Actor));
    }
#endif    
#endif    
}


/************************************************************************
 *	スクロール処理							*
 ************************************************************************/
static	void	msgScroll(t_utimg *dispImg,
			  int Wsize, int Hsize)
{
    int		i, j;

    for( i = 1; i < Hsize; i++ ){
	for( j = 0; j < Wsize; j++ ){
	    *(dispImg + ((i - 1) * Wsize) + j) = *(dispImg + (i * Wsize) + j);
	}
    }
}

#if !JAPAN
/****************************************************************
 *	文字をわさわさ動かす					*
 ****************************************************************/
static	void	wasawasaMoji(ACTOR *Actor)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    IMGINF	*imageInfo = IMGINFp_P(CASTIFp_P(Actor));
    t_pxci8	*image[3];

    if( misc->kushaFlg ){
	misc->count++;
	misc->loopCnt++;
	image[0] = misc->image;
	image[1] = misc->image2;
	image[2] = misc->image3;
	if( misc->count >= 9 ){
	    misc->count = 0;
	    misc->ptnNo++;
	    if( misc->ptnNo >= 3 )
		misc->ptnNo = 0;
	}
	if( misc->loopCnt >= 55){
	    misc->ptnNo = 0;
	    misc->count = 0;
	    misc->loopCnt = 0;
	}
	setP_image(imageInfo, image[misc->ptnNo]);
	UTTYIFp_P(imageInfo)->image = image[misc->ptnNo];
    }
}
#endif

/************************************************************************
 *	表示するメッセージのスクロール処理				*
 ************************************************************************/
static void Msg_DispScroll(ACTOR *Actor)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    int		idx = MSG_READNO(Actor);
    unshort	*msgData = MSG_DATA(Actor);
    t_utimg	*image = misc->image;
    t_utimg	*image2 = misc->image2;
    t_utimg	*image3 = misc->image3;
//    int		Wsize = (int)Actor->size[WW];
    int		Wsize = (int)Actor->size[WW] / 2;	/* 4bit TEST */
    int		Hsize = (int)Actor->size[HH];
    static unshort scrll_line;

#if !JAPAN
    wasawasaMoji(Actor);
#endif
    if( !MSG_SCRL(Actor) ){
	if( *(msgData + MSG_READNO(Actor)) == MSG_END ){
	    SetUp_MsgAction(Actor, MSG_INKEYCHECK);
	    return;
	}
    }
    msgScroll(image, Wsize, (Hsize + ORG_HSIZE));
    if( misc->kushaFlg ){
	msgScroll(image2, Wsize, (Hsize + ORG_HSIZE));
	msgScroll(image3, Wsize, (Hsize + ORG_HSIZE));
    }
    if( (Actor->timingCnt--) <= 1 ){
	if( *(msgData + idx) != MSG_END ){
	    Actor->timingCnt = ORG_HSIZE;
#if JAPAN
	    if( misc->kushaFlg ){
		oneLineSet((image2 + (Wsize * Hsize)), misc->fontSizeData,
			   (msgData + idx), Actor->size[WW]);
		oneLineSet((image3 + (Wsize * Hsize)), misc->fontSizeData,
			   (msgData + idx), Actor->size[WW]);
	    }
	    idx += oneLineSet((image + (Wsize * Hsize)), misc->fontSizeData,
			      (msgData + idx), Actor->size[WW]);
#else
	    if( misc->kushaFlg ){
		oneLineSet((image2 + (Wsize * Hsize)), misc->fontSizeData,
			   (msgData + idx), Actor->size[WW], 1);
		oneLineSet((image3 + (Wsize * Hsize)), misc->fontSizeData,
			   (msgData + idx), Actor->size[WW], 2);
	    }
	    idx += oneLineSet((image + (Wsize * Hsize)), misc->fontSizeData,
			      (msgData + idx), Actor->size[WW], 0);
#endif
	    MSG_READNO(Actor) = idx + 1;
	    idx = MSG_READNO(Actor);
	}
	MSG_SCRL(Actor)--;
	if( *(msgData + idx) >= MSGSCRL ){
	    if( *(msgData + idx) < MSG_END ){	/* データ終了？ */
		scrll_line = (unshort)(*(msgData + idx) - MSGSCRL);
		MSG_READNO(Actor)++;
		idx = MSG_READNO(Actor);
	    } else {
		scrll_line = 0;
	    }
	}
	if( !MSG_SCRL(Actor) ){
	    MSG_SCRL(Actor) = scrll_line;
	    scrll_line = 0;
	    if( *(msgData + idx) == MSG_END ){	/* データ終了？ */
		Actor->timingCnt = 0;
	    } else {
		SetUp_MsgAction(Actor, MSG_INKEYCHECK);
	    }
	}
    }
}

static	void	MsgX_DispScroll(ACTOR *Actor)
{
    MsgX_setpos(Actor);
    Msg_DispScroll(Actor);
    SetUp_MsgXAction(Actor, Actor->status);	//actStatusでなくstatusなので注意
}

/************************************************************************
 *	キー入力チェック						*
 ************************************************************************/
static void Msg_InKeyCheck(ACTOR *Actor)
{
#if !JAPAN
    wasawasaMoji(Actor);
#endif
    if( chkTrigger(CONT_A) || chkTrigger(CONT_B) ){
	if( Actor->timingCnt ){
	    SetUp_MsgAction(Actor, MSG_DISPSCROLL);
	}
    }
}

static	void	MsgX_InKeyCheck(ACTOR *Actor)
{
    MsgX_setpos(Actor);
    SetUp_MsgXAction(Actor, Actor->status);	//外部から切り替える
						//actStatusでなくstatusなので注意
}

/************************************************************************
 *	待ち状態							*
 ************************************************************************/
static void Msg_Wait(ACTOR *Actor)
{
#if !JAPAN
    wasawasaMoji(Actor);
#endif
    if( Actor->peActor ){
	if(Actor->peActor->drawType != NULL_DRAWTYPE) {
	    if( Actor->peActor->hidden != HIDDEN )
		Actor->hidden = DISPLAY;		/* 表示 */
//		Actor->hidden = HIDDEN;		/* 表示 */
	}
    } else {
	Actor->hidden = DISPLAY;		/* 表示 */
    }
    SetUp_MsgAction(Actor, Actor->status);	//actStatusでなくstatusなので注意
}

static	void	MsgX_Wait(ACTOR *Actor)
{
    MsgX_setpos(Actor);
    Msg_Wait(Actor);
    SetUp_MsgXAction(Actor, Actor->status);	//actStatusでなくstatusなので注意
}


/************************************************************************
 *	メッセージアクターの消去					*
 ************************************************************************/
void	Msg_Delete(ACTOR *Actor)
{
    if(DWIFp_P(Actor)) free_drawInfo(Actor);
    Actor->drawType = NULL_DRAWTYPE;
    set_deconstProc(Actor, free_Actor);
    refreshPrio(ON);                  /* 表示優先順位処理をON */    
}

/************************************************************************
 *	キャラクターパターンのフリップ情報	*
 ************************************************************************/
static	unint	Msg_flipInfo[] = { OFF_FLIP | V_FLIP };

/************************************************************************
 *	メッセージの初期表示データセット				*
 ************************************************************************/
static	void	init_msgSet(ACTOR *Actor)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    int		LineNo = 0;
    int		maxHLineCnt = (Actor->size[HH] + ORG_HSIZE) / ORG_HSIZE;
    int		pos = 0;
    t_utimg	*dispImg = misc->image;
    t_utimg	*dispImg2 = misc->image2;
    t_utimg	*dispImg3 = misc->image3;
    t_utimg	*fontSzDt = MSG_FNTSZDT(Actor);
    unshort	*msgData = MSG_DATA(Actor);
    int		idx = MSG_READNO(Actor);
    t_chsz	Wsize = Actor->size[WW];

    MSG_SCRL(Actor) = MSG1SCRL - MSGSCRL;
    Actor->timingCnt = ORG_HSIZE;
    for( LineNo = 0; LineNo < maxHLineCnt; LineNo++ ){
	pos = LineNo * ((Wsize / 2) * ORG_HSIZE);
#if JAPAN
	if( misc->kushaFlg ){
	    oneLineSet((dispImg2+pos), fontSzDt, (msgData + idx), Wsize);
	    oneLineSet((dispImg3+pos), fontSzDt, (msgData + idx), Wsize);
	}
	idx += oneLineSet((dispImg+pos), fontSzDt, (msgData + idx), Wsize);
#else
	if( misc->kushaFlg ){
	    oneLineSet((dispImg2+pos), fontSzDt, (msgData + idx), Wsize, 1);
	    oneLineSet((dispImg3+pos), fontSzDt, (msgData + idx), Wsize, 2);
	}
	idx += oneLineSet((dispImg+pos), fontSzDt, (msgData + idx), Wsize, 0);
#endif
	idx++;
	if( *(msgData + idx) == MSG_END ){
	    Actor->timingCnt = 0;
	    break;
	}
	if( *(msgData + idx) >= MSGSCRL ){
	    MSG_SCRL(Actor) = (unshort)(*(msgData + idx) - MSGSCRL);
	    idx++;
	}
    }

    MSG_READNO(Actor) = idx;
}

/************************************************************************
 *	メッセージ関係のメモリー解放					*
 ************************************************************************/
static	void	msg_free(void *miscInfo)
{
    if(((MSGINFO *)miscInfo)->category == IAM_MSG) {
	exist_msg = False;		//メッセージボードの複数発生を防ぐ為
    }
    
    if(((MSGINFO *)miscInfo)->msg_data)
 	Free(((MSGINFO *)miscInfo)->msg_data);
#if JAPAN
    if(((MSGINFO *)miscInfo)->fontSizeData)
 	Free(((MSGINFO *)miscInfo)->fontSizeData);
#endif
    if(((MSGINFO *)miscInfo)->image)
	Free(((MSGINFO *)miscInfo)->image);

    if( ((MSGINFO *)miscInfo)->kushaFlg ){
	if(((MSGINFO *)miscInfo)->image2)
	    Free(((MSGINFO *)miscInfo)->image2);
	if(((MSGINFO *)miscInfo)->image3)
	    Free(((MSGINFO *)miscInfo)->image3);
    }
    
    if(((MSGINFO *)miscInfo)->texInfo)
	free_texInfo(((MSGINFO *)miscInfo)->texInfo);

#if !S_MISC  // 1997/9/12 by otsuki.
    Free(miscInfo);
#endif
}

/************************************************************************
 *	メッセージデータテーブルの読み込み				*
 ************************************************************************/
static	int readRom_mesgData_tbl(ACTOR *Actor, int DispMsgNo)
{
    MSGINFO	*misc = MSGINFO_P(Actor);
    char	works[8 + sizeof(MSGDATAINFO)];
    MSGDATAINFO	*msgRomData;
    size_t	size;
    t_pxci8	*image, *image2, *image3;
    unshort	*utPal = NULL;
    IMGINF	*imageInfo = IMGINFp_P(CASTIFp_P(Actor));
    UTTYIF	*utypInfo = UTTYIFp_P(imageInfo);

#if JAPAN
    /* フォントサイズデータの領域確保と読み込み */
    MSG_FNTSZDT(Actor) = (unchar *)rMalloc(font_size_data_ORGSIZ);
    if( !MSG_FNTSZDT(Actor) ){
#if DEBUG
	PRINTF("readRom_mesgData_tbl:メモリ確保の失敗!!\n");
#endif
	return(False);
    }
    readrom_data(MSG_FNTSZDT(Actor), font_size_data, font_size_data_CMPSIZ);
#endif
#if !JAPAN
    MSG_FNTSZDT(Actor) = FontSizeData;
#endif

    /* テーブルデータの読み込み */
    msgRomData = (MSGDATAINFO *)to_8ByteAlign(works);
    readrom_data(msgRomData, (mesgData_tbl + DispMsgNo), sizeof(MSGDATAINFO));

    /* メッセージデータポインタのセット */
    MSG_DATA(Actor) = (unshort *)rMalloc((unint)msgRomData->msgDataSize);
    if( !MSG_DATA(Actor) ){
#if DEBUG
	PRINTF("readRom_msgData:メモリ確保の失敗!\n");
#endif
	return(False);
    }
    readrom_data(MSG_DATA(Actor), msgRomData->msgDataPos,
		 (unint)msgRomData->msgDataSize);

    /* 表示するサイズの設定 */
    Actor->size[WW] = msgRomData->dispWsize;
    Actor->size[HH] = msgRomData->dispHsize;
//    size = (size_t)(Actor->size[WW] * (Actor->size[HH] + ORG_HSIZE));
    size = (size_t)((Actor->size[WW] / 2) * (Actor->size[HH] + ORG_HSIZE));

    /* 表示用イメージバッファの確保と作成 */
    image = (char *)rMalloc(size);
    if(image == NULL) {
#if DEBUG
	PRINTF("readRom_msgData:表示用イメージバッファの確保の失敗!\n");
#endif
	return(False);
    }
    fillArea_c(image, size, 0x00);
    if( misc->kushaFlg ){
	image2 = (char *)rMalloc(size);
	image3 = (char *)rMalloc(size);

	if(image2 == NULL) {
#if DEBUG
	    PRINTF("readRom_msgData:表示用イメージバッファの確保の失敗!\n");
#endif
	    return(False);
	}
	if(image3 == NULL) {
#if DEBUG
	    PRINTF("readRom_msgData:表示用イメージバッファの確保の失敗!\n");
#endif
	    return(False);
	}
	fillArea_c(image2, size, 0x00);
	fillArea_c(image3, size, 0x00);
    }
    setP_image(imageInfo, image);
    utypInfo->image = image;
    misc->image = image;
    misc->image2 = image2;
    misc->image3 = image3;
    init_msgSet(Actor);

    /* RAM上のイメージ情報領域の確保 */
    utypInfo->texInfo = (void *)alloc_texInfo();
    MSG_TEXINFO(Actor) = utypInfo->texInfo;
    if( !utypInfo->texInfo ){
#if DEBUG
	PRINTF("readRom_mesgData_tbl:メモリ確保の失敗!\n");
#endif
	return(False);
    }

#if !JAPAN
    /* パターン切り替えフラグの初期化 */
    misc->ptnNo = 0;
    misc->count = 0;
    misc->loopCnt = 0;
#endif
    regist_spriteInfo(utypInfo->texInfo, (t_altp)INTSY_4,
		      (t_imsz)size, image, utPal,
		      (int)Actor->size[WW], (int)Actor->size[HH],
		      NULL);
    return(True);
}

/************************************************************************
 *	メッセージアクターの発生					*
 *			成功ならアクターのポインタ, 失敗ならNULLを返す	*
 ************************************************************************/
ACTOR	*make_MsgActor_byCategory(t_ctgry Category, 
				  int DispMsgNo,
				  float posX, float posY, float posZ, int flg)
{
    ACTOR	*msgActor;
    t_ocatt	no;

//    if( findCategory(Category) ){
//#if DEBUG
//	PRINTF("make_Message:すでにメッセージが表示してある!!\n");
//#endif
//	return NULL;
//    }

    if(Category == IAM_MSG) {
	if(exist_msg != False) {
#if DEBUG
	    PRINTF("すでにメッセージが表示してある!!\n");
#endif
	    return NULL;
	}
    }

#if JAPAN
    if( DispMsgNo >= MSG_DTend ){
#if DEBUG
	PRINTF("make_Message:ありえないメッセージをセットしている!!\n");
#endif
	return NULL;
    }
#endif

#if !JAPAN
#if !EUROPE
    if( is_JAPAN() )
	DispMsgNo += MSG_DTend;
#else
    if( is_FRANCE() )		/* フランス語？ */
	DispMsgNo += MSG_DTend;
    if( is_GERMANY() ){		/* ドイツ語？ */
	DispMsgNo += MSG_DTend;
	DispMsgNo += MSG_DTend;
    }
#endif
#endif

    /* アクターの発生 */
    no = affix_actorNum(Category);
    if ( no == NULL_OBJNUMBER ) {
#if DEBUG
	PRINTF("IAM_MSGの背番号がない !!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n");
#endif
	return NULL;
    }
    msgActor = birth_dynamicActor(uniqueName_OBJ(Category, DATA_PARTY, no),
				  posX, posY, posZ);
    /* キャラクターナンバー設定 */
//    set_charNumOBJ(msgActor->name, affix_actorNum(Category));

    /* 雑多情報の初期化 */
    msgActor->free_miscInfo = msg_free;
    msgActor->miscInfo = (MSGINFO *)rMalloc(sizeof(MSGINFO));
    if( !msgActor->miscInfo ){
#if DEBUG
	PRINTF(":メモリ確保の失敗!\n");
#endif
	discontinue_actor(msgActor);
	return(NULL);
    }
    
    clearArea_c(msgActor->miscInfo, sizeof(MSGINFO));

    /* くしゃくしゃフラグ設定 */
    ((MSGINFO *)msgActor->miscInfo)->kushaFlg = flg;
#if JAPAN
    /* このフラグは まだ使っていないので"０"にしている */
    ((MSGINFO *)msgActor->miscInfo)->kushaFlg = 0;
#endif

    MSG_NO(msgActor) = DispMsgNo;
    MSG_SLCT(msgActor) = -1;
    MSG_READNO(msgActor) = 0;

//    msgActor->drawType = SPRITE_X;
    msgActor->drawType = SPRITE;
    if(readRom_mesgData_tbl(msgActor, DispMsgNo) == False) {
#if DEBUG
	PRINTF(":メモリ確保の失敗!\n");
#endif
	discontinue_actor(msgActor);
	return(NULL);
    }
    
    if(!make_drawInfo(msgActor)) {
#if DEBUG
	PRINTF(":描画領域の確保に失敗した!\n");
#endif
	discontinue_actor(msgActor);
	return(NULL);
    }

    msgActor->hidden = HIDDEN;		/* 消去 */

    setP_flipInfo(DWIFTP_P(msgActor), Msg_flipInfo);

    SetUp_MsgAction(msgActor, MSG_WAIT);

    msgActor->retireProc = NULL;    /* 退場しない */

#if 0
    /* キャストに登録 */
    regist_soonBirthEntry(msgActor);
#endif
    refreshPrio(ON);                  /* 表示優先順位処理をON */

    /* メッセージボードの複数発生を防ぐ為 */
    MSG_CATEGORY(msgActor) = Category;
    if(Category == IAM_MSG) {
	exist_msg = True;		
    }

    return msgActor;
}

ACTOR	*make_MsgXActor_byCategory(t_ctgry Category,
				   int DispMsgNo,
				   float offsetX, float offsetY,
				   float scaleX, float scaleY)
{
    ACTOR *ac = make_MsgActor_byCategory(Category, DispMsgNo, 0.0F, 74.0F, -896.0F, 0);
//    ACTOR *ac = make_MsgActor_byCategory(Category, DispMsgNo, 0.0F, 74.0F, 6.0F);

    if(ac != NULL) {
	SetUp_MsgXAction(ac, MSG_WAIT);
	get_angOffsetX(ac) = offsetX;
	get_angOffsetY(ac) = offsetY;
	get_scaleX(ac) *= scaleX;
	get_scaleY(ac) *= scaleY;
	set_topPrio(ac);
    }
    
    return(ac);
}

/************************************************************************
 *	各アクションのための設定					*
 ************************************************************************/

static	void	SetUp_MsgAction(ACTOR *Actor, t_st status)
{
    static PROCESS process[MSG_ACTION] = {
	NULL,
	/* 待ち状態 */
	Msg_Wait,
	/* キー入力チェック */
	Msg_InKeyCheck,
	/* 表示するメッセージのスクロール処理 */
	Msg_DispScroll,
    };

    Actor->process = process[(Actor->status = status)];
						//actStatusでなくstatusなので注意
}


static	void	SetUp_MsgXAction(ACTOR *Actor, t_st status)
{
    static PROCESS process[MSG_ACTION] = {
	NULL,
	/* 待ち状態 */
	MsgX_Wait,
	/* キー入力チェック */
	MsgX_InKeyCheck,
	/* 表示するメッセージのスクロール処理 */
	MsgX_DispScroll,
    };

    Actor->process = process[(Actor->status = status)];
						//actStatusでなくstatusなので注意
}
