LUACN论坛

 找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
热搜: YJWOW MagicStone BoL
查看: 8184|回复: 7

[综合] 宠物对战抛砖引玉

[复制链接]
发表于 2015-12-2 00:19:51 | 显示全部楼层 |阅读模式
这个是nerdpack里的宠物对战的部分代码,用起来挺流畅的,但是技能好像是随机用的,抛砖引玉看有没有更好的办法做到一些技能判定之类的


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

  4. local function scanJournal()
  5.         local petTable = {}
  6.         local maxAmount, petAmount = C_PJ.GetNumPets()
  7.         for i=1,petAmount do
  8.                 local guid, id, _, _, lvl, _ , _, name, icon = C_PJ.GetPetInfoByIndex(i)
  9.                 local health, maxHealth, attack, speed, rarity = C_PJ.GetPetStats(guid)
  10.                 local healthPercentage = math.floor((health / maxHealth) * 100)
  11.                 if healthPercentage > tonumber(PeFetch('NePpetBot', 'swapHealth')) then
  12.                         petTable[#petTable+1]={
  13.                                 guid = guid,
  14.                                 lvl = lvl,
  15.                                 attack = attack,
  16.                         }
  17.                 end
  18.         end
  19.         if petTable[1] ~= nil then
  20.                 if PeFetch('NePpetBot', 'teamtype') == 'BattleTeam' then
  21.                         table.sort(petTable, function(a,b) return a.attack > b.attack end)
  22.                 else
  23.                         table.sort(petTable, function(a,b) return a.lvl > b.lvl end)
  24.                 end
  25.                 maxPetLvl = petTable[1].lvl
  26.         end
  27.         return petTable
  28. end

  29. local function scanLoadOut()
  30.         local loadOut = {}
  31.         for k=1,3 do
  32.                 local petID, petSpellID_slot1, petSpellID_slot2, petSpellID_slot3, locked = C_PJ.GetPetLoadOutInfo(k)
  33.                 local _,_, level, _,_,_,_, petName, petIcon, petType, _,_,_,_, canBattle = C_PJ.GetPetInfoByPetID(petID)
  34.                 local health, maxHealth, attack, speed, rarity = C_PJ.GetPetStats(petID)
  35.                 local healthPercentage = math.floor((health / maxHealth) * 100)
  36.                 loadOut[#loadOut+1] = {
  37.                         health = healthPercentage,
  38.                         level = level,
  39.                         id = petID,
  40.                         attack = attack
  41.                 }
  42.         end
  43.         return loadOut
  44. end

  45. local function buildBattleTeam()
  46.         if PeFetch('NePpetBot', 'teamtype') == 'BattleTeam' then
  47.                 local petTable = scanJournal()
  48.                 for i=1,#petTable do
  49.                         if #petTable > 0 and not C_PJ.PetIsSlotted(petTable[i].guid) then
  50.                                 local loadOut = scanLoadOut()
  51.                                 for k=1,#loadOut do
  52.                                         if loadOut[k].level < maxPetLvl then
  53.                                                 if loadOut[k].level < maxPetLvl or loadOut[k].health < tonumber(PeFetch('NePpetBot', 'swapHealth'))
  54.                                                 or not C_PJ.PetIsFavorite(loadOut[k].id) and PeFetch('NePpetBot', 'favorites')
  55.                                                 or loadOut[k].attack < petTable[i].attack then
  56.                                                         C_PJ.SetPetLoadOutInfo(k, petTable[i].guid)
  57.                                                         break
  58.                                                 end
  59.                                         end
  60.                                 end
  61.                         end
  62.                 end
  63.         end
  64. end

  65. local function buildLevelingTeam()
  66.         if PeFetch('NePpetBot', 'teamtype') == 'LvlngTeam' then
  67.                 local petTable = scanJournal()
  68.                 for i=1,#petTable do
  69.                         if #petTable > 0 and not C_PJ.PetIsSlotted(petTable[i].guid) then
  70.                                 local loadOut = scanLoadOut()
  71.                                 for k=1,#loadOut do
  72.                                         if loadOut[k].level >= maxPetLvl then
  73.                                                 if loadOut[k].level >= maxPetLvl or loadOut[k].health < tonumber(PeFetch('NePpetBot', 'swapHealth'))
  74.                                                 or not C_PJ.PetIsFavorite(loadOut[k].id) and PeFetch('NePpetBot', 'favorites') then
  75.                                                         C_PJ.SetPetLoadOutInfo(k, petTable[i].guid)
  76.                                                         break
  77.                                                 end
  78.                                         end
  79.                                 end
  80.                         end
  81.                 end
  82.         end
  83. end

  84. local function scanGroup()
  85.         local petAmount = C_PB.GetNumPets(1)
  86.         local goodPets = {}
  87.         for k=1,petAmount do
  88.                 local health = getPetHealth(1, k)
  89.                 if health > tonumber(PeFetch('NePpetBot', 'swapHealth')) then
  90.                         goodPets[#goodPets+1] = {
  91.                                 id = k,
  92.                                 health = health
  93.                         }
  94.                 end
  95.         end
  96.         table.sort(goodPets, function(a,b) return a.health > b.health end)
  97.         return goodPets
  98. end

  99. local function PetSwap()
  100.         local activePet = C_PB.GetActivePet(1)
  101.         local goodPets = scanGroup()
  102.         if #goodPets < 1 then
  103.                 C_PB.ForfeitGame()
  104.         else
  105.                 for i=1,#goodPets do
  106.                         if getPetHealth(1, activePet) <= tonumber(PeFetch('NePpetBot', 'swapHealth')) then
  107.                                 C_PB.ChangePet(goodPets[i].id)
  108.                                 break
  109.                         end
  110.                 end
  111.         end
  112.         return false
  113. end

  114. local function scanPetAbilitys()
  115.         local Abilitys = {}
  116.         local activePet = C_PB.GetActivePet(1)
  117.         local enemieActivePet = C_PB.GetActivePet(2)
  118.         for i=3,1,-1 do
  119.                 local isUsable, currentCooldown = C_PB.GetAbilityState(1, activePet, i)
  120.                 if isUsable then
  121.                         local id, name, icon, maxcooldown, desc, numTurns, abilityPetType, nostrongweak = C_PB.GetAbilityInfo(1, activePet, i)
  122.                         local enemieType = C_PB.GetPetType(2, enemieActivePet)
  123.                         local attackModifer = C_PB.GetAttackModifier(abilityPetType, enemieType)
  124.                         local power = C_PB.GetPower(1, activePet)
  125.                         local totalDmg = power*attackModifer
  126.                         Abilitys[#Abilitys+1]={
  127.                                 dmg = totalDmg,
  128.                                 name = name,
  129.                                 icon = icon,
  130.                                 id = i
  131.                         }
  132.                         --print(i..' '..totalDmg..'( '..power..' \ '..attackModifer..' \ '..numTurns..' )'..maxcooldown)
  133.                 end
  134.         end
  135.         table.sort(Abilitys, function(a,b) return a.dmg > b.dmg end)
  136.         return Abilitys
  137. end

  138. local _lastAttack = '...'
  139. local function PetAttack()
  140.         local Abilitys = scanPetAbilitys()
  141.         for i=1,#Abilitys do
  142.                 if #Abilitys > 1 and _lastAttack ~= Abilitys[i].name or #Abilitys <=1 then
  143.                         if Abilitys[i] ~= nil then
  144.                                 _lastAttack = Abilitys[i].name
  145.                                 petBotGUI.elements.lastAttack:SetText('|T'..Abilitys[i].icon..':10:10|t'..Abilitys[i].name)
  146.                                 C_PB.UseAbility(Abilitys[i].id)
  147.                         end
  148.                 end
  149.         end
  150.         C_PB.SkipTurn()
  151. end

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

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

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

  169.                 if isRunning and not C_PB.IsWaitingOnOpponent() then
  170.                         if not C_PB.IsInBattle() then
  171.                                 buildBattleTeam()
  172.                                 buildLevelingTeam()
  173.                         else
  174.                                 -- Trap
  175.                                 if getPetHealth(2, enemieActivePet) <= 35 and PeFetch('NePpetBot', 'trap') and C_PB.IsWildBattle() and C_PB.IsTrapAvailable() then
  176.                                         C_PB.UseTrap()
  177.                                 -- Swap
  178.                                 elseif not PetSwap() then
  179.                                         if C_PB.GetBattleState() == 3 then
  180.                                                 PetAttack()
  181.                                         end
  182.                                 end
  183.                         end
  184.                 end
  185.         end
  186. end), nil)
复制代码


回复

使用道具 举报

发表于 2015-12-2 03:11:58 来自手机 | 显示全部楼层
太高深了,技术贴赶紧顶!
回复 支持 反对

使用道具 举报

发表于 2015-12-2 08:42:19 | 显示全部楼层
这个厉害啊,我要百度一下
回复 支持 反对

使用道具 举报

发表于 2016-4-3 11:48:02 | 显示全部楼层
非常感谢诶楼主分享@!@!@
回复 支持 反对

使用道具 举报

发表于 2016-4-3 14:56:42 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
回复 支持 反对

使用道具 举报

发表于 2016-4-26 15:42:15 | 显示全部楼层
太高深了,技术贴赶紧顶!感谢分享
回复 支持 反对

使用道具 举报

发表于 2017-5-9 07:29:24 来自手机 | 显示全部楼层
看不懂啊。。。。。。。。。。
回复 支持 反对

使用道具 举报

发表于 2023-2-25 20:26:39 | 显示全部楼层
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

小黑屋|手机版|Archiver|LUACN论坛

GMT+8, 2024-4-20 07:59 AM , Processed in 0.072978 second(s), 30 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表