SkinLogic.lua 11 KB

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