戻る

2006/3/17カスタムAIモルティシアVer1



-- ※"require"のある行からコピペで、AI.luaへ上書き貼り付けして使用してください※
-- ※使用は自己責任で※
-------------------------------------------------
-- デフォルトで先行型になっているので、非先行型が好みの方は
-- function GetMyEnemy (myid)内の GetMyEnemyB (myid) を GetMyEnemyA (myid)に変えて使用してください
-------------------------------------------------
-- 参考AI
-- The Winter's Tale
-- http://www.sgv417.jp/~winter/index.html

-- 工体研究所rev10
-- http://blog.livedoor.jp/aidev/archives/50119284.html

-- アルケミストのAIを語るスレ2
-- http://enif.mmobbs.com/test/read.cgi/livero/1140506800/l50
-------------------------------------------------

require "./AI/Const.lua"
require "./AI/Util.lua"		
		
-----------------------------
-- state
-----------------------------
IDLE_ST					= 0	-- 待機状態
FOLLOW_ST				= 1	-- 所持者追従状態	
CHASE_ST				= 2	-- ターゲット追跡状態
ATTACK_ST				= 3	-- 攻撃状態
MOVE_CMD_ST				= 4
STOP_CMD_ST				= 5
ATTACK_OBJECT_CMD_ST			= 6
ATTACK_AREA_CMD_ST			= 7
PATROL_CMD_ST				= 8
HOLD_CMD_ST				= 9
SKILL_OBJECT_CMD_ST			= 10
SKILL_AREA_CMD_ST			= 11
FOLLOW_CMD_ST				= 12
----------------------------

------------------------------------------
-- global variable
------------------------------------------
MyState				= IDLE_ST	-- ホムの現在状態(初期値は待機状態)
MyEnemy				= 0		-- ホムがターゲットしている敵のキャラクターID
MyDestX				= 0		-- ホムの目標座標X(移動先 / スキル使用座標)
MyDestY				= 0		-- ホムの目標座標Y(移動先 / スキル使用座標)
MyPatrolX				= 0		-- ホムの偵察目的地 x座標
MyPatrolY				= 0		-- ホムの偵察目的地 y座標
ResCmdList				= List.new()	-- 予約コマンドリスト
MyID					= 0		-- ホムンクルス自身のid
MySkill					= 0		-- ホムンクルスの使用スキル
MySkillLevel				= 0		-- ホムンクルスの使用スキルレベル
------------------------------------------
------------------------------------------
-- 追加分global variable
------------------------------------------

------------------------------------------


-- 手動(予約)処理
------------- command process  ---------------------
-- 移動コマンド処理
function	OnMOVE_CMD (x,y)
	TraceAI ("OnMOVE_CMD")

	if ( x == MyDestX and y == MyDestY and MOTION_MOVE == GetV(V_MOTION,MyID)) then
		return		-- 目的地と現在地が同一の場合は、処理しない
	end

	local curX, curY = GetV (V_POSITION,MyID)		-- ホム自身の座標を代入
	-- ホムの自動戻り処理
	if (math.abs(x-curX)+math.abs(y-curY) > 15) then	-- 目的地が一定距離(15セル)以上なら (サーバーで遠距離は処理しないため)
		List.pushleft (ResCmdList,{MOVE_CMD,x,y})	-- 元の目的地への移動を予約する
		x = math.floor((x+curX)/2)					-- 予約した地点と現在位置の中間地点へ移動する
		y = math.floor((y+curY)/2)					-- 同上
	end
	-- 移動処理
	Move (MyID,x,y)	
	MyState = MOVE_CMD_ST -- 移動状態へ、以下初期化
	MyDestX = x
	MyDestY = y
	MyEnemy = 0
	MySkill = 0
end

-- 停止コマンド処理
function	OnSTOP_CMD ()

	TraceAI ("OnSTOP_CMD")

	if (GetV(V_MOTION,MyID) ~= MOTION_STAND) then	-- 現在の命令がSTANDでない場合
		Move (MyID,GetV(V_POSITION,MyID))	-- ホム自身の座標へ移動(その場で停止)
	end
	MyState = IDLE_ST	-- 待機状態へ、以下初期化
	MyDestX = 0
	MyDestY = 0
	MyEnemy = 0
	MySkill = 0
