SkinLogic.lua 18 KB

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