SkinLogic.lua 18 KB

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