--                 űϵͳáרҵ޸ģ
------------------------------------------------------------------------------
 = {}
. = {}
..=0 -- 1 2 NPC 3 
..=1
..ԭ=2
..ԭʼͼ=%_ԭʼͼ%
..ͼ=3
..ͼ=4
..X=5
..Y=6
..=7
..ȼ=8
..HP=9
..Ѫ=287
..MP=10
..ħ=288
..BP=11
..BP=12
..BP=13
..ٶBP=14
..ħBP=15
..=16
..=17
..=18
..ˮ=19
..=20
..=21
..=22
..˯=23
..ʯ=24
..=25
..=26
..=27
..ɱ=28
..=29
..=30
..=31
..=32
..=33
..=41
..=42
..ͼ=43--δ֪
..ɫ=44
..RegistNumber=48
..ְҵ=49
..ְ=50
..ְҵ=51
..=52
..=53
..н=54
..=55
..=56
..=57
..=58
..=59
..װƺ=60 --δ -1 ޳ƺ  0 1ְҵƺ
..ս=62
..½=65
..ȡ=67
..ʼ=68
..ϳƷ=69
..м=70
..=71
..=72
..Ƴ=73
..׽=74
..=76
..˵ȼ=38
..ͷ汾=80
..ս=139
..DP=139
..GM=144
..½=149
..=150
..ʱ=153
..ԭ=%_%
..=%_ҳƺ%
..˺=%_˺%
..ϴ=148
..ε¼=600
..սIndex=273 --սIndex -1 ս״̬
..ս״̬=272 --սĽ׶Σ0ս2׶Ρ3ѡָ׶Σ6ս
. = {}
..ID=68
..ɳ=0
..ɳ=1
..ǿȳɳ=2
..ٶȳɳ=3
..ħɳ=4
..=51
..ֵȼ=52
..ҳ=385
..״̬=69 --״̬0  1 ս2 Ϣ3 ɢ16
..CDK=2002--Զ
..=2004
..=2001


.={}
..Ƭ=53
..Ƭȼ=49
..Ƭ=53
..Ƭȼ=49

. = {}
..ID = 0
..ͼ = %_ͼ%
..=3
..װ=4 --ֵ10ǿװ
..˫=5
..ѵ=9
..ѵ=10
..ȼ=11
..;=65
..;=13
..С=14
..󹥻=15
..=18
..=19
..=20
..=21
..ظ=22
..ɱ=23
..=24
..=25
..=26
..HP=27
..MP=28
..=29
..=31
..=45
..Ӳһ=46
..Ӳζ=47
..ħ=72
..ħ=55
..һ=32
..Զ=33
..һֵ=34
..Զֵ=35
..=39
..˯=40
..ʯ=41
..=42
..ҿ=43
..=44
..ǳʧ = 53
..ʧ = 53
..ܷ = 54
..Ʒ˵=57
..Ҽ˵=58
.. = 63
..ʱ = 67
..δ = 2000
..Ѽ = 2001
..ӡ=2002
..ò=2003
..˫Ч=2003
..ӡ=2004
..߲=2007

 = {}
.Ϣ=0			--ֱ@ʾDataַ
.봰=1			--ֱ@ʾDataַ
.ѡ=2			--|
.˫봰=35		--Data1data2
.ͼ=37		--ͼԻ

ť = {}
ť.=0
ť.ȷ=1
ť.ر=2
ť.ȷر=3
ť.=4
ť.=8
ť.Ƿ=12
ť.ȷر=7
ť.ȡ=34
ť.ȡ=18
ť.ȡ=50
ť.һҳ=16
ť.һҳ=32
.={}
..=1
..=2
..NPC=2
..=3

={}
.վ=1
.ֹ=2
.=3
.ͣ=4
.=5
.ħ=6
.Ͷ=7
.=8
.=9
.=10
.=11
.=12
.=13
.ŭ=14
.=15
.ͷ=16
.ʯͷ=17
.=18
.=19
.·=30
.վ=31
ɫ={}
ɫ.ɫ =0 
ɫ.ɫ =1
ɫ.ɫ =2 
ɫ.ɫ =3 
ɫ.ɫ =4 
ɫ.ɫ =5 
ɫ.ɫ =6 
ɫ.ɫ =7 
ɫ.ɫ=8 
ɫ.ɫ =9
={}
.=3
.=2
.С=1
tab_laba = tab_laba or {}
function MSG(Msg)--ϵͳʾȿ
	for k,v in pairs(Players) do
		if tab_laba[k]==nil then
			NLG.SystemMessage(v.Index,Msg)
		end
	end
end
--תΪʱ
function ShowGAWindowTalk(_PlayerIndex,_Window, _Select,_Seqno,_Data,_MePtr)
	return NLG.ShowWindowTalked(_PlayerIndex,_MePtr,_Window,_Select,_Seqno,_Data);
end
function NumtoTime(time)
	local Сʱ =math.floor(time/3600);
	time=time-Сʱ*3600
	local  = math.floor(time/60);
	time=time-*60
	local  = time
	local MSG=Сʱ.."Сʱ"....""....""
	return MSG
