SkinLogic.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. local Msg = require("core.Msg")
  2. local Grid = require("bag.Grid")
  3. local BagLogic = require("bag.BagLogic")
  4. local Broadcast = require("broadcast.Broadcast")
  5. local Lang = require("common.Lang")
  6. local Util = require("common.Util")
  7. local ItemDefine = require("bag.ItemDefine")
  8. local BeSkill = require("combat.BeSkill")
  9. local HeroLogic = require("hero.HeroLogic")
  10. local RoleHeadLogic = require("role.RoleHeadLogic")
  11. local SkinExcel = require("excel.skin").skin
  12. local HeroExcel = require("excel.hero").hero
  13. local SkillExcel = require("excel.skin").skill
  14. local OutExcel = require("excel.skin").out
  15. local ItemExcel = require("excel.item").item
  16. local RoleAttr = require("role.RoleAttr")
  17. local function getBag(human)
  18. if not (human.skinBag and human.heroSkin) then
  19. human.skinBag = {}
  20. human.heroSkin = {}
  21. human.hasSkin = {}
  22. for i = 1,human.db.skinBag[0] do
  23. local data = human.db.skinBag[i]
  24. if data then
  25. if data.heroInd then
  26. local skinSkillID
  27. local heroGrid = human.db.heroBag[data.heroInd]
  28. if not heroGrid then
  29. data.heroInd = nil
  30. else
  31. local heroID = heroGrid.id
  32. local heroConf = HeroExcel[heroID]
  33. for _,v in ipairs(heroConf.skin) do
  34. if v[1] == data.id then
  35. human.heroSkin[data.heroInd] = {i,tonumber(v[2])}
  36. end
  37. end
  38. end
  39. end
  40. human.skinBag[i] = data.id
  41. human.hasSkin[data.id] = human.hasSkin[data.id] or {}
  42. human.hasSkin[data.id][i] = 1
  43. end
  44. end
  45. end
  46. return human.db.skinBag
  47. end
  48. --[[
  49. skin = {
  50. ttl = number -- 有效时长 -1表示永久
  51. state = number -- 0表示未穿戴 1 表示穿戴
  52. } -- skin 拥有唯一性
  53. ]]
  54. -- 获取所有皮肤信息
  55. local function skinListQuery(human)
  56. local skinBag = human.db.skinBag
  57. local ret = {}
  58. for id,data in pairs(skinBag) do
  59. ret[id] = {
  60. id = id,
  61. ttl = data.ttl,
  62. state = data.state,
  63. }
  64. end
  65. return ret
  66. end
  67. -- 获取皮肤为skinId的皮肤
  68. local function skinQueryById(human,skinId)
  69. local skin = human.db.skinBag[skinId]
  70. if skin then
  71. return {
  72. id = skinId,
  73. ttl = skin.ttl,
  74. state = skin.state,
  75. }
  76. end
  77. end
  78. -- 生成发送给客户端的皮肤数据
  79. local function skinNetGen(human,net,id)
  80. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  81. net.id = id
  82. net.name = skinCfg.name
  83. data.desc = skinCfg.desc
  84. data.head = skinCfg.head
  85. data.body = skinCfg.body
  86. data.icon = skinCfg.icon
  87. data.camp = skinCfg.camp
  88. data.order = skinCfg.order
  89. data.type = skinCfg.type
  90. local attrLen = 0
  91. for k,v in ipairs(skinCfg.attrs) do
  92. attrLen = attrLen + 1
  93. data.attrs[attrLen].key = v[1]
  94. data.attrs[attrLen].value = v[2]
  95. end
  96. data.attrs[0] = attrLen
  97. data.heroName = HeroExcel[skinCfg.heroId] and HeroExcel[skinCfg.heroId].name or ""
  98. end
  99. -- 皮肤穿戴
  100. --[[
  101. @param2 = id -- 皮肤Id
  102. @param3 = heroIdx -- 对应英雄存档的idx
  103. ]]
  104. local function skinOn(human,id,heroIdx)
  105. local skin = skinQueryById(human,id)
  106. -- 当前skin不存在 或者 皮肤已经穿戴
  107. if not skin or skin.state == 1 then
  108. return Broadcast.sendErr(human, id..":skinOn:"..Lang.SKIN_PARAM_ERR)
  109. end
  110. local heroId = HeroLogic.getHeroIdByIndex(heroIdx)
  111. local heroGrid = HeroLogic.getHeroGrid(human.heroIdx,heroId)
  112. -- 英雄不存在 或者 英雄已经装备当前皮肤
  113. if not heroGrid then
  114. return Broadcast.sendErr(human, heroIdx..":heroIdx:"..Lang.SKIN_PARAM_ERR)
  115. end
  116. if heroGrid.skin == id then
  117. return Broadcast.sendErr(human, Lang.SKIN_DOUBLE_ON)
  118. end
  119. human.db.skinBag[id].state = 1
  120. -- 对所有heroId相同的英雄操作
  121. local heroIdxList = HeroLogic.getHeroListById(human,heroId)
  122. for _,idx in ipairs(heroIdxList) do
  123. human.db.heroBag[heroIdx].skinOn = id
  124. end
  125. local msgRet = Msg.gc.GC_SKIN_ON
  126. msgRet.heroInd = heroIdx
  127. msgRet.skinInd = id
  128. Msg.send(msgRet,human.fd)
  129. end
  130. -- 皮肤脱下
  131. --[[
  132. @param2 = heroIdx -- 对应英雄存档的idx
  133. ]]
  134. local function skinOff(human,heroIdx)
  135. local heroId = HeroLogic.getHeroIdByIndex(heroIdx)
  136. local heroGrid = HeroLogic.getHeroGrid(human,heroIdx,heroId)
  137. local skinId = heroGrid.skin
  138. -- 英雄不存在 或者 英雄未穿戴皮肤
  139. if not heroGrid or not skinId then
  140. return Broadcast.sendErr(human, heroIdx..":heroIdx:"..Lang.SKIN_PARAM_ERR)
  141. end
  142. human.db.skinBag[skinId].state = 0
  143. -- 对所有heroId相同的英雄操作
  144. local heroIdxList = HeroLogic.getHeroListById(human,heroId)
  145. for _,idx in ipairs(heroIdxList) do
  146. human.db.heroBag[idx].skinOn = nil
  147. end
  148. local msgRet = Msg.gc.GC_SKIN_ON
  149. msgRet.heroInd = heroIdx
  150. msgRet.skinInd = skinId
  151. Msg.send(msgRet,human.fd)
  152. end
  153. --------------------------------------------------------------
  154. function initAfterHot()
  155. for _,outConf in pairs(OutExcel) do
  156. outConf.totalWeight = 0
  157. for _,v in ipairs(outConf.out) do
  158. outConf.totalWeight = outConf.totalWeight + v[2]
  159. end
  160. end
  161. end
  162. -- 优先使用皮肤的技能,新版本没有默认返回false
  163. function setSkill(human,heroInd,heroConf,obj)
  164. return false
  165. --[[if not human then return end
  166. if not human.db.skinBag then
  167. --假人
  168. return
  169. end
  170. local bagDB = getBag(human)
  171. local skinSkillID = human.heroSkin[heroInd] and human.heroSkin[heroInd][2]
  172. if not skinSkillID then
  173. return
  174. end
  175. local skillConf = SkillExcel[skinSkillID]
  176. Skill.setSkill(obj, heroConfig,skillConf)
  177. BeSkill.setBeSkill(obj,heroConf,skillConf)
  178. return true]]
  179. end
  180. function checkHeroSkin(human, heroInd)
  181. if not heroInd then return end
  182. if not human or not human.db then return end
  183. local heroId = HeroLogic.getHeroIdByIndex(heroInd)
  184. local heroGrid = HeroLogic.getHeroGrid(human,heroInd,heroId)
  185. if heroGrid.skinOn then
  186. return heroGrid.skinOn,true
  187. end
  188. --[[if not human.db.skinBag then
  189. return
  190. end
  191. local bagDB = getBag(human)
  192. if human.heroSkin[heroInd] then
  193. local skinInd = human.heroSkin[heroInd][1]
  194. local skinID = bagDB[skinInd].id
  195. local skillID = human.heroSkin[heroInd][2]
  196. return skinID,skillID
  197. else
  198. return
  199. end
  200. ]]
  201. end
  202. -- 检查是否有皮肤body
  203. function getBody(human,heroInd)
  204. local heroId = HeroLogic.getHeroIdByIndex(heroInd)
  205. local heroGrid = HeroLogic.getHeroGrid(human,heroInd,heroId)
  206. local skinId = heroGrid.skinOn
  207. if not skinId then
  208. return
  209. end
  210. return SkinExcel[skinId].body,SkinExcel[skinId].head
  211. --[[if not human.db.skinBag then
  212. return
  213. end
  214. local bagDB = getBag(human)
  215. if human.heroSkin[heroInd] then
  216. local skinInd = human.heroSkin[heroInd][1]
  217. local skinID = bagDB[skinInd].id
  218. return SkinExcel[skinID].body,SkinExcel[skinID].head
  219. end]]
  220. end
  221. function getHeroSkin(human,heroInd)
  222. local heroId = HeroLogic.getHeroIdByIndex(heroInd)
  223. local heroGrid = HeroLogic.getHeroGrid(human,heroInd,heroId)
  224. local skinId = heroGrid.skinOn
  225. if not skinId then
  226. return
  227. end
  228. return SkillExcel[skinId]
  229. --[=[if not human or not human.db.skinBag then
  230. return
  231. end
  232. local bagDB = getBag(human)
  233. if human.heroSkin[heroInd] then
  234. local skinInd = human.heroSkin[heroInd][1]
  235. local skinID = bagDB[skinInd].id
  236. local skinConf = SkinExcel[skinID]
  237. local skillConf = SkillExcel[human.heroSkin[heroInd][2]]
  238. return skinConf,skillConf
  239. end]=]
  240. end
  241. -------------------------- protocol --------------------------
  242. -- 皮肤信息查询
  243. function skinQuery(human)
  244. local msgRet = Msg.gc.GC_SKIN_BAG
  245. local data = skinListQuery(human)
  246. local maxLen = #msgRet.list -- 每次最多只能传30个
  247. local cnt = 0
  248. for id in pairs(SkinExcel) do
  249. cnt = cnt + 1
  250. msgRet.list[cnt].ind = id
  251. msgRet.list[cnt].isOn = data[id].state
  252. skinNetGen(human,msgRet.list[cnt].data,id)
  253. if cnt >= maxLen then
  254. msgRet.list[0] = maxLen
  255. cnt = 0
  256. msgRet.isEnd = 1
  257. Msg.send(msgRet,human.fd)
  258. end
  259. end
  260. msgRet.list[0] = cnt
  261. msgRet.isEnd = 2
  262. Msg.send(msgRet,human.fd)
  263. end
  264. -- 皮肤穿戴功能
  265. function skinOp(human,id,heroIdx)
  266. if id == 0 then
  267. return skinOff(human,heroIdx)
  268. end
  269. return skinOn(human,id,heroIdx)
  270. end
  271. -- 皮肤解锁
  272. function skinUnlock(human,id)
  273. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  274. -- 皮肤已经解锁
  275. if human.db.skinBag[id] then
  276. -- 发放皮肤重复物品
  277. local itemList = {}
  278. for _,item in pairs(skinCfg.repeated) do
  279. itemList[#itemList+1] = {
  280. item[1],item[2]
  281. }
  282. end
  283. return BagLogic.addItemList(human,itemList,"skin")
  284. end
  285. local now = os.time()
  286. human.db.skinBag[id] = {
  287. ttl = -1,
  288. state = 0,
  289. getTime = now,
  290. }
  291. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, skinCfg.head)
  292. -- 所有角色属性提升
  293. -- 回复给客户端
  294. local msgRet = Msg.gc.GC_SKIN_UPDATE
  295. local data = msgRet.list[1].data
  296. data.ind = id
  297. data.isOn = 0
  298. skinNetGen(human,data.data,id)
  299. msgRet.list[1].op = 1
  300. msgRet.list[0] = 1
  301. Msg.send(msgRet,human.fd)
  302. end
  303. -- 计算皮肤属性
  304. function doCalcSkinHero(human,attrs)
  305. local skinBag = human.db.skinBag
  306. local attrMap = {}
  307. for id in pairs(skinBag) do
  308. local skinCfg = SkinExcel[id]
  309. for _,attr in pairs(skinCfg.attrs) do
  310. attrMap[attr[1]] = attrMap[attr[1]] or 0
  311. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  312. end
  313. end
  314. for attr,cnt in pairs(attrMap) do
  315. RoleAttr.updateValue(attr,cnt, attrs)
  316. end
  317. end