end



-- 指定目標への攻撃コマンド処理
function	OnATTACK_OBJECT_CMD (id)

	TraceAI ("OnATTACK_OBJECT_CMD")

	MySkill = 0
	MyEnemy = id 		-- 指定目標のidをホムのターゲットに指定
	MyState = CHASE_ST 	-- 指定目標を追跡

end

-- 指定座標への攻撃コマンド処理
function	OnATTACK_AREA_CMD (x,y)

	TraceAI ("OnATTACK_AREA_CMD")

	if (x ~= MyDestX or y ~= MyDestY or MOTION_MOVE ~= GetV(V_MOTION,MyID)) then
		Move (MyID,x,y)		-- 座標目標と指定座標が違う、または現在の状態が移動状態ではない場合、指定座標まで移動
	end
	MyDestX = x
	MyDestY = y
	MyEnemy = 0	-- ターゲットリセット
	MyState = ATTACK_AREA_CMD_ST	-- 範囲攻撃開始
	
end


-- 偵察(移動)コマンドの処理
function	OnPATROL_CMD (x,y)

	TraceAI ("OnPATROL_CMD")

	MyPatrolX , MyPatrolY = GetV (V_POSITION,MyID)	-- 偵察目的地に現在のホムの座標を代入
	MyDestX = x		-- 指定座標を目標座標に設定
	MyDestY = y
	Move (MyID,x,y)
	MyState = PATROL_CMD_ST		-- 偵察開始

end


-- ターゲット指定コマンド
function	OnHOLD_CMD ()

	TraceAI ("OnHOLD_CMD")

	MyDestX = 0
	MyDestY = 0
	MyEnemy = 0
	MyState = HOLD_CMD_ST

end


-- 指定目標へスキルを使うコマンドの処理
function	OnSKILL_OBJECT_CMD (level,skill,id)

	TraceAI ("OnSKILL_OBJECT_CMD")

	MySkillLevel = level
	MySkill = skill
	MyEnemy = id
	MyState = CHASE_ST 

end

-- 指定座標へスキルを使うコマンドの処理
function	OnSKILL_AREA_CMD (level,skill,x,y)

	TraceAI ("OnSKILL_AREA_CMD")

	Move (MyID,x,y)
	MyDestX = x
	MyDestY = y
	MySkillLevel = level
	MySkill = skill
	MyState = SKILL_AREA_CMD_ST
	
end


-- 主人追尾コマンドの処理
function	OnFOLLOW_CMD ()

	-- 待機命令は、待機状態と休息状態を互いに転換させる
	if (MyState ~= FOLLOW_CMD_ST) then	-- 現在状態が待機コマンド実行中でない場合
		MoveToOwner (MyID)		-- ホム所持者(ケミ)の元まで移動
		MyState = FOLLOW_CMD_ST
		MyDestX, MyDestY = GetV (V_POSITION,GetV(V_OWNER,MyID)) -- 所持者の座標を目標に指定
		MyEnemy = 0 
		MySkill = 0
		TraceAI ("OnFOLLOW_CMD")
	else
		MyState = IDLE_ST		-- 待機コマンド実行中だった場合、待機状態に
		MyEnemy = 0 
		MySkill = 0
		TraceAI ("FOLLOW_CMD_ST --> IDLE_ST")
	end

end