end
function CheckAutoTime(Time)--ʱ{W,h,m}
	_Time = os.time()
	local tWeek = tonumber(os.date("%w"))
	local tHour = tonumber(os.date("%H"))
	local tMin = tonumber(os.date("%M"))
	local rt = false
	for i = 1,#Time[1] do
		if tWeek==Time[1][i] then
			rt = true
		end
	end
	if rt==false then
		return false
	end
	local rt = false
	for i = 1, #Time[2] do
		if tHour== Time[2][i] then
			rt = true
		end
	end
	if rt==false then
		return false
	end
	local rt = false
	for i = 1, #Time[3] do
		if tMin==Time[3][i] then
			rt = true
		end
	end
	if rt==false then
		return false
	end
	return true
end

function getIntPart(x) ---ָȥС
	if x <= 0 then
	   return math.ceil(x);
	end

	if math.ceil(x) == x then
	   x = math.ceil(x);
	else
	   x = math.ceil(x) - 1;
	end
	return x;
end

function Playerkey(player)
	return Char.GetData(player,%_˺%)..Char.GetData(player,48);
end
function Char.PetPos(_PlayerIndex) --ڳλӿڷؿλ
	local PetPos = 5	
	for t = 0,4 do
	local PetIndex =Char.GetPetIndex(_PlayerIndex,t);
		if (PetIndex>0) then 
			PetPos = PetPos - 1			
		end	
	end	
	return PetPos
end	
function Char.ItemSlot(_PlayerIndex)--ʹõ
	local nTotal = 0
	for Pos = 8,27 do
		local tItemIndex = Char.GetItemIndex(_PlayerIndex,Pos)
		if tItemIndex>=0 then
			nTotal = nTotal + 1
		end
	end
	return nTotal
end
function Char.ItemPos(_PlayerIndex) --ڵλӿ
	local ItemPos = 20	
	for t = 8,27 do
	local ItemIndex =Char.GetItemIndex(_PlayerIndex,t);
		if (ItemIndex>0) then 
			ItemPos = ItemPos - 1			
		end	
	end	
	return ItemPos
end
function Char.GetItemPos(_PlayerIndex,_ItemIndex) --õλ
	local ItemPos = -1	
	for Pos =1,27 do
		local tItemIndex =Char.GetItemIndex(_PlayerIndex,Pos);
		if (tItemIndex==_ItemIndex) then 
			ItemPos = Pos	
			break
		end	
	end	
	return ItemPos
end
function Char.HaveItem( _PlayerIndex,_ItemID)--GEжϵӿ
	local tHaveItem = {}
	local nTotal = 0
	for Pos = 0,27 do
		local tItemIndex = Char.GetItemIndex(_PlayerIndex,Pos)
		if tItemIndex>=0 and (Item.GetData(tItemIndex,..ID)==_ItemID) then
			local Num = Item.GetData(Char.GetItemIndex(_PlayerIndex,Pos),..ѵ)
			nTotal = nTotal + Num
		end
	end
	return nTotal
end
function Char.ItemNum( _PlayerIndex, _ItemID)
	local tHaveItem = {}
	local nTotal = 0
	for Pos = 0,27 do
		local tItemIndex = Char.GetItemIndex(_PlayerIndex, Pos)
		if tItemIndex>=0 and (Item.GetData(tItemIndex, ..ID)==_ItemID) then
			local Num = Item.GetData(Char.GetItemIndex(_PlayerIndex, Pos), ..ѵ)
			nTotal = nTotal + Num
		end
	end
	return nTotal
end

function Char.GiveJinKa(_PlayerIndex,Num)	--GEӼGA߽ӿ
	local CdKey=Char.GetData(_PlayerIndex,%_˺%)
	if Char.ItemPos(_PlayerIndex) == 0 then
		NLG.SystemMessage(_PlayerIndex,"[ϵͳ]㣬ŵĽ𿨽תҵл"..Num)
		EasySqlJKBank(CdKey,Num)
	else
		Char.GiveItem(_PlayerIndex,JinKaItem,Num)
	end
	return Gold
end
function Char.SetGold(_PlayerIndex,Gold)	--GEӼGA߽ӿ
	local tMaxGold=10000000 --Я
	local tGold=Char.GetData(_PlayerIndex,%_%)
	local CdKey=Char.GetData(_PlayerIndex,%_˺%)
	if tGold+Gold > tMaxGold and Gold>0 then
		local BankGold=tGold+Gold-tMaxGold
		Gold = tMaxGold - tGold
		EasySqlBank(CdKey,BankGold)
		NLG.SystemMessage(_PlayerIndex,"[ϵͳ]ЯѾֵע⡣ѽħ"..BankGold.."ҵ");
	end

	if Gold<0  and tGold+Gold < 0 then
		Gold = tGold
		NLG.SystemMessage(_PlayerIndex,"[ϵͳ]ЯħҲ㡣");
		EasySqlBank(CdKey,tGold+Gold)
	end
	if Gold==0 then
		return 0
	end
	if Gold >0 then
		NLG.SystemMessage(_PlayerIndex,"[ϵͳ]ħ+"..Gold.."");
	else
		NLG.SystemMessage(_PlayerIndex,"[ϵͳ]ħ"..Gold.."");
	end
	Char.PlusData(_PlayerIndex,%_%,Gold)
	NLG.UpChar(_PlayerIndex);
	return Gold
