Pet.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Pet = {}
  2. local this = {}
  3. function Pet.onCall(actor, petCfgId, petId)
  4. local attributeRole = ConfigDataManager.getTableValue("cfg_pet","attributerole","id", petCfgId)
  5. if string.isNullOrEmpty(attributeRole) then
  6. return
  7. end
  8. local mapId = getbaseinfo(actor, "unimapid")
  9. local petActor = getactor(petId, mapId)
  10. local attMap = string.toStringStringMap(attributeRole, "#", "|")
  11. for id, percent in pairs(attMap) do
  12. local attrName = attrid2name(actor, id)
  13. local attrValue = getattrinfo(actor, attrName)
  14. local scale = tonumber(percent) / 10000
  15. local addValue = math.round( attrValue * scale)
  16. jprint("召唤兽继承主人属性", attrName, attrValue, scale, addValue )
  17. updateattrgroup(petActor, "master", attrName, addValue)
  18. end
  19. jprint("当前主人的最大攻击力", getattrinfo(actor, "maxDC"))
  20. jprint("当前召唤兽的最大攻击力", getattrinfo(petActor, "maxDC"))
  21. this.addPetBuff(petActor, petCfgId)
  22. end
  23. function this.addPetBuff(petActor, petCfgId)
  24. local buffId = ConfigDataManager.getTableValue("cfg_pet","buffid","id", petCfgId)
  25. if string.isNullOrEmpty(buffId) then
  26. return
  27. end
  28. local strBuffs = string.split(buffId, "|")
  29. local buffIds = {}
  30. --jprint("配置字段:", buffId, "竖线分隔结果:", strBuffs)
  31. for _, values in pairs(strBuffs) do
  32. local weights = string.toStringStringMap(values,"#","&")
  33. local buffCfgId = this.randomByWeight(weights)
  34. --jprint("weights:", weights, "随机结果:", buffCfgId)
  35. table.insert(buffIds, buffCfgId)
  36. end
  37. for _, buffCfgId in pairs(buffIds) do
  38. addbuff(petActor, buffCfgId, 1)
  39. end
  40. --jprint("召唤兽信息:",petActor,"添加召唤兽的buff成功:", buffId, "随机到的buff信息:", buffIds)
  41. end
  42. function this.randomByWeight(weights)
  43. local totalWeight = 0
  44. for _, weight in pairs(weights) do
  45. weight = tonumber(weight)
  46. totalWeight = totalWeight + weight
  47. end
  48. local randomValue = math.random(1, totalWeight)
  49. -- 遍历权重数组,找到对应的下标
  50. local accumulatedWeight = 0
  51. for i, weight in pairs(weights) do
  52. accumulatedWeight = accumulatedWeight + weight
  53. if randomValue <= accumulatedWeight then
  54. return i
  55. end
  56. end
  57. end
  58. function Pet.ClearPet(actor)
  59. local pets = getpets(actor)
  60. if pets == nil then
  61. return
  62. end
  63. for _, petActor in pairs(pets) do
  64. local petInfo = getmonsterinfo(petActor)
  65. local petConfigId = petInfo["cfgid"]
  66. info("宠物配置信息",petConfigId)
  67. if tonumber(petConfigId) ~= tonumber(PetConstant.Tian_Ying) then
  68. removepet(petActor)
  69. end
  70. end
  71. end
  72. function Pet.PetDie(actor,monsterCfgId)
  73. local ai = ConfigDataManager.getTableValue("cfg_monster", "ai", "id", monsterCfgId)
  74. if ai == nil then
  75. error("updateMonsterReliveTime cfg_monster表ai字段为空 monsterCfgId:" .. monsterCfgId)
  76. return
  77. end
  78. local aiConfig = ConfigDataManager.getById("cfg_monster_ai", ai)
  79. if aiConfig == nil then
  80. error("updateMonsterReliveTime cfg_monster_ai为nil id:" .. ai)
  81. return
  82. end
  83. local deadDisappear = tonumber(aiConfig['deaddisappear'])
  84. if not string.isNullOrEmpty(deadDisappear) then
  85. local mills = tonumber(deadDisappear)
  86. local sec = mills / 1000
  87. if sec > 0 then
  88. info(sec .. "秒后移除召唤兽", actor)
  89. setontimer(actor, 3, sec)
  90. else
  91. sec = 1
  92. info(sec .. "秒后移除召唤兽", actor)
  93. setontimer(actor, 3, sec)
  94. end
  95. end
  96. end
  97. function ontimer3(actor)
  98. removepet(actor)
  99. info("移除召唤兽", actor)
  100. end