SkinLogic.lua 10 KB

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