end
function Char.GetTeamNum(_PlayerIndex)   ---GEӿ--ж϶ж
	local Num= 0
	if Char.GetData(_PlayerIndex,%_ģʽ%) == 0 then
		Num=1
	else
		local TeamDIndex = Char.GetTeamIndex(_PlayerIndex,0);
		for _Pos = 0,4 do
			_PosIndex = Char.GetTeamIndex(TeamDIndex,_Pos);
			if _PosIndex >=0 then
				Num = Num+1
			end
		end
	end
	return Num
end
function Char.InTeam(_PlayerIndex,P2)   --жP2Ƿڶ
	local Num= -1
	if Char.GetData(_PlayerIndex,%_ģʽ%) == 0 then
		Num=-1
	else
		local TeamDIndex = Char.GetTeamIndex(_PlayerIndex,0);
			for _Pos = 0,4 do
				local _PosIndex = Char.GetTeamIndex(TeamDIndex,_Pos);
				if _PosIndex >=0 then
					if _PosIndex==P2 then
						Num = 1
					end
				end
			end
	end
	return Num
end
function Char.GetTeamMaxLevel(_PlayerIndex)   ---GEӿ--ж϶߼
	local Level= 0
	if Char.GetData(_PlayerIndex,%_ģʽ%) == 0 then
		Level=Char.GetData(_PlayerIndex,%_ȼ%)
	else
		local TeamDIndex = Char.GetTeamIndex(_PlayerIndex,0);
		for _Pos = 0,4 do
			_PosIndex = Char.GetTeamIndex(TeamDIndex,_Pos);
			if _PosIndex >=0 then
				local tLevel=Char.GetData(_PosIndex,%_ȼ%)
				if tLevel >Level then
					Level=tLevel
				end
			end
		end
	end
	return Level
end
function GetJinKa(_PlayerIndex)   ---GEӿ--жҽ
	local JinKaItem =JinKaItem or 23159
	local tHaveItem = {}
	local nTotal = 0
	for Pos = 0,27 do
		local tItemIndex = Char.GetItemIndex(_PlayerIndex,Pos)
		if tItemIndex>=0 and (Item.GetData(tItemIndex,..ID)==JinKaItem) then
			local Num = Item.GetData(Char.GetItemIndex(_PlayerIndex,Pos),..ѵ)
			nTotal = nTotal + Num
		end
	end
	return nTotal
end
function GetGold(_PlayerIndex)   ---GEӿ--жҽ
	return Char.GetData(_PlayerIndex,%_%)
end

function Char.GiveItemAdv(_PlayerIndex,_ItemID,_ItemNum)--GE߽ӿ
	local OldItem = {}
	local NewItem = {}
	local NewItemPos = {}
	for i = 8,27 do
		OldItem[i]=Item.GetData(Char.GetItemIndex(_PlayerIndex,i),..ID)
	end
	Char.GiveItem(_PlayerIndex,_ItemID,_ItemNum)
	for i = 8,27 do
		if OldItem[i]~=Item.GetData(Char.GetItemIndex(_PlayerIndex,i),..ID) then
			table.insert(NewItemPos,i)
		end
	end
	if table.getn(NewItemPos)==0 then
		return -1
	else
		return NewItemPos
	end
end

function EasyGive(_PlayerIndex,GiveStr)---- --ʽ||--ͣG-ңI-ߣP-E-飬F-T-ʱA-ԣS-ӵ㣩
	local TM_Give = Split(GiveStr,"|")
	local MaxLevel=MaxLevel or 150
	local tLevel=Char.GetData(_PlayerIndex,%_ȼ%)
	TM_Give[2] = tonumber(TM_Give[2])
	TM_Give[3] = tonumber(TM_Give[3])
	local TM_RT = ""
	if (TM_Give[1]=="G") then
		Char.SetGold(_PlayerIndex,TM_Give[3]); 
		TM_RT = "[]" .. TM_Give[3] .. "G"
	elseif (TM_Give[1]=="I") then
		if Char.ItemPos(_PlayerIndex) == 0 then
		
			TM_RT = "[]ȡʧܣ!"
		else
			Char.GiveItem(_PlayerIndex,TM_Give[2],TM_Give[3])
			TM_RT = "[]*" .. TM_Give[3] .. ""
		end
	elseif (TM_Give[1]=="P") then
		if Char.PetPos(_PlayerIndex)==0 then
			TM_RT = "[]ȡʧܣ!"
		else
			if tonumber(TM_Give[3])>=1 then TM_Give[3]=1 end
			local PetIndex=Char.GivePet(_PlayerIndex,TM_Give[2],TM_Give[3])
			if PetIndex>=0 then
				PetName=Char.GetData(PetIndex,%_ԭ%)
				if TM_Give[3]==0 then
					TM_RT = "[]" .. PetName.. "*1ֻ"
				else
					TM_RT = "[]" .. PetName.. "*1ֻ"
				end
			end
		end
	elseif (TM_Give[1]=="T") then
	if Char.IsFeverTime(_PlayerIndex)==1 then Char.FeverTimeStop(_PlayerIndex) end--GPùرմӿʱ
		Char.SetData(_PlayerIndex,153,Char.GetData(_PlayerIndex,153)+TM_Give[3])
		local TM_FeverTime = tonumber(TM_Give[3])
		TM_RT = "[ʱ]" .. TM_FeverTime .. "룬"
	elseif (TM_Give[1]=="E") then
		if tLevel >= MaxLevel then
			return "[]ȡʧܣȼ!"
		end
		Char.SetData(_PlayerIndex,%_%,Char.GetData(_PlayerIndex,%_%)+TM_Give[3])
		TM_RT = "[]" .. TM_Give[3] .. "㣬"
	elseif (TM_Give[1]=="F") then
		Char.SetData(_PlayerIndex,%_%,Char.GetData(_PlayerIndex,%_%)+TM_Give[3])
		TM_RT = "[]" .. TM_Give[3] .. "㣬"
	elseif (TM_Give[1]=="A") then
		Char.SetData(_PlayerIndex,TM_Give[2],Char.GetData(_PlayerIndex,Give[2])+TM_Give[3])
		TM_RT = "[" .. TM_Give[2] .. "]" .. TM_Give[3] .. "㣬"
	elseif (TM_Give[1]=="S") then
		Char.SetData(_PlayerIndex,%_%,Char.GetData(_PlayerIndex,%_%+TM_Give[3]))
		TM_RT = "[]"..TM_Give[3]..""
	end
	NLG.UpChar(_PlayerIndex);
	return TM_RT
