强力老猫 发表于 2015-12-2 00:19:51

宠物对战抛砖引玉

这个是nerdpack里的宠物对战的部分代码,用起来挺流畅的,但是技能好像是随机用的,抛砖引玉看有没有更好的办法做到一些技能判定之类的


local function getPetHealth(owner, index)
        return math.floor((C_PB.GetHealth(owner, index) / C_PB.GetMaxHealth(owner, index)) * 100)
end

local function scanJournal()
        local petTable = {}
        local maxAmount, petAmount = C_PJ.GetNumPets()
        for i=1,petAmount do
                local guid, id, _, _, lvl, _ , _, name, icon = C_PJ.GetPetInfoByIndex(i)
                local health, maxHealth, attack, speed, rarity = C_PJ.GetPetStats(guid)
                local healthPercentage = math.floor((health / maxHealth) * 100)
                if healthPercentage > tonumber(PeFetch('NePpetBot', 'swapHealth')) then
                        petTable[#petTable+1]={
                                guid = guid,
                                lvl = lvl,
                                attack = attack,
                        }
                end
        end
        if petTable ~= nil then
                if PeFetch('NePpetBot', 'teamtype') == 'BattleTeam' then
                        table.sort(petTable, function(a,b) return a.attack > b.attack end)
                else
                        table.sort(petTable, function(a,b) return a.lvl > b.lvl end)
                end
                maxPetLvl = petTable.lvl
        end
        return petTable
end

local function scanLoadOut()
        local loadOut = {}
        for k=1,3 do
                local petID, petSpellID_slot1, petSpellID_slot2, petSpellID_slot3, locked = C_PJ.GetPetLoadOutInfo(k)
                local _,_, level, _,_,_,_, petName, petIcon, petType, _,_,_,_, canBattle = C_PJ.GetPetInfoByPetID(petID)
                local health, maxHealth, attack, speed, rarity = C_PJ.GetPetStats(petID)
                local healthPercentage = math.floor((health / maxHealth) * 100)
                loadOut[#loadOut+1] = {
                        health = healthPercentage,
                        level = level,
                        id = petID,
                        attack = attack
                }
        end
        return loadOut
end

local function buildBattleTeam()
        if PeFetch('NePpetBot', 'teamtype') == 'BattleTeam' then
                local petTable = scanJournal()
                for i=1,#petTable do
                        if #petTable > 0 and not C_PJ.PetIsSlotted(petTable.guid) then
                                local loadOut = scanLoadOut()
                                for k=1,#loadOut do
                                        if loadOut.level < maxPetLvl then
                                                if loadOut.level < maxPetLvl or loadOut.health < tonumber(PeFetch('NePpetBot', 'swapHealth'))
                                                or not C_PJ.PetIsFavorite(loadOut.id) and PeFetch('NePpetBot', 'favorites')
                                                or loadOut.attack < petTable.attack then
                                                        C_PJ.SetPetLoadOutInfo(k, petTable.guid)
                                                        break
                                                end
                                        end
                                end
                        end
                end
        end
end

local function buildLevelingTeam()
        if PeFetch('NePpetBot', 'teamtype') == 'LvlngTeam' then
                local petTable = scanJournal()
                for i=1,#petTable do
                        if #petTable > 0 and not C_PJ.PetIsSlotted(petTable.guid) then
                                local loadOut = scanLoadOut()
                                for k=1,#loadOut do
                                        if loadOut.level >= maxPetLvl then
                                                if loadOut.level >= maxPetLvl or loadOut.health < tonumber(PeFetch('NePpetBot', 'swapHealth'))
                                                or not C_PJ.PetIsFavorite(loadOut.id) and PeFetch('NePpetBot', 'favorites') then
                                                        C_PJ.SetPetLoadOutInfo(k, petTable.guid)
                                                        break
                                                end
                                        end
                                end
                        end
                end
        end
end

local function scanGroup()
        local petAmount = C_PB.GetNumPets(1)
        local goodPets = {}
        for k=1,petAmount do
                local health = getPetHealth(1, k)
                if health > tonumber(PeFetch('NePpetBot', 'swapHealth')) then
                        goodPets[#goodPets+1] = {
                                id = k,
                                health = health
                        }
                end
        end
        table.sort(goodPets, function(a,b) return a.health > b.health end)
        return goodPets
end

local function PetSwap()
        local activePet = C_PB.GetActivePet(1)
        local goodPets = scanGroup()
        if #goodPets < 1 then
                C_PB.ForfeitGame()
        else
                for i=1,#goodPets do
                        if getPetHealth(1, activePet) <= tonumber(PeFetch('NePpetBot', 'swapHealth')) then
                                C_PB.ChangePet(goodPets.id)
                                break
                        end
                end
        end
        return false
