-- 与Action3DEffect.ini、WeaponMotion.ini有关
local WeaponMotionData = 
{
	-- ["右手武器.左手武器"] = { R=, L=, },
	-- 未配置时，直接取武器本身的id
	-- 左爪右链刃时，左爪的武器动作要区别于右手单持爪的动作，占用679;
	-- 左爪右链刃时，左爪的极品光效要绑在左手武器的虚似体中，占用679;
	["616.615"] = { R=nil, L=679, },

	-- 双持双截棍
	["617.617"] = { R=680, L=618, },

	-- 远战铁扇，右手大扇，左手小扇
	["626.627"] = { R=650, L=660, },
	-- 左手空
	["626.0"] = { R=650, L=nil, },

	-- 近战铁扇，右手小扇，左手大扇
	["627.626"] = { R=630, L=640, },
	-- 左手空
	["627.0"] = { R=630, L=nil, },

	-- 雷神双持专属武器
	["681.680"] = { R=681, L=682, },
}

-- 忍者用
local WeaponActDataRenZhe = {
	[500] = {
		[999] = 500,
	},
	-- 忍刀
	[601] = {
		[0] = 672,
	},
}

-- 与3dmotion.ini有关
local WeaponActData =
{
	-- 右手武器->对应weapon.ini里的title
	-- Sort = title / 1000
	-- [右手武器Sort] = {
		-- 左手武器->对应weapon.ini里的title
		-- [左手武器Sort] = 武器动作编号,
	-- }
	-- 右手空
	[0] = {
		-- 双手都没拿武器
		[0] = 0,
		-- 右空，左手盾
		[900] = 700,
	},
	-- 长武外套
	[350] = {
		[900] = 735,
		[560] = 602,
		[350] = 602,
		[999] = 560,
	},
	-- 短武外套
	[360] = {
		[601] = 672,
		[610] = 673,
		[612] = 674,
		[613] = 676,
		-- 右手短武器外套，左手拂尘，武器编号用423
		[619] = 423,
		[625] = 620,
		[900] = 736,
		[560] = 602,
		[350] = 602,
		[999] = 611,
	},
	-- 弓外套
	[370] = {
		[999] = 370,
	},
	-- 刀
	[410] = {
		[601] = 672,
		[610] = 673,
		[900] = 741,
		[999] = 611,
	},
	-- 剑
	[420] = {
		[601] = 672,
		[610] = 673,
		[900] = 741,
		[999] = 611,
	},
	-- 法剑
	[421] = {
		[0] = 611,
		[601] = 672,
		[610] = 673,
		-- 右手法剑，左手拂尘，武器编号用423
		[619] = 423,
		[900] = 742,
		[999] = 423,
	},
	-- 钩
	[430] = {
		[601] = 672,
		[610] = 673,
		[900] = 743,
		[999] = 611,
	},
	-- 节鞭
	[440] = {
		[601] = 672,
		[610] = 673,
		[900] = 744,
		[999] = 611,
	},
	-- 斧
	[450] = {
		[601] = 672,
		[610] = 673,
		[900] = 745,
		[999] = 611,
	},
	-- 锤
	[460] = {
		[0] = 611,
		[601] = 672,
		[610] = 673,
		[900] = 746,
		[999] = 611,
	},
	-- 棒
	[480] = {
		[601] = 672,
		[610] = 673,
		[900] = 748,
		[999] = 611,
	},
	-- 杵
	[481] = {
		[601] = 672,
		[610] = 673,
		[900] = 748,
		[999] = 611,
	},
	-- 匕首
	[490] = {
		[601] = 672,
		[610] = 673,
		[900] = 749,
		[999] = 611,
	},
	[500] = {
		-- 弓
		[999] = 500,
	},
	--长刀
	[510] = {
		[0] = 560,
		[900] = 756,
	},
	-- 钩镰
	[511] = {
		[900] = 756,
	},
	-- 长斧
	[530] = {
		[0] = 560,
		[900] = 753,
	},
	-- 长锤
	[540] = {
		[0] = 560,
		[900] = 754,
	},
	-- 长枪
	[560] = {
		[0] = 560,
		[900] = 756,
		[560] = 602,
		[350] = 602,
		[360] = 602,
	},
	--棍
	[561] = {
		[0] = 560,
		[900] = 756,
	},
	-- 锄
	[562] = {
		[900] = 756,
	},
	-- 长戟
	[580] = {
		[0] = 560,
		[900] = 758,
	},
	-- 忍刀
	[601] = {
		[0] = 672,
		-- 拿有忍刀，武器编号固定用672
		[601] = 672,
		[999] = 672,
	},
	
	-- 弓手翻身巨弩
	[605] = {
		[603] = 605,
	},	
	-- 弓手史诗武器
	[606] = {
		[0] = 606,
		[999] = 606,
	},
	
	-- 念珠
	[610] = {
		[0] = 610,
		[601] = 672,
		-- 双手之一拿着念珠，例如主手短剑右手念珠，或双持念珠，武器编号用673
		[610] = 611,
		[999] = 611,
	},
	-- 刺剑
	[611] = {
		-- 单手拿刺剑,武器编号固定用675
		[0] = 674,
		[601] = 672,
		[610] = 674,
		-- 左火枪或右刺剑，或右海盗史诗枪刃左其他短武，武器编号用674
		[612] = 674,
		[999] = 674,
	},
	-- 飞刀
	[613] = {
		-- 单手拿飞刀，武器编号固定用677
		[0] = 676,
		-- 双手飞刀，武器编号用676
		[603] = 603,
		[604] = 604,
		[500] = 603,
		[999] = 676,
	},
	-- 十字剑
	[614] = {
		[601] = 672,
		[610] = 673,

		-- 双手勇士史诗，武器编号用678
		[614] = 678,
		[900] = 741,
		[999] = 611,
	},
	[615] = {
		-- 复用忍者双刀
		[999] = 672,
	},
	-- 忍者史诗
	[616] = {
		-- 单手拿忍者史诗，复用忍者单刀动作
		-- 单手拿史诗显示爪外型
		[0] = 616,
		[601] = 672,
		[610] = 673,
		-- 拿两把忍者史诗武器显示成左爪右链刃外型，武器编号用679
		[615] = 679,
		[900] = 741,
		[999] = 672,
	},
	-- 双截棍
	[617] = {
		-- 双手双截棍，武器编号680
		[617] = 680,
		[999] = 611,
	},
	-- 史诗法剑
	[620] = {
		-- 单手拿道士史诗，复用法剑动作
		[0] = 421,
		-- 右手史诗法剑，左手拂尘，武器编号用423
		[619] = 423,
		[999] = 423,
	},
	-- 史诗法剑(变)
	[621] = {
		-- 单手拿道士史诗，复用法剑动作
		[0] = 421,
		-- 右手史诗法剑(变)，左手拂尘，武器编号用423
		[619] = 423,
		[999] = 423,
	},
	-- 天王柱,武僧史诗
	[622] = {
		-- 单手拿武僧史诗，复用念珠动作
		[0] = 610,
		-- 拿两把武僧史诗武器显示成左天王柱(623)右锁链(622)外型，武器编号用627
		[623] = 627,
		-- 右手武僧史诗，左手其他武器，武器编号用673
		[999] = 611,
	},
	-- 拳套,战士史诗
	[624] = {
		-- 单手拿战士史诗，武器编号固定用620
		[0] = 620,
		-- 拿有战士史诗(左手625，右手624)，武器编号固定用620
		[625] = 620,	
		[999] = 620,
	},
	[626] = {
		-- 远战铁扇右手拿大扇，武器编号固定用650
		[999] = 650,
	},
	[627] = {
		-- 近战铁扇右手拿小扇，武器编号固定用630
		[999] = 630,
	},
	[670] = {
		-- 左手海盗史诗手炮，右手必然是海盗史诗枪刃，武器编号用670
		[999] = 670,
	},
	[671] = {
		-- 单手拿海盗史诗枪刃，武器编号固定用675
		[0] = 675,
		-- 左火枪或右刺剑，或右海盗史诗枪刃左其他短武，武器编号用674
                [670] = 670,
		[999] = 674,
	},
	[681] = {
		[0] = 690,
		[680] = 690,
		[999] = 690,
	},
}