end

function getTabKey(_PlayerIndex)
	local tCdKey = Char.GetData(_PlayerIndex, %_˺%)
	local tPlayerLR = Char.GetData(_PlayerIndex, 47)
	local tTabKey = "T_" .. tCdKey .. "_" .. tPlayerLR
	return tTabKey
end

function GAMsgFormat(Msg)
	local tMsg = Split(Msg,"\\n")
	local NewMsg=""
	for i=1,#tMsg do
		if tMsg[i]~="" and string.find(tMsg[i],"|")==nil then
			NewMsg=NewMsg..strFormat(tMsg[i],45,2).."\\n"
		else
			NewMsg=NewMsg..tMsg[i].."\\n"
		end
		
	end
	return NewMsg
end
function strFormat(str,str_len,str_type)
--[[
һַ
õĿռ䳤
12У3
]]--
	if string.len(str)>str_len then
		str = string.sub(str,1,str_len)
	end
	if(str_type == 3) then
		str_tmp=string.format("%"..str_len.."s", str);
	elseif(str_type == 1) then
		str_tmp=string.format("%-"..str_len.."s", str);
	elseif(str_type == 2) then
		local tmp_splace=math.floor((str_len-string.len(str))/2)+string.len(str);
		str_tmp=string.format("%"..tmp_splace.."s", str);
		str_tmp=string.format("%-"..str_len.."s", str_tmp);
	end
	return str_tmp;
end
function diffDay(basetime,difftime)--Ի 0:00:00 㣬жʱ
	local PassSec = os.date("%S",basetime)
	local PassMin = os.date("%M",basetime)
	local PassHour = os.date("%H",basetime)
	basetime = basetime - PassHour*3600 - PassMin*60 - PassSec
	return math.floor((basetime-difftime)/86400)+1
end

function EquipPlusStat( _ItemIndex,_StatTab,_StatValue )--ǿĽӿ
	--  S-ǿR-ģE-ħD-ħL-ʱ
	--ʹ÷ο if EquipPlusStat(itemindex,"S") ==nil then  EquipPlusStat(itemindex,"S",0) end ֵǴ
	local tStatTab = {}
	if type(_StatTab)=="nil" then
		--GetAll
		local tItemStat = Item.GetData(_ItemIndex,..ò)
		if string.find(tItemStat,",")==nil then
			return nil
		end
		if string.find(tItemStat,"|")==nil then
			local tSub = Split(tItemStat,",")
			tStatTab[tSub[1]]=tonumber(tSub[2])
			return tStatTab
		end
		local tStat = Split(tItemStat,"|")
		for k,v in pairs(tStat) do
			local tSub = Split(v,",")
			tStatTab[tSub[1]]=tonumber(tSub[2])
		end
		return tStatTab
	elseif type(_StatTab)=="table" then
		--SetAll
		local tStat = ""
		for k,v in pairs(_StatTab) do
			tStat = tStat .. k .. "," .. v .. "|"
		end
		Item.SetData(_ItemIndex,..ò,tStat)
	elseif type(_StatTab)=="string" and type(_StatValue)=="nil" then
		--GetSub
		local tStatTab = EquipPlusStat(_ItemIndex) or {}
		for k,v in pairs(tStatTab) do
			if _StatTab==k then
				return tonumber(v)
			end
		end
		return nil
	elseif type(_StatTab)=="string" then
		--SetSub
		local tStatTab = EquipPlusStat(_ItemIndex) or {}
		tStatTab[_StatTab]=_StatValue
		EquipPlusStat(_ItemIndex,tStatTab)
	end