-- 手動(予約)処理、本体
function	ProcessCommand (msg)

	if		(msg[1] == MOVE_CMD) then	-- msg[1]が移動コマンドだった場合
		OnMOVE_CMD (msg[2],msg[3])		-- msg[2]とmsg[3]の値をそれぞれOnMOVE_CMDのxとyへ
		TraceAI ("MOVE_CMD")
	elseif	(msg[1] == STOP_CMD) then		-- msg[1]が停止コマンドだった場合
		OnSTOP_CMD ()
		TraceAI ("STOP_CMD")
	elseif	(msg[1] == ATTACK_OBJECT_CMD) then	-- msg[1]が指定目標への攻撃コマンドだった場合
		OnATTACK_OBJECT_CMD (msg[2])		-- msg[2]をOnATTACK_OBJECT_CMDのidへ
		TraceAI ("ATTACK_OBJECT_CMD")
	elseif	(msg[1] == ATTACK_AREA_CMD) then	-- msg[1]が指定座標への攻撃コマンドだった場合
		OnATTACK_AREA_CMD (msg[2],msg[3])
		TraceAI ("ATTACK_AREA_CMD")
	elseif	(msg[1] == PATROL_CMD) then		-- msg[1]が偵察(移動)コマンドだった場合
		OnPATROL_CMD (msg[2],msg[3])
		TraceAI ("PATROL_CMD")
	elseif	(msg[1] == HOLD_CMD) then		-- msg[1]がターゲット指定のコマンドだった場合
		OnHOLD_CMD ()
		TraceAI ("HOLD_CMD")
	elseif	(msg[1] == SKILL_OBJECT_CMD) then	-- msg[1]が指定座標へのスキルコマンドだった場合
		OnSKILL_OBJECT_CMD (msg[2],msg[3],msg[4],msg[5])	-- それぞれ、OnSKILL_OBJECT_CMD (level,skill,id)へ代入
		TraceAI ("SKILL_OBJECT_CMD")
	elseif	(msg[1] == SKILL_AREA_CMD) then
		OnSKILL_AREA_CMD (msg[2],msg[3],msg[4],msg[5])		-- それぞれ、OnSKILL_AREA_CMD (level,skill,x,y)へ代入
		TraceAI ("SKILL_AREA_CMD")
	elseif	(msg[1] == FOLLOW_CMD) then		-- msg[1]が待機コマンドだった場合
		OnFOLLOW_CMD ()
		TraceAI ("FOLLOW_CMD")
	end
end


--------------------------------------------------
-- 基本処理
-------------- state process  --------------------

-- 待機処理
function	OnIDLE_ST ()
	
	TraceAI ("OnIDLE_ST")

	local cmd = List.popleft(ResCmdList)
	if (cmd ~= nil) then		
		ProcessCommand (cmd)	-- 予約コマンド処理 
		return 
	end

	local	object = GetOwnerEnemy (MyID)	-- 所持者(ケミ)のidをobjectへ
	if (object ~= 0) then			-- MYOWNER_ATTACKED_IN
		MyState = CHASE_ST
		MyEnemy = object
		TraceAI ("IDLE_ST -> CHASE_ST : MYOWNER_ATTACKED_IN")
		return 
	end

	object = GetMyEnemy (MyID)		-- ホムの敵のidをobjectへ
	if (object ~= 0) then			-- ATTACKED_IN
		MyState = CHASE_ST
		MyEnemy = object
		TraceAI ("IDLE_ST -> CHASE_ST : ATTACKED_IN")
		return
	end

	local distance = GetDistanceFromOwner(MyID)
	if ( distance > 2 or distance == -1) then		-- MYOWNER_OUTSIGNT_IN
		MyState = FOLLOW_ST
		TraceAI ("IDLE_ST -> FOLLOW_ST")
		return;
	end

end



-- 主人のところへ戻る
function	OnFOLLOW_ST ()

	TraceAI ("OnFOLLOW_ST")

	if (GetDistanceFromOwner(MyID) <= 2) then		--  DESTINATION_ARRIVED_IN 
		MyState = IDLE_ST
		TraceAI ("FOLLOW_ST -> IDLW_ST")
		return;
	elseif (GetV(V_MOTION,MyID) == MOTION_STAND or GetV(V_MOTION,MyID) == MOTION_MOVE) then
		local x,y = GetV(V_POSITION,GetV(V_OWNER,MyID))
		Move (MyID,x,y)
		TraceAI ("FOLLOW_ST -> FOLLOW_ST")
		return;
	end

end



-- 追跡処理
function	OnCHASE_ST ()

	TraceAI ("OnCHASE_ST")

	if (true == IsOutOfSight(MyID,MyEnemy)) then	-- ENEMY_OUTSIGHT_IN
		MyState = IDLE_ST
		MyEnemy = 0
		MyDestX, MyDestY = 0,0
		TraceAI ("CHASE_ST -> IDLE_ST : ENEMY_OUTSIGHT_IN")
		return
	end