local function GetWMotData(idRWeapon, idLWeapon, key)
	local idRSort = math.floor(idRWeapon / 1000)
	local idLSort = math.floor(idLWeapon / 1000)
	local tData = WeaponMotionData[string.format("%d.%d", idRSort, idLSort)]

	if (tData == nil) then
		tData = WeaponMotionData[string.format("%d.999", idRSort)]
	end

	if (tData ~= nil) then
		local ret = tData[key]

		if (ret ~= nil) then
			return ret
		end
	end

	return nil
end

local function GetRWeaponMotionData(idRWeapon, idLWeapon)
	if (idRWeapon==360393) then 
	return 360394
	elseif (idRWeapon==360428) then 
	return 360429
	elseif (idRWeapon==360430) then 
	return 360431
end
	local ret = GetWMotData(idRWeapon, idLWeapon, "R")

	if (ret ~= nil) then 
		idRWeapon = idRWeapon % 1000 + ret * 1000
	end
	
	return idRWeapon
end
		
local function GetLWeaponMotionData(idRWeapon, idLWeapon)
	if (idLWeapon==360393) then 
	return 360393
	elseif (idLWeapon==360428) then 
	return 360428
	elseif (idLWeapon==360430) then 
	return 360430
end

	local ret = GetWMotData(idRWeapon, idLWeapon, "L")

	if (ret ~= nil) then 
		idLWeapon = idLWeapon % 1000 + ret * 1000
	end
	
	return idLWeapon