end
function Char.GivePet(_PlayerIndex,PetID)
	local PetMstCdk =Char.GetData(_PlayerIndex,..˺)
	local PetIndex= Char.GivePet(_PlayerIndex,PetID)
	Char.SetData(PetIndex,..CDK,PetMstCdk)
	return PetIndex
end
function Char.DurMaxItemNum(_PlayerIndex,ItmeID) --ȡ;õߵĵλ
	local DurMaxItemPos={}
	for i = 8,27 do
		if ItmeID==Item.GetData(Char.GetItemIndex(_PlayerIndex,i),..ID) then
			if Item.GetData(Char.GetItemIndex(_PlayerIndex,i),..;)==Item.GetData(Char.GetItemIndex(_PlayerIndex,i),..;)  then
				table.insert(DurMaxItemPos,i)
			end
		end
	end
	return DurMaxItemPos
end

function Item.PlusData(Index,Const,Value)--ǿ
	Item.SetData(Index,Const,Item.GetData(Index,Const) + Value)
	return
end
function Char.PlusData(Index,Const,Value)--ǿ
	return Char.SetData(Index,Const,Char.GetData(Index,Const) + Value)
end
function Char.GetPlusData(Index,Const,Value)--ǿȡ
	return Char.SetData(Index,Const,Char.GetData(Index,Const) + Value)
end

function CreateNpc(NPCName)
	IntNpc = IntNpc or {}
	if IntNpc[NPCName] == nil then
		IntNpc[NPCName]  = NL.CreateNpc("lua/Module/System.lua", "Myinit")
	end
	return IntNpc[NPCName]
end
function GQDelLuaNpc(NpcIndex)
	for Name, Index in pairs(IntNpc) do
		if Index==NpcIndex then
			IntNpc[Name] = nil
		end
	end
	NL.DelNpc(NpcIndex)
	return 
end
function SetNpc(NpcIndex,MapId,x,y,Imge,dir,Name)
		Char.SetData(NpcIndex, 3, 0);
		Char.SetData(NpcIndex, %_ͼ%,MapId);      
		Char.SetData(NpcIndex, %_X%,x);         
		Char.SetData(NpcIndex, %_Y%,y);    
		Char.SetData(NpcIndex,%_%,dir);    
		Char.SetData(NpcIndex, %_ԭ%, Name); 
		Char.SetData(NpcIndex, %_ԭ%, Imge);    
		Char.SetData(NpcIndex, %_%, Imge);    
		NLG.UpChar(NpcIndex);
	return -1
end
function Myinit(_MeIndex) --NPC
	return true;
end


function Log(strTxt,_Mode) ----LOGдԶʱ
	local LogFile = "./lua/Lua.Log"
	local file = io.open(LogFile,"a")
	if _Mode==1 then
		file:write(strTxt,"\r\n")
	else
		file:write(os.date("%Y%m%d%X"),strTxt,"\r\n")
	end
	file:close()
end
function Char.DelItemByIndex(_PlayerIndex,_ItemIndex)
	local _ItemNum=Item.GetData(_ItemIndex,%_ѵ%)
	local _ItemDur=Item.GetData(_ItemIndex,..;)
	local _ItemPos=Char.GetItemPos(_PlayerIndex,_ItemIndex)
	LogItem("[ʹü¼][" .. Char.GetData(_PlayerIndex,..˺) .. "]" .. Char.GetData(_PlayerIndex,..ԭ) .. "ʹ"..Item.GetData(_ItemIndex,..Ѽ))
	if _ItemNum>1 then
		Item.PlusData(_ItemIndex,%_ѵ%,-1)
		Item.UpItem(_PlayerIndex,_ItemIndex);
		return  -1
	end
	if _ItemDur>1 then
		Item.PlusData(_ItemIndex,..;,-1)
		Item.UpItem(_PlayerIndex,_ItemIndex);
		return  -2
	end
	return NLG.DelItemByPos(_PlayerIndex,_ItemPos)
end
function LogItem(strTxt,_Mode) ----LOGдԶʱ
	local LogFile = "./lua/Log/"..os.date("%Y-%m-%d").."LogItem.Log"
	local file = io.open(LogFile,"a")
	if _Mode==1 then
		file:write(strTxt,"\r\n")
	else
		file:write(os.date("%Y%m%d%X"),strTxt,"\r\n")
	end
	file:close()
end
function NLG.CanTalk( _MePtr ,_PlayerPtr) 
	if(NLG.CheckInFront(_PlayerPtr,_MePtr,2) == false) then
		return false
	end 
	return true
