SkinLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 CombatDefine = require("combat.CombatDefine")
  18. local CombatImpl = require("combat.CombatImpl")
  19. local function getBag(human)
  20. if not (human.skinBag and human.heroSkin) then
  21. human.skinBag = {}
  22. human.heroSkin = {}
  23. human.hasSkin = {}
  24. for i = 1,human.db.skinBag[0] do
  25. local data = human.db.skinBag[i]
  26. if data then
  27. if data.heroInd then
  28. local skinSkillID
  29. local heroGrid = human.db.heroBag[data.heroInd]
  30. if not heroGrid then
  31. data.heroInd = nil
  32. else
  33. local heroID = heroGrid.id
  34. local heroConf = HeroExcel[heroID]
  35. for _,v in ipairs(heroConf.skin) do
  36. if v[1] == data.id then
  37. human.heroSkin[data.heroInd] = {i,tonumber(v[2])}
  38. end
  39. end
  40. end
  41. end
  42. human.skinBag[i] = data.id
  43. human.hasSkin[data.id] = human.hasSkin[data.id] or {}
  44. human.hasSkin[data.id][i] = 1
  45. end
  46. end
  47. end
  48. return human.db.skinBag
  49. end
  50. --[[
  51. skin = {
  52. ttl = number -- 有效时长 -1表示永久
  53. state = number -- 0表示未穿戴 1 表示穿戴
  54. } -- skin 拥有唯一性
  55. ]]
  56. -- 获取所有皮肤信息
  57. local function skinListQuery(human)
  58. local skinBag = human.db.skinBag
  59. local ret = {}
  60. for i = 1,#skinBag do
  61. local data = skinBag[i]
  62. ret[data.id] = {
  63. idx = i,
  64. id = data.id,
  65. ttl = data.ttl,
  66. state = data.state,
  67. }
  68. end
  69. return ret
  70. end
  71. -- 获取皮肤为skinId的皮肤
  72. local function skinQueryById(human,skinId)
  73. local skin = human.db.skinBag[skinId]
  74. if skin then
  75. return {
  76. id = skinId,
  77. ttl = skin.ttl,
  78. state = skin.state,
  79. }
  80. end
  81. end
  82. local function skinQueryIdxById(human,skinId)
  83. local skinBag = human.db.skinBag
  84. for i = 1,#skinBag do
  85. local data = skinBag[i]
  86. if data.id == skinId then
  87. return i
  88. end
  89. end
  90. end
  91. -- 生成发送给客户端的皮肤数据
  92. local function skinNetGen(human,net,id)
  93. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  94. net.id = id
  95. net.heroId = skinCfg.heroId or 0
  96. net.name = skinCfg.name
  97. net.desc = skinCfg.desc
  98. net.head = skinCfg.head
  99. net.body = skinCfg.body
  100. net.icon = skinCfg.icon
  101. net.camp = skinCfg.camp
  102. net.order = skinCfg.order
  103. net.type = skinCfg.type
  104. local attrLen = 0
  105. for k,v in ipairs(skinCfg.attrs) do
  106. attrLen = attrLen + 1
  107. net.attrs[attrLen].key = v[1]
  108. net.attrs[attrLen].value = v[2]
  109. end
  110. net.attrs[0] = attrLen
  111. net.heroName = HeroExcel[skinCfg.heroId] and HeroExcel[skinCfg.heroId].name or ""
  112. end
  113. -- 皮肤穿戴
  114. --[[
  115. @param2 = id -- 皮肤Id
  116. @param3 = heroIdx -- 对应英雄存档的idx
  117. ]]
  118. local function skinOn(human,skinIdx,heroIdx)
  119. --local idx = skinQueryIdxById(human,skinIdx)
  120. local skin = human.db.skinBag[skinIdx]
  121. -- 当前skin不存在 或者 皮肤已经穿戴
  122. if not skin or skin.state == 1 then
  123. return Broadcast.sendErr(human, skinIdx..":skinOn:"..Lang.SKIN_PARAM_ERR)
  124. end
  125. local heroId = HeroLogic.getHeroIdByIndex(human,heroIdx)
  126. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroIdx)
  127. -- 英雄不存在 或者 英雄已经装备当前皮肤
  128. if not heroGrid then
  129. return Broadcast.sendErr(human, heroIdx..":heroIdx:"..Lang.SKIN_PARAM_ERR)
  130. end
  131. if heroGrid.skinOn == skin.id then
  132. return Broadcast.sendErr(human, Lang.SKIN_DOUBLE_ON)
  133. end
  134. local skinCfg = SkinExcel[skin.id]
  135. human.db.skinBag[skinIdx].state = 1
  136. -- 对所有heroId相同的英雄操作
  137. local heroIdxList = HeroLogic.getHeroListById(human,heroId)
  138. for _,idx in ipairs(heroIdxList) do
  139. human.db.heroBag[idx].skinOn = skin.id
  140. RoleHeadLogic.setHead(human,skinCfg.head,RoleHeadLogic.HEAD_TYPE_1)
  141. if skinCfg.body ~= 0 then
  142. RoleHeadLogic.setHead(human,skinCfg.body,RoleHeadLogic.HEAD_TYPE_3)
  143. end
  144. end
  145. -- 回复给客户端
  146. local msgRet = Msg.gc.GC_SKIN_UPDATE
  147. local data = msgRet.list[1].data
  148. data.ind = skinIdx
  149. data.isOn = 1
  150. skinNetGen(human,data.data,skin.id)
  151. msgRet.list[1].op = 3
  152. msgRet.list[0] = 1
  153. Msg.send(msgRet,human.fd)
  154. end
  155. -- 皮肤脱下
  156. --[[
  157. @param2 = heroIdx -- 对应英雄存档的idx
  158. ]]
  159. local function skinOff(human,heroIdx)
  160. local heroId = HeroLogic.getHeroIdByIndex(human,heroIdx)
  161. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroIdx)
  162. local skinId = heroGrid.skinOn
  163. -- 英雄不存在 或者 英雄未穿戴皮肤
  164. if not heroGrid or not skinId then
  165. return Broadcast.sendErr(human, heroIdx..":heroIdx:"..Lang.SKIN_PARAM_ERR)
  166. end
  167. local skinIdx = nil
  168. for idx,skin in pairs(human.db.skinBag) do
  169. if idx > 0 and skin.id == skinId then
  170. skinIdx = idx
  171. human.db.skinBag[idx].state = 0
  172. break
  173. end
  174. end
  175. assert(skinIdx,"param error")
  176. -- 对所有heroId相同的英雄操作
  177. local heroIdxList = HeroLogic.getHeroListById(human,heroId)
  178. local heroCfg = HeroExcel[heroId]
  179. for _,idx in ipairs(heroIdxList) do
  180. -- 脱下皮肤 并且还原默认身体 icon和head
  181. human.db.heroBag[idx].skinOn = nil
  182. --local defaultHead = RoleHeadLogic.getDefaultHead(human)
  183. RoleHeadLogic.setHead(human,heroCfg.head,RoleHeadLogic.HEAD_TYPE_1)
  184. RoleHeadLogic.setHead(human,heroCfg.body,RoleHeadLogic.HEAD_TYPE_3)
  185. end
  186. -- 回复给客户端
  187. local msgRet = Msg.gc.GC_SKIN_UPDATE
  188. local data = msgRet.list[1].data
  189. data.ind = skinIdx
  190. data.isOn = 0
  191. skinNetGen(human,data.data,skinId)
  192. msgRet.list[1].op = 3
  193. msgRet.list[0] = 1
  194. Msg.send(msgRet,human.fd)
  195. end
  196. --------------------------------------------------------------
  197. function initAfterHot()
  198. for _,outConf in pairs(OutExcel) do
  199. outConf.totalWeight = 0
  200. for _,v in ipairs(outConf.out) do
  201. outConf.totalWeight = outConf.totalWeight + v[2]
  202. end
  203. end
  204. end
  205. -- 优先使用皮肤的技能,新版本没有默认返回false
  206. function setSkill(human,heroInd,heroConf,obj)
  207. return false
  208. --[[if not human then return end
  209. if not human.db.skinBag then
  210. --假人
  211. return
  212. end
  213. local bagDB = getBag(human)
  214. local skinSkillID = human.heroSkin[heroInd] and human.heroSkin[heroInd][2]
  215. if not skinSkillID then
  216. return
  217. end
  218. local skillConf = SkillExcel[skinSkillID]
  219. Skill.setSkill(obj, heroConfig,skillConf)
  220. BeSkill.setBeSkill(obj,heroConf,skillConf)
  221. return true]]
  222. end
  223. function checkHeroSkin(human, heroInd)
  224. if not heroInd then return end
  225. if not human or not human.db or not human.db.heroBag then return end
  226. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  227. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  228. if heroGrid.skinOn then
  229. return heroGrid.skinOn,true
  230. end
  231. --[[if not human.db.skinBag then
  232. return
  233. end
  234. local bagDB = getBag(human)
  235. if human.heroSkin[heroInd] then
  236. local skinInd = human.heroSkin[heroInd][1]
  237. local skinID = bagDB[skinInd].id
  238. --local skillID = human.heroSkin[heroInd][2]
  239. return skinID,true
  240. else
  241. return
  242. end]]
  243. end
  244. -- 检查是否有皮肤body
  245. function getBody(human,heroInd)
  246. if not heroInd then
  247. return
  248. end
  249. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  250. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  251. local skinId = heroGrid.skinOn
  252. if not skinId then
  253. return
  254. end
  255. return SkinExcel[skinId].body,SkinExcel[skinId].head
  256. --[[if not human.db.skinBag then
  257. return
  258. end
  259. local bagDB = getBag(human)
  260. if human.heroSkin[heroInd] then
  261. local skinInd = human.heroSkin[heroInd][1]
  262. local skinID = bagDB[skinInd].id
  263. return SkinExcel[skinID].body,SkinExcel[skinID].head
  264. end]]
  265. end
  266. function getHeroSkin(human,heroInd)
  267. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  268. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  269. local skinId = heroGrid.skinOn
  270. if not skinId then
  271. return
  272. end
  273. return SkillExcel[skinId]
  274. --[=[if not human or not human.db.skinBag then
  275. return
  276. end
  277. local bagDB = getBag(human)
  278. if human.heroSkin[heroInd] then
  279. local skinInd = human.heroSkin[heroInd][1]
  280. local skinID = bagDB[skinInd].id
  281. local skinConf = SkinExcel[skinID]
  282. local skillConf = SkillExcel[human.heroSkin[heroInd][2]]
  283. return skinConf,skillConf
  284. end]=]
  285. end
  286. function getBodyByHeroId(human, heroid)
  287. if not human then return end
  288. local list = HeroLogic.getHeroListById(human, heroid)
  289. if #list > 0 then
  290. local heroIdx = list[1]
  291. return getBody(human, heroIdx)
  292. end
  293. end
  294. local function calcSkinAttr(human)
  295. local skinBag = human.db.skinBag
  296. if not skinBag then
  297. return
  298. end
  299. local attrMap = {}
  300. for i = 1,#skinBag do
  301. local data = skinBag[i]
  302. local skinCfg = SkinExcel[data.id]
  303. for _,attr in pairs(skinCfg.attrs) do
  304. attrMap[attr[1]] = attrMap[attr[1]] or 0
  305. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  306. end
  307. end
  308. return attrMap
  309. end
  310. -------------------------- protocol --------------------------
  311. -- 皮肤信息查询
  312. function skinQuery(human)
  313. local msgRet = Msg.gc.GC_SKIN_BAG
  314. local data = skinListQuery(human)
  315. local cnt = 0
  316. for _,info in pairs(data) do
  317. cnt = cnt + 1
  318. msgRet.data[cnt].ind = info.idx
  319. msgRet.data[cnt].id = info.id
  320. msgRet.data[cnt].isOn = info.state
  321. end
  322. msgRet.data[0] = cnt
  323. local maxLen = #msgRet.list -- 每次最多只能传30个
  324. cnt = 0
  325. for id in pairs(SkinExcel) do
  326. cnt = cnt + 1
  327. skinNetGen(human,msgRet.list[cnt],id)
  328. --[[if cnt >= maxLen then
  329. msgRet.list[0] = maxLen
  330. cnt = 0
  331. msgRet.isEnd = 1
  332. Msg.send(msgRet,human.fd)
  333. end]]
  334. end
  335. msgRet.list[0] = cnt
  336. msgRet.isEnd = 2
  337. Msg.send(msgRet,human.fd)
  338. end
  339. -- 皮肤穿戴功能
  340. function skinOp(human,heroIdx,skinIdx)
  341. if skinIdx == 0 then
  342. skinOff(human,heroIdx)
  343. else
  344. skinOn(human,skinIdx,heroIdx)
  345. end
  346. local retMsg = Msg.gc.GC_SKIN_ON
  347. retMsg.heroId = heroIdx
  348. retMsg.skinInd = skinIdx
  349. Msg.send(retMsg,human.fd)
  350. end
  351. -- 皮肤解锁
  352. function skinUnlock(human,id)
  353. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  354. local isUnlock = false
  355. for i = 1,#human.db.skinBag do
  356. local skin = human.db.skinBag[i]
  357. if skin.id == id then
  358. isUnlock =true
  359. break
  360. end
  361. end
  362. --[[for _,skin in pairs(human.db.skinBag) do
  363. if skin.id == id then
  364. isUnlock =true
  365. break
  366. end
  367. end]]
  368. -- 皮肤已经解锁
  369. if isUnlock then
  370. -- 发放皮肤重复物品
  371. local itemList = {}
  372. for _,item in pairs(skinCfg.repeated) do
  373. itemList[#itemList+1] = {
  374. item[1],item[2]
  375. }
  376. end
  377. return BagLogic.addItemList(human,itemList,"skin")
  378. --Broadcast.sendErr(human, ":skin repeat:"..Lang.SKIN_REPEAT)
  379. --return
  380. end
  381. local now = os.time()
  382. human.db.skinBag[#human.db.skinBag + 1] = {
  383. id = id,
  384. ttl = -1,
  385. state = 0,
  386. getTime = now,
  387. }
  388. -- 解锁头像
  389. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, skinCfg.head)
  390. -- 解锁身体
  391. if skinCfg.body ~= 0 then
  392. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_3, skinCfg.body)
  393. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_5, skinCfg.body)
  394. end
  395. -- 所有角色属性提升
  396. -- 回复给客户端
  397. local msgRet = Msg.gc.GC_SKIN_UPDATE
  398. local data = msgRet.list[1].data
  399. data.ind = #human.db.skinBag
  400. data.isOn = 0
  401. skinNetGen(human,data.data,id)
  402. msgRet.list[1].op = 1
  403. msgRet.list[0] = 1
  404. Msg.send(msgRet,human.fd)
  405. end
  406. -- 计算皮肤属性
  407. function doCalcSkinHero(human,attrs)
  408. local skinBag = human.db.skinBag
  409. local attrMap = {}
  410. for i = 1,#skinBag do
  411. local data = skinBag[i]
  412. local skinCfg = SkinExcel[data.id]
  413. for _,attr in pairs(skinCfg.attrs) do
  414. attrMap[attr[1]] = attrMap[attr[1]] or 0
  415. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  416. end
  417. end
  418. --[=[for id in pairs(skinBag) do
  419. local skinCfg = SkinExcel[id]
  420. for _,attr in pairs(skinCfg.attrs) do
  421. attrMap[attr[1]] = attrMap[attr[1]] or 0
  422. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  423. end
  424. end]=]
  425. for attr,cnt in pairs(attrMap) do
  426. RoleAttr.updateValue(attr,cnt, attrs)
  427. end
  428. end
  429. function onFightBegin(human)
  430. local skinAttrMap = calcSkinAttr(human)
  431. if not skinAttrMap or not next(skinAttrMap) then
  432. return
  433. end
  434. for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
  435. local obj = CombatImpl.objList[pos]
  436. if obj then
  437. for attr,cnt in ipairs(skinAttrMap) do
  438. obj.sysAttr[attr] = obj.sysAttr[attr] or 0
  439. obj.sysAttr[attr] = obj.sysAttr[attr] +cnt
  440. end
  441. obj.isSysAttrChange = true
  442. end
  443. end
  444. end