end

local function GetWActData(idRSort, idLSort)
	local tR = WeaponActData[idRSort]

	if (tR == nil) then
		return nil
	end

	local ret = tR[idLSort]

	if (ret ~= nil) then
		return ret
	end

	ret = tR[999]
	if (ret ~= nil) then
		return ret
	end

	return nil
end

-- 获取忍者的动作id
local function GetWActDataRenZhe(idRSort, idLSort)
	local tR = WeaponActDataRenZhe[idRSort]

	if (tR == nil) then
		return 672
	end

	local ret = tR[idLSort]

	if (ret ~= nil) then
		return ret
	end

	ret = tR[999]
	if (ret ~= nil) then
		return ret
	end

	return 672
end

-- ****************************************************
-- * 3dmotion.ini 编号规则是(tt)aaabbbcccdddd         *
-- * aaa为坐骑类型, tt为马匹等级(冲锋时使用)          *
-- * bbb为角色外形，ccc为武器动作数据，dddd为动作类型 *
-- ****************************************************
-- 返回值：用于3dmotion.ini，编号规则中的ccc
-- idRWeapon，右手武器显示出来的id(即玩家实际看到的武器id，非itemtype)
-- idLWeapon，右手武器显示出来的id(即玩家实际看到的武器id，非itemtype)
local function GetWeaponActData(idRWeapon, idLWeapon, profession, armor)
	if (idRWeapon == 360364 or idRWeapon == 360365 or idRWeapon == 360366 or idRWeapon == 360367 or idRWeapon == 360375 or idRWeapon == 360415 or idRWeapon == 360409) then
		if math.floor(idLWeapon / 1000) == 619 then
			return 423
		end
		return 611
	end
	local proSort = math.floor(profession / 1000)
	local idRSort = math.floor(idRWeapon / 1000)
	local idLSort = math.floor(idLWeapon / 1000)
	local idTemp = idRSort

	if (idLSort == 500 or idLSort == 370) then
		idRSort = idLSort
	elseif (idLSort == 380) then
		-- 盾外套按盾来处理
		idLSort = 900
	end

	if (idTemp == 1050) then
		idLSort = idTemp
	end

-- if (proSort == 5) then
-- 	return GetWActDataRenZhe(idRSort, idLSort)
-- end

	if (idRSort == 0 or idLSort == 0) then
		local ret = GetWActData(idRSort, idLSort)

		if (ret ~= nil) then
			return ret
		end

		return idRSort
	end

	local ret = GetWActData(idRSort, idLSort)

	if (ret ~= nil) then
		return ret
	end

	-- single hand weapon
	if (math.floor(idLSort / 100) == 4 or idLSort == 360) then
		return 600 + math.floor(idRSort % 100 / 10) * 10 + math.floor(idLSort % 100 / 10)
	end

	return 0
end

-- 获取armor对应的披风id
local function GetShowCloakID(profession, armor, rweapon, lweapon, mount, act)
	local act = GetWeaponActData(rweapon, lweapon, profession, armor)
	if (act ~= 672 and act ~= 601 and act ~= 679) then
		return 0
	end

	if (math.floor(armor / 1000) % 1000 == 135) then
		return 135000
	end

	local rArmor = math.floor(armor % 1000000)
	if (rArmor >= 189910 and rArmor <= 189939) then
		return armor
	end

	return 0
end

-- 获取对应披风光效的人物外观id(用于Action3DEffect.ini)
local function GetCloakLook(armor, look, mount, act)
	if (act ~= 130 or mount < 1) then
		return look
	end

	return look + 1000
end

