///////// YAYA　ゴーストテンプレート
///////// メニュー処理辞書

//**** 注意 ********************************************************************
// この辞書はゴーストテンプレートに必要な共通処理をまとめたものです。
// 通常はここをいじる必要はありません。
//**** 注意 ********************************************************************


//******************************************************************************
//起動・終了初期化
//******************************************************************************

OnSystemLoad.AYATEMPLATE
{
	AYATEMPLATE.installedplugin_name = IARRAY
	AYATEMPLATE.installedplugin_id = IARRAY
	AYATEMPLATE.stroke = IARRAY
	AYATEMPLATE.strokeid = IARRAY
	AYATEMPLATE.stroketimer = IARRAY
	AYATEMPLATE.mikireflag = 0
	AYATEMPLATE.LastTalk = ''
}

OnSystemUnload.AYATEMPLATE
{
	//ERASEVAR('AYATEMPLATE.installedplugin_name')
	//ERASEVAR('AYATEMPLATE.installedplugin_id')
	ERASEVAR('AYATEMPLATE.stroke')
	ERASEVAR('AYATEMPLATE.strokeid')
	ERASEVAR('AYATEMPLATE.stroketimer')
	ERASEVAR('AYATEMPLATE.mikireflag')
	ERASEVAR('AYATEMPLATE.LastTalk')
}

//******************************************************************************
//ランダムトーク
//******************************************************************************

OnAITalk
{
	OnAiTalk
}

//------------------------------------------------------------------------------
//OnAiTalkイベント
//------------------------------------------------------------------------------
OnAiTalk
{
	//---- 通常のランダムトーク、ただしチェイン中はチェイントーク
	if SHIORI3FW.IsChain {
		AYATEMPLATE.LastTalk = ChainTalk
	}
	else {
		AYATEMPLATE.LastTalk = RandomTalk
	}
	AYATEMPLATE.LastTalk
}


//******************************************************************************
//メニュー関係
//******************************************************************************

//------------------------------------------------------------------------------
//イベント処理
//選択肢ID/アンカーIDをそのまま関数名として書けるようになります。
//------------------------------------------------------------------------------

OnChoiceSelect
{
	if ISFUNC(reference[0]) {
		EVAL(reference[0])
	}
}

OnAnchorSelect
{
	if RE_SEARCH(reference[0],'^(http|https)://') {
		if basewarename == 'SSP' {
			"\C"
		}
		--
		"\j[%(AYATEMPLATE.EscapeText(reference[0]))]"
	}
	else {
		if ISFUNC(reference[0]) {
			EVAL(reference[0])
		}
	}
}

//------------------------------------------------------------------------------
//メニュー横1行反転
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//通常メニュー
AYATEMPLATE.MenuItem
{
	_linefeed = 100
	_n = _argc
	if _n >= 3 {
		if GETTYPE(_argv[_n-1]) == 1 { //整数
			_linefeed = _argv[_n-1]
			_n -= 1
		}
	}

	_lftext = ''
	while _linefeed > 0 {
		if _linefeed >= 100 {
			_lftext += '\n'
		}
		else {
			_lftext += '\n[half]'
		}
		_linefeed -= 100
	}
	
	_lftext
	--
	'\![*]\q['
	--
	AYATEMPLATE.EscapeText( SHIORI3FW.MakeJustText(_argv[0],46) )
	--
	_s = ''
	for _i = 1 ; _i < _n ; _i++ {
		_s += ',' + AYATEMPLATE.EscapeText(_argv[_i])
	}
	_s
	--
	']'
}

//------------------------------------------------------------------------------
//エスケープ
AYATEMPLATE.EscapeText
{
	_r = _argv[0]
	if RE_SEARCH(_r,'["\[\]]') {
		'"' + REPLACE(_r,'"','""') + '"'
	}
	else {
		_r
	}
}

//******************************************************************************
//リンクメニュー
//******************************************************************************
AYATEMPLATE.LinkMenuConvert
{
    _text = ''
    _data = IARRAY
    for _i = 0 ; _i < _argc ; _i++ {
        _data = (RE_SPLIT(_argv[_i],'[ \t]*\|[ \t]*'),'','') //ダミー
        _text += "%(_data[0])%(CHR(1))%(_data[1])%(CHR(1))%(_data[2])%(CHR(1))%(CHR(2))"
    }
    _text;
}

//******************************************************************************
//切り替えコア
//******************************************************************************

AYATEMPLATE.ExecuteChangeCallTalk
{
	AYATEMPLATE.ExecuteGhostNameFunc(_argv[0],reference[0],_argv[1])
}