-- HP40%以下だと追跡(攻撃)しない
	local MyHP = GetV (V_HP,MyID)
	local MyMaxHP = GetV (V_MAXHP,MyID)
	local HPper = (MyHP / MyMaxHP) * 100
	if (40 < HPper) then 

		if (true == IsInAttackSight(MyID,MyEnemy)) then  -- ENEMY_INATTACKSIGHT_IN
			MyState = ATTACK_ST
			TraceAI ("CHASE_ST -> ATTACK_ST : ENEMY_INATTACKSIGHT_IN")
			return
		end

		local x, y = GetV (V_POSITION,MyEnemy)
		if (MyDestX ~= x or MyDestY ~= y) then			-- DESTCHANGED_IN
			MyDestX, MyDestY = GetV (V_POSITION,MyEnemy);
			Move (MyID,MyDestX,MyDestY)
			TraceAI ("CHASE_ST -> CHASE_ST : DESTCHANGED_IN")
			return
		end

		if (MyDestX ~= x or MyDestY ~= y) then			-- DESTCHANGED_IN
			MyDestX, MyDestY = GetV (V_POSITION,MyEnemy);
			Move (MyID,MyDestX,MyDestY)
			TraceAI ("CHASE_ST -> CHASE_ST : DESTCHANGED_IN")
			return
		end
	else

-- ホム逃亡処理		
		local distance = GetDistanceFromOwner(MyID)
		if ( distance > 5 or distance == -1) then		-- MYOWNER_OUTSIGNT_IN
			MyState = FOLLOW_ST
			TraceAI ("IDLE_ST -> FOLLOW_ST")
			return;
		end

		local AwayX,AwayY,OwnerX, OwnerY
		AwayX,AwayY = GetV (V_POSITION,MyEnemy);
		OwnerX, OwnerY = GetV (V_POSITION,GetV(V_OWNER,MyID))
		MyX,MyY = GetV(V_POSITION,MyID)
		local distance = GetDistanceFromOwner(MyID)
	
		if (OwnerX > AwayX and OwnerY > AwayY) then	-- 北東へ
			MyDestX = AwayX + 4
			MyDestY = AwayY + 4
			if (MyX > OwnerX + 4) then
				MyDestX = MyDestX - 2
				if (MyY > OwnerY + 4) then
					MyDestY = MyDestY - 2
				end
			end

		elseif (OwnerX > AwayX and OwnerY <= AwayY) then	--南東へ
			MyDestX = AwayX + 4
			MyDestY = AwayY - 4
			if (MyX > OwnerX + 4) then
				MyDestX = MyDestX - 2
				if (MyY < OwnerY - 4) then
					MyDestY = MyDestY + 2
				end
			end

		elseif (OwnerX <= AwayX and OwnerY <= AwayY) then	--南西へ
			MyDestX = AwayX - 4
			MyDestY = AwayY - 4
			if (MyX < OwnerX - 2) then
				MyDestX = MyDestX + 2
				if (MyY < OwnerY - 4) then
					MyDestY = MyDestY + 2
				end
			end

		elseif (OwnerX <= AwayX and OwnerY > AwayY) then	--北西へ
			MyDestX = AwayX - 4
			MyDestY = AwayY + 4
			if (MyX < OwnerX - 4) then
				MyDestX = MyDestX + 2
				if (MyY > OwnerY + 4) then
					MyDestY = MyDestY - 2
				end
			end
		else
			MoveToOwner (MyID)
			return
		end

		Move (MyID,MyDestX,MyDestY)

		TraceAI ("CHASE_ST -> CHASE_ST : DESTCHANGED_IN")
		return
	end

end


