SkinLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 checkHeroSkin(human, heroInd)
  231. if not heroInd then return end
  232. if not human or not human.db or not human.db.heroBag then return end
  233. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  234. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  235. if heroGrid.skinOn then
  236. return heroGrid.skinOn,true
  237. end
  238. --[[if not human.db.skinBag then
  239. return
  240. end
  241. local bagDB = getBag(human)
  242. if human.heroSkin[heroInd] then
  243. local skinInd = human.heroSkin[heroInd][1]
  244. local skinID = bagDB[skinInd].id
  245. --local skillID = human.heroSkin[heroInd][2]
  246. return skinID,true
  247. else
  248. return
  249. end]]
  250. end
  251. -- 检查是否有皮肤body
  252. function getBody(human,heroInd)
  253. if not heroInd then
  254. return
  255. end
  256. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  257. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  258. local skinId = heroGrid.skinOn
  259. if not skinId then
  260. return
  261. end
  262. return SkinExcel[skinId].body,SkinExcel[skinId].head
  263. --[[if not human.db.skinBag then
  264. return
  265. end
  266. local bagDB = getBag(human)
  267. if human.heroSkin[heroInd] then
  268. local skinInd = human.heroSkin[heroInd][1]
  269. local skinID = bagDB[skinInd].id
  270. return SkinExcel[skinID].body,SkinExcel[skinID].head
  271. end]]
  272. end
  273. function getHeroSkin(human,heroInd)
  274. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  275. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  276. local skinId = heroGrid.skinOn
  277. if not skinId then
  278. return
  279. end
  280. return SkillExcel[skinId]
  281. --[=[if not human or not human.db.skinBag then
  282. return
  283. end
  284. local bagDB = getBag(human)
  285. if human.heroSkin[heroInd] then
  286. local skinInd = human.heroSkin[heroInd][1]
  287. local skinID = bagDB[skinInd].id
  288. local skinConf = SkinExcel[skinID]
  289. local skillConf = SkillExcel[human.heroSkin[heroInd][2]]
  290. return skinConf,skillConf
  291. end]=]
  292. end
  293. function getBodyByHeroId(human, heroid)
  294. if not human then return end
  295. local list = HeroLogic.getHeroListById(human, heroid)
  296. if #list > 0 then
  297. local heroIdx = list[1]
  298. return getBody(human, heroIdx)
  299. end
  300. end
  301. local function calcSkinAttr(human)
  302. local skinBag = human.db.skinBag
  303. if not skinBag then
  304. return
  305. end
  306. local attrMap = {}
  307. for i = 1,#skinBag do
  308. local data = skinBag[i]
  309. local skinCfg = SkinExcel[data.id]
  310. for _,attr in pairs(skinCfg.attrs) do
  311. attrMap[attr[1]] = attrMap[attr[1]] or 0
  312. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  313. end
  314. end
  315. return attrMap
  316. end
  317. -------------------------- protocol --------------------------
  318. -- 皮肤信息查询
  319. function skinQuery(human)
  320. local msgRet = Msg.gc.GC_SKIN_BAG
  321. local data = skinListQuery(human)
  322. local cnt = 0
  323. for _,info in pairs(data) do
  324. cnt = cnt + 1
  325. msgRet.data[cnt].ind = info.idx
  326. msgRet.data[cnt].id = info.id
  327. msgRet.data[cnt].isOn = info.state
  328. end
  329. msgRet.data[0] = cnt
  330. local maxLen = #msgRet.list -- 每次最多只能传30个
  331. cnt = 0
  332. for id in pairs(SkinExcel) do
  333. cnt = cnt + 1
  334. skinNetGen(human,msgRet.list[cnt],id)
  335. --[[if cnt >= maxLen then
  336. msgRet.list[0] = maxLen
  337. cnt = 0
  338. msgRet.isEnd = 1
  339. Msg.send(msgRet,human.fd)
  340. end]]
  341. end
  342. msgRet.list[0] = cnt
  343. msgRet.isEnd = 2
  344. Msg.send(msgRet,human.fd)
  345. end
  346. -- 皮肤穿戴功能
  347. function skinOp(human,heroIdx,skinIdx)
  348. if skinIdx == 0 then
  349. skinOff(human,heroIdx)
  350. else
  351. skinOn(human,skinIdx,heroIdx)
  352. end
  353. local retMsg = Msg.gc.GC_SKIN_ON
  354. retMsg.heroId = heroIdx
  355. retMsg.skinInd = skinIdx
  356. Msg.send(retMsg,human.fd)
  357. end
  358. -- 皮肤解锁
  359. function skinUnlock(human,id)
  360. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  361. local isUnlock = false
  362. for i = 1,#human.db.skinBag do
  363. local skin = human.db.skinBag[i]
  364. if skin.id == id then
  365. isUnlock =true
  366. break
  367. end
  368. end
  369. --[[for _,skin in pairs(human.db.skinBag) do
  370. if skin.id == id then
  371. isUnlock =true
  372. break
  373. end
  374. end]]
  375. -- 皮肤已经解锁
  376. if isUnlock then
  377. -- 发放皮肤重复物品
  378. local itemList = {}
  379. for _,item in pairs(skinCfg.repeated) do
  380. itemList[#itemList+1] = {
  381. item[1],item[2]
  382. }
  383. end
  384. return BagLogic.addItemList(human,itemList,"skin")
  385. --Broadcast.sendErr(human, ":skin repeat:"..Lang.SKIN_REPEAT)
  386. --return
  387. end
  388. local now = os.time()
  389. human.db.skinBag[#human.db.skinBag + 1] = {
  390. id = id,
  391. ttl = -1,
  392. state = 0,
  393. getTime = now,
  394. }
  395. -- 解锁头像
  396. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, skinCfg.head)
  397. -- 解锁身体
  398. if skinCfg.body ~= 0 then
  399. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_3, skinCfg.body)
  400. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_5, skinCfg.body)
  401. end
  402. -- 所有角色属性提升
  403. -- 回复给客户端
  404. local msgRet = Msg.gc.GC_SKIN_UPDATE
  405. local data = msgRet.list[1].data
  406. data.ind = #human.db.skinBag
  407. data.isOn = 0
  408. skinNetGen(human,data.data,id)
  409. msgRet.list[1].op = 1
  410. msgRet.list[0] = 1
  411. Msg.send(msgRet,human.fd)
  412. end
  413. -- 计算皮肤属性
  414. function doCalcSkinHero(human,attrs)
  415. local skinBag = human.db.skinBag
  416. local attrMap = {}
  417. for i = 1,#skinBag do
  418. local data = skinBag[i]
  419. local skinCfg = SkinExcel[data.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 id in pairs(skinBag) do
  426. local skinCfg = SkinExcel[id]
  427. for _,attr in pairs(skinCfg.attrs) do
  428. attrMap[attr[1]] = attrMap[attr[1]] or 0
  429. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  430. end
  431. end]=]
  432. for attr,cnt in pairs(attrMap) do
  433. RoleAttr.updateValue(attr,cnt, attrs)
  434. end
  435. end
  436. function onFightBegin(human)
  437. local skinAttrMap = calcSkinAttr(human)
  438. if not skinAttrMap or not next(skinAttrMap) then
  439. return
  440. end
  441. for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
  442. local obj = CombatImpl.objList[pos]
  443. if obj then
  444. for attr,cnt in ipairs(skinAttrMap) do
  445. obj.sysAttr[attr] = obj.sysAttr[attr] or 0
  446. obj.sysAttr[attr] = obj.sysAttr[attr] +cnt
  447. end
  448. obj.isSysAttrChange = true
  449. end
  450. end
  451. end