-- 发型信息
local T_Hair = {
-- [发型id] = {},
[82] = { Scale=150, TopShow = true, Z=-6, Z1=-6, },
[83] = { Scale=150, TopShow = true, Z=-6, Z1=-6, },
[85] = { Scale=150, TopShow = true, Z=-6, Z1=-6, },
[86] = { Scale=150, TopShow = true, Z=-6, Z1=-6, },
[87] = { Scale=150, TopShow = true, Z=-6, Z1=-6, },
[196] = { Scale=100, TopShow = true, Z=-6, Z1=-6, },
[197] = { Scale=120, TopShow = true, Z=-6, Z1=-6, },
[198] = { Scale=140, TopShow = true, Z=-6, Z1=-6, },
[199] = { Scale=160, TopShow = true, Z=-6, Z1=-6, },
[200] = { Scale=180, TopShow = true, Z=-6, Z1=-6, },
[126] = { Scale=200, TopShow = true, Z=-6, Z1=-6, },
[131] = { Scale=100, TopShow = true, Z=-6, Z1=-6, },
[132] = { Scale=120, TopShow = true, Z=-6, Z1=-6, },
[133] = { Scale=140, TopShow = true, Z=-6, Z1=-6, },
[134] = { Scale=160, TopShow = true, Z=-6, Z1=-6, },
[135] = { Scale=180, TopShow = true, Z=-6, Z1=-6, },
[136] = { Scale=200, TopShow = true, Z=-6, Z1=-6, },
[126] = { Scale=200, TopShow = true, Z=-6, Z1=-6, },
[141] = { Scale=100, TopShow = true, Z=-6, Z1=-6, },
[142] = { Scale=120, TopShow = true, Z=-6, Z1=-6, },
[143] = { Scale=140, TopShow = true, Z=-6, Z1=-6, },
[144] = { Scale=160, TopShow = true, Z=-6, Z1=-6, },
[145] = { Scale=180, TopShow = true, Z=-6, Z1=-6, },
[146] = { Scale=200, TopShow = true, Z=-6, Z1=-6, },
}

-- 获取发型信息
local function GetHairInfo(hair, key, default)
	local tHair = T_Hair[hair]
	if (tHair == nil) then
		return default
	end

	local ret = tHair[key]
	if (ret == nil) then
		return default
	end

	return ret
end

-- 根据人物体型获取发型信息
local function GetHairInfoByLook(look, hair, key, default)
	local tHair = T_Hair[hair]
	if (tHair == nil) then
		return default
	end

	if (look == 5 or look == 7) then
		key = key .. "1"
	end
	
	local ret = tHair[key]
	if (ret == nil) then
		return default
	end

	return ret
end

-- 判断是否需要由程序控制披风光效的帧间隔
local function IsCalcCloakFrame(act, mount)
	if (act ~= 130 or mount > 0) then
		return false
	end

	return true
end

local function IsHideHeadArmor(armor)
	if (armor >= 189910 and armor <= 189939) then
		return true
	end

	return false
end

-- 是否开启坐骑动作帧率限制
local function GetMountActionLimit()
	return true
end

-- 清理变量释放内存
local function Unload()
	T_Hair = nil
	WeaponActData = nil
	WeaponMotionData = nil
	WeaponEffData = nil
	WeaponActDataRenZhe = nil
end

-- 通过require传给其他Lua脚本使用
local I3DRole = {
	GetHairInfo = GetHairInfo,
	GetHairInfoByLook = GetHairInfoByLook,
	GetWeaponActData = GetWeaponActData,
	GetRWeaponMotionData = GetRWeaponMotionData,
	GetLWeaponMotionData = GetLWeaponMotionData,
	GetShowCloakID = GetShowCloakID,
	GetCloakLook = GetCloakLook,
	IsCalcCloakFrame = IsCalcCloakFrame,
	IsHideHeadArmor = IsHideHeadArmor,
	GetMountActionLimit = GetMountActionLimit,
	Unload = Unload,
}

-- C++程序使用的global函数名
I3DRole_GetHairInfo = I3DRole.GetHairInfo
I3DRole_GetHairInfoByLook = I3DRole.GetHairInfoByLook
I3DRole_IsHideHeadArmor = I3DRole.IsHideHeadArmor
I3DRole_GetCloakLook = I3DRole.GetCloakLook
I3DRole_GetShowCloakID = I3DRole.GetShowCloakID
I3DRole_IsCalcCloakFrame = I3DRole.IsCalcCloakFrame
I3DRole_GetWeaponActData = I3DRole.GetWeaponActData
I3DRole_GetRWeaponMotionData = I3DRole.GetRWeaponMotionData
I3DRole_GetLWeaponMotionData = I3DRole.GetLWeaponMotionData
I3DRole_GetMountActionLimit = I3DRole.GetMountActionLimit
I3DRole_Unload = I3DRole.Unload

-- require返回值
return I3DRole