-- 攻撃処理
function	OnATTACK_ST ()

	TraceAI ("OnATTACK_ST")
	
	if (true == IsOutOfSight(MyID,MyEnemy)) then	-- ENEMY_OUTSIGHT_IN
		MyState = IDLE_ST
		TraceAI ("ATTACK_ST -> IDLE_ST")
		return 
	end

	if (MOTION_DEAD == GetV(V_MOTION,MyEnemy)) then   -- ENEMY_DEAD_IN
		MyState = IDLE_ST
		TraceAI ("ATTACK_ST -> IDLE_ST")
		return
	end
		
	if (false == IsInAttackSight(MyID,MyEnemy)) then  -- ENEMY_OUTATTACKSIGHT_IN
		MyState = CHASE_ST
		MyDestX, MyDestY = GetV (V_POSITION,MyEnemy);
		Move (MyID,MyDestX,MyDestY)
		TraceAI ("ATTACK_ST -> CHASE_ST  : ENEMY_OUTATTACKSIGHT_IN")
		return
	end
	
	if (MySkill == 0) then
		Attack (MyID,MyEnemy)
	else
		SkillObject (MyID,MySkillLevel,MySkill,MyEnemy)
		MySkill = 0
	end
	TraceAI ("ATTACK_ST -> ATTACK_ST  : ENERGY_RECHARGED_IN")
	return


end



-- 移動処理
function	OnMOVE_CMD_ST ()

	TraceAI ("OnMOVE_CMD_ST")

	local x, y = GetV (V_POSITION,MyID)
	if (x == MyDestX and y == MyDestY) then				-- DESTINATION_ARRIVED_IN
		MyState = IDLE_ST
	end
end




function OnSTOP_CMD_ST ()


end




function OnATTACK_OBJECT_CMD_ST ()

	

end




function OnATTACK_AREA_CMD_ST ()

	TraceAI ("OnATTACK_AREA_CMD_ST")

	local	object = GetOwnerEnemy (MyID)
	if (object == 0) then							
		object = GetMyEnemy (MyID) 
	end

	if (object ~= 0) then							-- MYOWNER_ATTACKED_IN or ATTACKED_IN
		MyState = CHASE_ST
		MyEnemy = object
		return
	end

	local x , y = GetV (V_POSITION,MyID)
	if (x == MyDestX and y == MyDestY) then			-- DESTARRIVED_IN
			MyState = IDLE_ST
	end

end




function OnPATROL_CMD_ST ()

	TraceAI ("OnPATROL_CMD_ST")

	local	object = GetOwnerEnemy (MyID)
	if (object == 0) then							
		object = GetMyEnemy (MyID) 
	end

	if (object ~= 0) then							-- MYOWNER_ATTACKED_IN or ATTACKED_IN
		MyState = CHASE_ST
		MyEnemy = object
		TraceAI ("PATROL_CMD_ST -> CHASE_ST : ATTACKED_IN")
		return
	end

	local x , y = GetV (V_POSITION,MyID)
	if (x == MyDestX and y == MyDestY) then			-- DESTARRIVED_IN
		MyDestX = MyPatrolX
		MyDestY = MyPatrolY
		MyPatrolX = x
		MyPatrolY = y
		Move (MyID,MyDestX,MyDestY)
	end

end



-- ターゲット処理
function OnHOLD_CMD_ST ()

	TraceAI ("OnHOLD_CMD_ST")
	
	if (MyEnemy ~= 0) then
		local d = GetDistance(MyEnemy,MyID)	-- ホムのターゲットの距離
		if (d ~= -1 and d <= GetV(V_ATTACKRANGE,MyID)) then
				Attack (MyID,MyEnemy)
		else
			MyEnemy = 0;
		end
		return
	end


	local	object = GetOwnerEnemy (MyID)
	if (object == 0) then							
		object = GetMyEnemy (MyID)
		if (object == 0) then						
			return
		end
	end

	MyEnemy = object

end




function OnSKILL_OBJECT_CMD_ST ()
	
end




function OnSKILL_AREA_CMD_ST ()

	TraceAI ("OnSKILL_AREA_CMD_ST")

	local x , y = GetV (V_POSITION,MyID)
	if (GetDistance(x,y,MyDestX,MyDestY) <= GetV(V_SKILLATTACKRANGE,MyID,MySkill)) then	-- DESTARRIVED_IN
		SkillGround (MyID,MySkillLevel,MySkill,MyDestX,MyDestY)
		MyState = IDLE_ST
		MySkill = 0
	end

end






