SkinLogic.lua 11 KB

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