//******************************************************************************
//切り替え/コミュ共通コア（ゴースト名付加関数呼び出し）
//_argv[0]	関数名ベース
//_argv[1]	ゴースト名（自動的にシステム文字エスケープされる）
//			ゴースト名が空の場合は_argv[2]の評価へ
//			「関数名ベース＋ゴースト名」の関数が無い場合は「ベースOther」
//_argv[2]	上記の関数が存在しなかった場合、呼ぶ関数名（Optional)
//******************************************************************************
AYATEMPLATE.ExecuteGhostNameFunc
{
	_ghostname = SHIORI3FW.TranslateSystemChar(_argv[1])

	if _ghostname != '' {
		_funcname = "%(_argv[0])_%(_ghostname)"
		if ISFUNC(_funcname) {
			_script = EVAL(_funcname)
			if STRLEN(_script) != 0 {
				_script
				return
			}
		}

		_funcname = "%(_argv[0])Other"
			if ISFUNC(_funcname) {
			_script = EVAL(_funcname)
			if STRLEN(_script) != 0 {
				_script
				return
			}
		}
	}

	if _argc >= 2 {
		_script = EVAL(_argv[2])
		if STRLEN(_script) != 0 {
			REPLACE(_script,"\-","")
			return
		}
	}
}

//******************************************************************************
//コミュ関係
//******************************************************************************

//こっちから話しかける
AYATEMPLATE.ExecuteTalkToTalk
{
	//まず話しかける相手を決める
	_ghostname=AYATEMPLATE.GetRandomSakuraName()
	res_reference0=_ghostname
	
	DUMPVAR()
	
	//共通コアを使って関数呼び出し
	AYATEMPLATE.ExecuteGhostNameFunc('TalkTo',_ghostname,'TalkToNobody')
}

//話しかけに応答する
AYATEMPLATE.ExecuteReplyToTalk
{
	AYATEMPLATE.ExecuteGhostNameFunc('ReplyTo',_argv[0])
}

//話しかける相手をランダムに決定
AYATEMPLATE.GetRandomSakuraName
{
	_ghostlist=IARRAY
	//FMOが使える場合はFMOから読みます
	if ISFUNC('SHIORI3FW.RefreshFMOTable') {
		SHIORI3FW.RefreshFMOTable
		_ghostlist=SHIORI3FW.SakuraNameList
	}else{
		_ghostlist=ghostexlist
	}
	if ARRAYSIZE(_ghostlist)>0 {
		ANY(_ghostlist)
	}else{
		''
	}
}

//起動中ゴーストリスト取得
On_otherghostname
{
	// 現在起動中のゴーストのリストを作成する
	ghostexlist = reference
	ghostexcount = ARRAYSIZE(ghostexlist)
}



//******************************************************************************
//マウスイベントコア
//******************************************************************************
AYATEMPLATE.MouseEventExec
{
	_fname = _argv[0] + TOSTR(reference[3]) + TOSTR(reference[4])
	if ISFUNC(_fname) {
		EVAL(_fname)
		return
	}
	
	_fname = _argv[0] + TOSTR(reference[3])
	if ISFUNC(_fname) {
		EVAL(_fname)
		return
	}
}

AYATEMPLATE.MouseMoveWheelFunc
{
	_side = TOINT(reference[3])
	if _argv[0] {
		_side += 10
	}
	
	if AYATEMPLATE.strokeid[_side] == reference[4] {
		if (GETTICKCOUNT - TOINT(AYATEMPLATE.stroketimer[_side])) > 1500 {
			AYATEMPLATE.stroke[_side] = 1
		}
		AYATEMPLATE.stroketimer[_side] = GETTICKCOUNT
		
		_stroke = TOINT(AYATEMPLATE.stroke[_side])
		_stroke += 1
		AYATEMPLATE.stroke[_side] = _stroke
		
		if _stroke >= _argv[1] {
			if 'talking' !_in_ status {
				AYATEMPLATE.MouseEventExec(_argv[2]);
				AYATEMPLATE.stroke[_side] = 1
			}
		}
	}
	else {
		AYATEMPLATE.stroke[_side] = 1
		AYATEMPLATE.stroketimer[_side] = GETTICKCOUNT
		AYATEMPLATE.strokeid[_side] = reference[4]
	}
}

//------------------------------------------------------------------------------
//プラグイン
//------------------------------------------------------------------------------
On_installedplugin : void
{
	_n = ARRAYSIZE(reference)
	AYATEMPLATE.installedplugin_name = IARRAY
	AYATEMPLATE.installedplugin_id = IARRAY
	
	for _i = 0 ; _i < _n ; _i++ {
		_a = RE_SPLIT(reference[_i],'[,\x1]')
		
		AYATEMPLATE.installedplugin_name ,= _a[0]
		AYATEMPLATE.installedplugin_id ,= _a[1]
	}
}

AYATEMPLATE.IsPluginExist
{
	if _argc == 0 {
		0
		return
	}
	
	if ASEARCH(_argv[0],AYATEMPLATE.installedplugin_id) >= 0 {
		1
		return
	}
	if ASEARCH(_argv[0],AYATEMPLATE.installedplugin_name) >= 0 {
		1
		return
	}
	0
}