-- 主人追尾ALT+T
function OnFOLLOW_CMD_ST ()

	TraceAI ("OnFOLLOW_CMD_ST")

	local ownerX, ownerY, myX, myY
	ownerX, ownerY = GetV (V_POSITION,GetV(V_OWNER,MyID)) -- 持ち主
	myX, myY = GetV (V_POSITION,MyID)					  -- 自分 
	
	local d = GetDistance (ownerX,ownerY,myX,myY)

	if ( d <= 3) then									-- 3セル以下の距離なら
		return 
	end

	local motion = GetV (V_MOTION,MyID)
	if (motion == MOTION_MOVE) then					-- 移動中
		d = GetDistance (ownerX, ownerY, MyDestX, MyDestY)
		if ( d > 3) then									-- 4セル以上の距離なら
			MoveToOwner (MyID)
			MyDestX = ownerX
			MyDestY = ownerY
			return
		end
	else									-- 他の動作 
		MoveToOwner (MyID)
		MyDestX = ownerX
		MyDestY = ownerY
		return
	end
	
end








function	GetOwnerEnemy (myid)
	local result = 0
	local owner  = GetV (V_OWNER,myid)
	local actors = GetActors ()
	local enemys = {}
	local index = 1
	local target
	for i,v in ipairs(actors) do
		if (v == owner) then
			target = GetV (V_TARGET,v)
				if (IsMonster(target) == 1) then
				enemys[index] = target
				index = index+1
				end
		end
		if (v ~= owner and v ~= myid) then
			target = GetV (V_TARGET,v)
			if (target == owner) then
				if (IsMonster(v) == 1) then
					enemys[index] = v
					index = index+1
				else
					local motion = GetV(V_MOTION,v) -- vの行動を取得
					if (motion == MOTION_ATTACK or motion == MOTION_ATTACK2) then -- 攻撃行動を取っているなら
						enemys[index] = v -- 敵とみなす
						index = index+1
					end
				end
			end
		end
	end

	local min_dis = 100
	local dis
	for i,v in ipairs(enemys) do
		dis = GetDistance2 (myid,v)
		if (dis < min_dis) then
			result = v
			min_dis = dis
		end
	end
	
	return result
end



function	GetMyEnemy (myid)
local result = 0

local MyHP = GetV (V_HP,MyID)
local MyMaxHP = GetV (V_MAXHP,MyID)
local HPper = (MyHP / MyMaxHP) * 100
	if (50 <= HPper) then		-- HPが50%以下になったら非アクティブ化
		result = GetMyEnemyB (myid)
	else
		result = GetMyEnemyA (myid)
	end
return result
end

-------------------------------------------
--  非先攻型 GetMyEnemy
-------------------------------------------
function	GetMyEnemyA (myid)
	local result = 0
	local owner  = GetV (V_OWNER,myid)
	local actors = GetActors ()
	local enemys = {}
	local index = 1
	local target

	local type

	local MyHP = GetV (V_HP,MyID)
	local MyMaxHP = GetV (V_MAXHP,MyID)
	local HPper = (MyHP / MyMaxHP) * 100
-- HPが40%以下だとアクティブに敵認識
	if (40 >= HPper) then

	for i,v in ipairs(actors) do		-- iとvへ、テーブルactorsの値が順番に初期値として入り、iがnilになったらループ脱出
		if (v ~= owner and v ~= myid ) then	-- vの値が主人のidでもホム自身のidでもない場合
			if (1 == IsMonster(v) and IsNotNoManner( myid, v )) then	-- vがモンスターでIsNotNoManner( myid, v )に値が入っている場合
				if (3 >= GetDistance2 (myid, v)) then	-- 自分との距離が3の敵
				
				enemys[index] = v				-- 敵リストにvのidを追加
				index = index+1

				end
			end
		end
	end

	else

	for i,v in ipairs(actors) do		-- iとvへ、テーブルactorsの値が順番に初期値として入り、iがnilになったらループ脱出
		if (v ~= owner and v ~= myid) then	-- vの値が主人のidでもホム自身のidでもない場合
			target = GetV (V_TARGET,v)	-- vのターゲット対象を抽出
			if (target == myid) then	-- ターゲットが自分だったら
				enemys[index] = v	-- 敵リストにvのidを追加
				index = index+1
			end
		end
	end

	end


	local min_dis = 100
	local dis
	for i,v in ipairs(enemys) do	-- iとvへ、テーブルenemysの値が順番に初期値として入り、iがnilになったらループ脱出
		dis = GetDistance2 (myid,v)	-- 自分をタゲってる敵(v)との距離をdisに代入
		if (dis < min_dis) then		-- 距離が100未満だったら
			result = v		-- resultへvを代入
			min_dis = dis
		end
	end

	return result				-- resultを返す