end

local function scanPetAbilitys()
        local Abilitys = {}
        local activePet = C_PB.GetActivePet(1)
        local enemieActivePet = C_PB.GetActivePet(2)
        for i=3,1,-1 do
                local isUsable, currentCooldown = C_PB.GetAbilityState(1, activePet, i)
                if isUsable then
                        local id, name, icon, maxcooldown, desc, numTurns, abilityPetType, nostrongweak = C_PB.GetAbilityInfo(1, activePet, i)
                        local enemieType = C_PB.GetPetType(2, enemieActivePet)
                        local attackModifer = C_PB.GetAttackModifier(abilityPetType, enemieType)
                        local power = C_PB.GetPower(1, activePet)
                        local totalDmg = power*attackModifer
                        Abilitys[#Abilitys+1]={
                                dmg = totalDmg,
                                name = name,
                                icon = icon,
                                id = i
                        }
                        --print(i..' '..totalDmg..'( '..power..' \ '..attackModifer..' \ '..numTurns..' )'..maxcooldown)
                end
        end
        table.sort(Abilitys, function(a,b) return a.dmg > b.dmg end)
        return Abilitys
end

local _lastAttack = '...'
local function PetAttack()
        local Abilitys = scanPetAbilitys()
        for i=1,#Abilitys do
                if #Abilitys > 1 and _lastAttack ~= Abilitys.name or #Abilitys <=1 then
                        if Abilitys ~= nil then
                                _lastAttack = Abilitys.name
                                petBotGUI.elements.lastAttack:SetText('|T'..Abilitys.icon..':10:10|t'..Abilitys.name)
                                C_PB.UseAbility(Abilitys.id)
                        end
                end
        end
        C_PB.SkipTurn()
end

C_Timer.NewTicker(0.5, (function()
        if NeP.Core.CurrentCR and petBotGUI.parent:IsShown() then
                local activePet = C_PB.GetActivePet(1)
                local enemieActivePet = C_PB.GetActivePet(2)
               
                -- Pet 1
                local petID, petSpellID_slot1, petSpellID_slot2, petSpellID_slot3, locked = C_PJ.GetPetLoadOutInfo(1)
                local _,_, level, _,_,_,_, petName, petIcon, petType, _,_,_,_, canBattle = C_PJ.GetPetInfoByPetID(petID)
                petBotGUI.elements.petslot1:SetText('|T'..petIcon..':10:10|t'..petName)

                -- Pet 2
                local petID, petSpellID_slot1, petSpellID_slot2, petSpellID_slot3, locked = C_PJ.GetPetLoadOutInfo(2)
                local _,_, level, _,_,_,_, petName, petIcon, petType, _,_,_,_, canBattle = C_PJ.GetPetInfoByPetID(petID)
                petBotGUI.elements.petslot2:SetText('|T'..petIcon..':10:10|t'..petName)

                -- Pet 3
                local petID, petSpellID_slot1, petSpellID_slot2, petSpellID_slot3, locked = C_PJ.GetPetLoadOutInfo(3)
                local _,_, level, _,_,_,_, petName, petIcon, petType, _,_,_,_, canBattle = C_PJ.GetPetInfoByPetID(petID)
                petBotGUI.elements.petslot3:SetText('|T'..petIcon..':10:10|t'..petName)

                if isRunning and not C_PB.IsWaitingOnOpponent() then
                        if not C_PB.IsInBattle() then
                                buildBattleTeam()
                                buildLevelingTeam()
                        else
                                -- Trap
                                if getPetHealth(2, enemieActivePet) <= 35 and PeFetch('NePpetBot', 'trap') and C_PB.IsWildBattle() and C_PB.IsTrapAvailable() then
                                        C_PB.UseTrap()
                                -- Swap
                                elseif not PetSwap() then
                                        if C_PB.GetBattleState() == 3 then
                                                PetAttack()
                                        end
                                end
                        end
                end
        end
end), nil)

优优默默 发表于 2015-12-2 03:11:58

太高深了,技术贴赶紧顶!

God_Envy 发表于 2015-12-2 08:42:19

这个厉害啊,我要百度一下{:5_249:}

jindy 发表于 2016-4-3 11:48:02

非常感谢诶楼主分享@!@!@

10086qq 发表于 2016-4-3 14:56:42

liuyucoco 发表于 2016-4-26 15:42:15

太高深了,技术贴赶紧顶!感谢分享

feng3498 发表于 2017-5-9 07:29:24

看不懂啊。。。。。。。。。。

leonokok 发表于 2023-2-25 20:26:39

{:5_240:}{:5_240:}{:5_238:}{:5_158:}
页: [1]
查看完整版本: 宠物对战抛砖引玉