--[[
飺
    κ3CһӢ۽űýűʵһӢۣԶȨȵӢ۲ʹõűָӢϡ

÷	ĬÿʹZԶһ
	лӢۣѡӢۺʹCtrl+`ݼɸıԺ󾻻
	Ƿ񷵻ִвǰĵǰӢý鲻ҪãԼһʱ䣡

ע⣺
       1. һֳ֧κ3C 5ӢۣҴھȨȵߡýűҪӢΪԼӢۡ
       2. ʹһӢȫھȨȵͷŷΧڣﵽЧ
       3. һ5Ӣ£ӳ(С)ʱִоȻڲߵӦʱ˵ӦѾܿˣ
CPUĲʱ൱ģִһʱйлѡӢ۵κβ򽫻жһ
       4. ʱʱӾãùֱ࣬ӵһʱֱӡͬʱӦע3㡣

                                       ߣҹ
                                       			]]

--û޸Ĵ
ClickKey = 90		--ĬϲZԼɸı
hDestID = 'hctO'	--ĿӢ(Լ)Ĭţͷ
sRes = 'shsw'		--ʹƷID(ڼЧõID羻Ȩ)
isReturnCur = false	--Ƿ񷵻ִвǰĵǰӢ
tSleep = 10		--(λms)һʱʱ䣬ģʽĬ10msʵ
--û޸Ĵ

function keypressEx(keycode)
    hWar3 = getwar3window()
    sendmessage(hWar3, 256, keycode, 0)
    sleep(10)
    sendmessage(hWar3, 257, keycode, 0)
end

function hasRes(hHD)
    hasPurif_=false
    for i=0,5,1 do
        gHD,gID,gNUM= getunititem(hHD,i)
        if gID==sRes and gNUM>0 then
            hasPurif_ = true
        end
    end
    return hasPurif_
end

function search(heroID)
    distHero = nil
    haResHero = 0
    keypressEx(112)
    sleep(tSleep)
    hnID,hnHD = getcurrentunit()
    if hID==heroID then
        distHero = 'F1'
    end
    if hasRes(hnHD) then
        haResHero =  112
    end  
    for iPos=1,4,1 do
        hoID = hnID
        keypressEx(112+iPos)
	iTick = gettickcount()
	while true do
	    hnID,hnHD = getcurrentunit()
	    if hnID~=hoID or (gettickcount()-iTick)>tSleep then
		break
	    end
	end
	if hnID==hoID then
	    break
	end
        iPos = iPos + 1
        if hnID==heroID then
            distHero = 'F'..iPos 
        end
        if hasRes(hnHD) then
            haResHero =  112+iPos-1
        end
    end
    return distHero,haResHero
end

function searchPosition(heroID)
    keypressEx(112)
    sleep(tSleep)
    hnID,hnHD = getcurrentunit()
    if hnID==heroID then
        return 'F1'
    end
    for iPos=1,4,1 do
        hoID = hnID
        keypressEx(112+iPos)
	iTick = gettickcount()
	while true do
	    hnID,hnHD = getcurrentunit()
	    if hnID~=hoID or (gettickcount()-iTick)>tSleep then
		break
	    end
	end
	if hnID==hoID then
	    break
	end
        iPos = iPos + 1
        if hnID==heroID then
            return 'F'..iPos 
        end            
    end
    return nil
end

setkeywatch('keyhookfunc')

function keyhookfunc(keycode, controlkey, downorup)
    if 0==iswar3front() then return 0 end
    if 1==ischat() then return 0 end
    if keycode==192 and 16==controlkey then 
        hID,hHD = getcurrentunit()
	if hID~=nil and hID~='' then
	    hDestID = hID
	end
	return 1
    end
    if keycode==ClickKey and downorup==0 then
	hoID = ''
	hoHD = ''
	hnID = ''
	hnHD = ''
	thisdistH = ''
	thisHaRes = 0
        hcurID, hcurHD = getcurrentunit()
        thisdistH, thisHaRes = search(hDestID)
	if thisdistH~= nil then
            if thisHaRes~=0 then
	        sleep(1)
		keypressEx(thisHaRes)
		sleep(tSleep)
                runkeys('ʹƷ'..sRes..' Ӣͷ'..thisdistH)
            end
	    if isReturnCur then
		sleep(tSleep)
		hcerHero = searchPosition(hcurID)
		if hcerHero~=nil then
		    sleep(tSleep)
		    runkeys('Ӣͷ'..hcerHero..' Ӣͷ'..hcerHero)
		end
	    end
	end
        return 1
    end
    return 0
end