SkinLogic.lua 12 KB

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