end
function ShopItemSplit(strTxt,rtSub)
	if (strTxt=='' or strTxt==nil or rtSub=='' or rtSub==nil) then
		return "Error Shop Data";
	end
	local TM_ShopItem = Split(strTxt,"|")
	local TM_ShopItemID = {}
	local TM_ShopItemNum = {}
	for i,v in pairs(TM_ShopItem) do
		if (math.mod(i,2)==1) then
			TM_ShopItemID[#TM_ShopItemID+1]=v
		else
			TM_ShopItemNum[#TM_ShopItemNum+1]=v
		end
	end
	if (string.lower(rtSub)=='item' or rtSub=='Ʒ') then
		return TM_ShopItemID;
	elseif (string.lower(rtSub)=='num' or rtSub=='') then
		return TM_ShopItemNum
	elseif (string.lower(rtSub)=='quantity' or rtSub=='') then
		return #TM_ShopItemID;
	else
		return {TM_ShopItemID,TM_ShopItemNum,#TM_ShopItemID};
	end
end

function Split(szFullString, szSeparator)
	local nFindStartIndex = 1
	local nSplitIndex = 1
	local nSplitArray = {}
	while true do
		local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
		if not nFindLastIndex then
			nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
			break
		end
		nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
		nFindStartIndex = nFindLastIndex + string.len(szSeparator)
		nSplitIndex = nSplitIndex + 1
	end
	return nSplitArray
end
			
function window_check(_tbl)								-------Զҳģ飨ʱ޶tableҳa = {{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},{3},{1},{2},}

local page = 1
local pos = 1
local _table = {}
local _temp = {}

while true do
	if #_tbl - pos*(page - 1) >= 7 then
		pos = 1
		while pos<=8 do
			if pos == 8 then
				_temp[pos] = page
				_table[page] = _temp
				page = page +1
				break
			else
				_temp[pos] = _tbl[pos+(page-1)*7]
			end
			pos = pos +1
		end
		_temp = {}
	end
	pos = pos-1
	if #_tbl - pos*(page - 1) < 7  then
		pos = 1
		while true do
			if _tbl[pos+(page-1)*7] ==nil then
					if pos == 1 then
						_table[page-1][#_table[page-1]] = 0
						break
					end
					_temp[pos] = 0
					_table[page] = _temp
					page = 0
					pos = 0
					break
			else
				_temp[pos] = _tbl[pos+(page-1)*7]
			end
			pos = pos +1
		end
		_temp = {}
		break
	end
end
return _table
end

function type_check (_type)
	if _type =="" then
		_type = 0
		return 3,_type
	elseif _type =="" or _type =="ͷ" then
		_type = 1
		return 3,_type
	elseif _type =="ǹ" then
	_type = 2
	return 3,_type
elseif _type =="" or _type =="" then
	_type = 3
	return 3,_type
elseif _type =="" then
	_type = 4
	return 3,_type
elseif _type =="С" or _type =="" then
	_type = 5
	return 3,_type
elseif _type =="" or _type =="" then
	_type = 6
	return 3,_type
elseif _type =="" or _type =="" then
	_type = 7
	return 3,_type
elseif _type =="ͷ" or _type =="ͷ"then
	_type = 8
	return 3,_type
elseif _type =="ñ" or _type =="ñ" then
	_type = 9
	return 3,_type
elseif _type =="" or _type =="" then
	_type = 10
	return 3,_type
elseif _type =="·" or _type =="" then
	_type = 11
	return 3,_type
elseif _type =="" or _type =="" then
	_type = 12
	return 3,_type
elseif _type =="ѥ" or _type =="ѥ" then
	_type = 13
	return 3,_type
elseif _type =="Ь" or _type =="Ь" then
	_type = 14
	return 3,_type
elseif _type =="ֻ"then
	_type = 15
	return 3,_type
elseif _type =="" then
	_type = 16
	return 3,_type
elseif _type =="" then
	_type = 17
	return 3,_type
elseif _type =="ָ" then
	_type = 18
	return 3,_type
elseif _type =="ͷ" then
	_type = 19
	return 3,_type
elseif _type =="" then
	_type = 20
	return 3,_type
elseif _type =="" then
	_type = 21
	return 3,_type
elseif _type =="" then
	_type = 23
	return 3,_type
elseif _type =="Կ" then
	_type = 28
	return 3,_type
elseif _type =="ʯ" or _type =="" then
	_type = 29
	return 3,_type
elseif _type =="ľ" or _type =="ľ" then
	_type = 30
	return 3,_type
elseif _type =="" then
	_type = 31
	return 3,_type
elseif _type =="" then
	_type = 32
	return 3,_type
elseif _type =="" then
	_type = 33
	return 3,_type
elseif _type =="߲" then
	_type = 34
	return 3,_type
elseif _type =="" or _type =="ʳ" or _type =="" then
	_type = 35
	return 3,_type
elseif _type =="" then
	_type = 36
	return 3,_type
elseif _type =="ʯ" then
	_type = 38
	return 3,_type
elseif _type =="ӡ" then
	_type = 40
	return 3,_type
elseif _type =="ͼ" or _type =="ͼ" then
	_type = 41
	return 3,_type
elseif _type =="ҩˮ" or _type =="ҩ" or _type =="Ѫ" or _type =="Ѫƿ" or _type =="ƿ" then
	_type = 30
	return 3,_type
elseif _type =="Ʊ" then
	_type = 47
	return 3,_type
elseif  type(tonumber(_type) ) == number then
	return 11,_type
else
	return -1,_type
end
end

function GetPetTechPos(_PetIndex)
	local	PetMaxTechPos=Char.GetData(_PetIndex,..)
	NLG.SystemMessage(-1,PetMaxTechPos);
	local str_ChangeWindow = "3|\\nѡ輼\\nѡɾļ\\n";
	for _SkillPos=0,PetMaxTechPos-1 do
		local TechID = Pet.GetTechID(_PetIndex, _SkillPos)
		if TechID<0 then
			str_ChangeWindow = str_ChangeWindow .. "\\n";
		else
			str_ChangeWindow = str_ChangeWindow .. ""..GetTech(TechID).."\\n";
		end
	end
	return str_ChangeWindow
end
function Num(_Data) --ֻ
	return tonumber(_Data)
end

function DelDurItem(_PlayerIndex,ItemIndex) --INDEXȡ posPOS
	local ItemDur = Item.GetData(ItemIndex,..;);
	if(ItemDur - 1 <= 0 )then
		GetItemPos(_PlayerIndex,ItemIndex,1);
	else
		Item.SetData(ItemIndex,..;,tonumber(ItemDur - 1));
		GetItemPos(_PlayerIndex,ItemIndex,0);
	end
	return 1;
end
function DeleteItemUser(_PlayerIndex,ItemIndex) --INDEXȡ posPOS
	local ItemDur = Item.GetData(ItemIndex,..;);
	if(ItemDur - 1 <= 0 )then
		GetItemPos(_PlayerIndex,ItemIndex,1);
	else
		Item.SetData(ItemIndex,..;,tonumber(ItemDur - 1));
		GetItemPos(_PlayerIndex,ItemIndex,0);
	end
	return 1;
end
function UpItemIndex(_PlayerIndex,ItemIndex)
	local i;
	for i=8,28 do
		if (Char.GetItemIndex(_PlayerIndex,i) == ItemIndex)then
			if(Num == 1)then
				NLG.DelItemByPos(_PlayerIndex,i);
				break;
			end
		Item.UpItem(_PlayerIndex,ItemIndex)
		break;
		end
	end
end
function GetItemPos(_PlayerIndex,ItemIndex,Num)---ȡPOSˢidnex
	local i;
	for i=8,28 do
		if (Char.GetItemIndex(_PlayerIndex,i) == ItemIndex)then
			if(Num == 1)then
				NLG.DelItemByPos(_PlayerIndex,i);
				break;
			end
		Item.UpItem(_PlayerIndex,ItemIndex)
		break;
		end
	end
	return i
end

function BattleinEvent(_PlayerIndex)  --жǷսУ-1 0 ս棬 1ս
	local v4=Char.GetData(_PlayerIndex,273);
	local v2=Char.GetData(_PlayerIndex,272);--
	local battin=-1;
	if ( v2 == 6 or v2 < 1 or v4 == -1 ) then
		battin =0 --ﲻս
	else
		battin =1 --ս
	end
	return battin
end

function ȡIndex(_PlayerIndex)
	local tMemberList = {}
	if Char.GetData(_PlayerIndex,%_ģʽ%) == 0 then
		tMemberList={_PlayerIndex}
	else
		for i = 0,4 do
			local tPlayerIndex = Char.GetTeamIndex(_PlayerIndex, i)
			if tPlayerIndex>=0 then
				table.insert(tMemberList, tPlayerIndex)
			end
		end
	end
	return tMemberList
end
function strTrim(s)	--ȥȡļȡβĿո
   return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function GetJobName(JobID)
	local JobTypeName = {}
	JobTypeName[1]=""
	JobTypeName[10]="ʿ"
	JobTypeName[20]="սʿ"
	JobTypeName[30]="ʿ"
	JobTypeName[40]=""
	JobTypeName[50]="ʿ"
	JobTypeName[60]="ʿ"
	JobTypeName[70]="ħʦ"
	JobTypeName[80]="ʦ"
	JobTypeName[90]="ʦ"
	JobTypeName[100]="ʦ"
	JobTypeName[110]="ѱʦ"
	JobTypeName[120]=""
	JobTypeName[130]="ʦ"
	JobTypeName[140]="ʿ"
	JobTypeName[150]=""
	JobTypeName[160]=""
	JobTypeName[200]=""
	JobTypeName[210]="ս"
	JobTypeName[220]="ǹְ"
	JobTypeName[230]="Զ"
	JobTypeName[240]="ְ"
	JobTypeName[250]="ְ"
	JobTypeName[260]="Сְ"
	JobTypeName[270]="װ"
	JobTypeName[280]="װ"
	JobTypeName[290]="ְ"
	JobTypeName[300]="ְ"
	JobTypeName[310]="ְ"
	JobTypeName[320]="ѥְ"
	JobTypeName[330]="Ьְ"
	JobTypeName[340]="ְ"
	JobTypeName[350]="ҩʦ"
	JobTypeName[360]="ҩʦ"
	JobTypeName[370]="ʦ"
	JobTypeName[380]="ʦ"
	JobTypeName[390]="ʦ"
	JobTypeName[400]="Ϥ"
	JobTypeName[410]="̽"
	JobTypeName[420]=""
	JobTypeName[430]="ʿ"
	JobTypeName[440]="ҽ"
	JobTypeName[450]="ɼ"
	JobTypeName[460]=""
	JobTypeName[470]=""
	JobTypeName[480]=""
	JobTypeName[1000]="ʦ"
	JobTypeName[1010]="֯"
	JobTypeName[170]="ʥʿ"
	JobTypeName[180]="Ӱ̿"
	JobTypeName[190]="ħʿ"
	JobTypeName[720]="װ"
	JobTypeName[710]="װ"
	JobTypeName[730]="װ"
	JobTypeName[740]="ȭ"
	if JobID==0 then JobID=1 end
	if JobTypeName[JobID] == nil then
		JobTypeName[JobID]="δ֪"
	end
	return JobTypeName[JobID]
end


local ItemTypeName={}
ItemTypeName[1]=""
ItemTypeName[2]=""
ItemTypeName[3]="ǹ"
ItemTypeName[4]=""
ItemTypeName[5]=""
ItemTypeName[6]="С"
ItemTypeName[7]=""
ItemTypeName[8]=""
ItemTypeName[9]="ͷ"
ItemTypeName[10]="ñ"
ItemTypeName[11]=""
ItemTypeName[12]=""
ItemTypeName[13]=""
ItemTypeName[14]="ѥ"
ItemTypeName[15]="Ь"
ItemTypeName[16]="ֻ"
ItemTypeName[17]=""
ItemTypeName[18]=""
ItemTypeName[19]="ָ"
ItemTypeName[20]="ȭ"
ItemTypeName[21]=""
ItemTypeName[22]="ͷ"
ItemTypeName[23]="ˮ"
ItemTypeName[24]=""
ItemTypeName[25]="Ҿ"
ItemTypeName[26]="֮"
ItemTypeName[27]=""
ItemTypeName[28]=""
ItemTypeName[29]="Կ"
ItemTypeName[30]=""
ItemTypeName[31]="ľ"
ItemTypeName[32]=""
ItemTypeName[33]=""
ItemTypeName[34]=""
ItemTypeName[35]="߲"
ItemTypeName[36]=""
ItemTypeName[37]=""
ItemTypeName[38]="ҩ"
ItemTypeName[39]="ʯ"
ItemTypeName[40]="֤"
ItemTypeName[41]="ӡ"
ItemTypeName[42]="ͼ"
ItemTypeName[43]=""
ItemTypeName[44]="ҩˮ"
ItemTypeName[45]=""
ItemTypeName[46]="׶ˮ"
ItemTypeName[47]="⾵"
ItemTypeName[48]="Ʊ"
ItemTypeName[49]="Ƭ"
ItemTypeName[50]=""
ItemTypeName[51]=""
ItemTypeName[52]="ը"
ItemTypeName[53]=""
ItemTypeName[54]="ʳ"
ItemTypeName[55]="޵"
ItemTypeName[56]="ͷ"
function GetItemType(ItemTypeID)
	if ItemTypeID==0 then ItemTypeID=1 end
	if ItemTypeName[ItemTypeID] == nil then
		ItemTypeName[ItemTypeID]="δ֪"
	end
	return ItemTypeName[ItemTypeID]
end

function GoToPlayerIntPos(_MeIndex,_PlayerIndex,intMapType,intMap,intPosX,intPosY) --NPC
	local MeMap=Char.GetData(_MeIndex,4)
	local mePosX = Char.GetData(_MeIndex,5);
	local mePosY = Char.GetData(_MeIndex,6);
	local dir=CharGoToDir(mePosX,mePosY,intPosX,intPosY);
	NPCPos_tab=NPCPos_tab or {}
	if NPCPos_tab[_MeIndex]==nil then
		NPCPos_tab[_MeIndex]={mePosX,mePosY,dir}
	end
	
	if  NPCPos_tab[_MeIndex][1]==mePosX and  NPCPos_tab[_MeIndex][2] == mePosY then
		dir=NPCPos_tab[_MeIndex][3]+1
		if dir > 7 then
			dir= 0
		end
		NPCPos_tab[_MeIndex]={mePosX,mePosY,dir}
		if mePosX==intPosX and intPosY==mePosY then
			return 0
		end
		NLG.SetAction(_MeIndex,2)
		print("߶")
		--dir=math.random(0,7)
	end
	NPCPos_tab[_MeIndex]={mePosX,mePosY,dir}
	NLG.WalkMove(_MeIndex,dir);
end
function CharGoToDir(posX_A,posY_A,posX_B,posY_B) --ȡĿ곯
	local TM_Dir = -1;
	if (posX_A==posX_B and posY_A>posY_B) then
		TM_Dir = 0;
	elseif (posX_A<posX_B and posY_A>posY_B) then
		TM_Dir = 1;
	elseif (posX_A<posX_B and posY_A==posY_B) then
		TM_Dir = 2;
	elseif (posX_A<posX_B and posY_A<posY_B) then
		TM_Dir = 3;
	elseif (posX_A==posX_B and posY_A<posY_B) then
		TM_Dir = 4;
	elseif (posX_A>posX_B and posY_A<posY_B) then
		TM_Dir = 5;
	elseif (posX_A>posX_B and posY_A==posY_B) then
		TM_Dir = 6;
	elseif (posX_A>posX_B and posY_A>posY_B) then
		TM_Dir = 7;
	end
	return TM_Dir
end 
function myrandom(a1,a2)
	local b1;
	if(a1>=a2)then
		b1 = math.random(a2,a1);
	else
		b1 = math.random(a1,a2);
	end
	return b1;
end