end

-------------------------------------------
--  先攻型 GetMyEnemy
-------------------------------------------

--
-- 横殴り防止
function IsNotNoManner( myid, id )	-- 攻撃行動中止
	local target = GetV( V_TARGET, id )	-- idのターゲット相手を抽出
	return target == 0 or ( target == myid or target == GetV( V_OWNER, myid ) )	-- タゲが居ない、またはタゲの対象がホム、持ち主(ケミだった場合)、値を返す
end
--
--
function	GetMyEnemyB (myid)
	local result = 0
	local owner  = GetV (V_OWNER,myid)
	local actors = GetActors ()
	local enemys = {}
	local index = 1
	local type
	for i,v in ipairs(actors) do		-- iとvへ、テーブルactorsの値が順番に初期値として入り、iがnilになったらループ脱出
		if (v ~= owner and v ~= myid) then	-- vの値が主人のidでもホム自身のidでもない場合
			if (1 == IsMonster(v) and IsNotNoManner( myid, v ) ) then	-- vがモンスターでIsNotNoManner( myid, v )に値が入っている場合
				if (6 >= GetDistance2 (GetV(V_OWNER,MyID), v)) then	-- 主人との距離が6以下の場合
				enemys[index] = v				-- 敵リストにvのidを追加
				index = index+1
				end
			end
		end
	end

	local min_dis = 100
	local dis
	for i,v in ipairs(enemys) do	-- iとvへ、テーブルenemysの値が順番に初期値として入り、iがnilになったらループ脱出
		dis = GetDistance2 (myid,v)	-- 自分をタゲってる敵(v)との距離をdisに代入
		if (dis < min_dis) then		-- 距離が100未満だったら
			result = v		-- resultへvを代入
			min_dis = dis
		end
	end

	return result
end





function AI(myid)

	MyID = myid
	local msg	= GetMsg (myid)			-- command
	local rmsg	= GetResMsg (myid)		-- reserved command

	
	if msg[1] == NONE_CMD then
		if rmsg[1] ~= NONE_CMD then
			if List.size(ResCmdList) < 10 then
				List.pushright (ResCmdList,rmsg) -- 予約コマンド保存
			end
		end
	else
		List.clear (ResCmdList)	-- 新しいコマンドが入力されたら、予約コマンドは削除する
		ProcessCommand (msg)	-- コマンド処理 
	end

		
	-- 状態処理 
 	if (MyState == IDLE_ST) then
		OnIDLE_ST ()
	elseif (MyState == CHASE_ST) then					
		OnCHASE_ST ()
	elseif (MyState == ATTACK_ST) then
		OnATTACK_ST ()
	elseif (MyState == FOLLOW_ST) then
		OnFOLLOW_ST ()
	elseif (MyState == MOVE_CMD_ST) then
		OnMOVE_CMD_ST ()
	elseif (MyState == STOP_CMD_ST) then
		OnSTOP_CMD_ST ()
	elseif (MyState == ATTACK_OBJECT_CMD_ST) then
		OnATTACK_OBJECT_CMD_ST ()
	elseif (MyState == ATTACK_AREA_CMD_ST) then
		OnATTACK_AREA_CMD_ST ()
	elseif (MyState == PATROL_CMD_ST) then
		OnPATROL_CMD_ST ()
	elseif (MyState == HOLD_CMD_ST) then
		OnHOLD_CMD_ST ()
	elseif (MyState == SKILL_OBJECT_CMD_ST) then
		OnSKILL_OBJECT_CMD_ST ()
	elseif (MyState == SKILL_AREA_CMD_ST) then
		OnSKILL_AREA_CMD_ST ()
	elseif (MyState == FOLLOW_CMD_ST) then
		OnFOLLOW_CMD_ST ()
	end

end







戻る

戻る