SkinLogic.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 idx > 0 and skin.id == skinId then
  153. skinIdx = idx
  154. human.db.skinBag[idx].state = 0
  155. break
  156. end
  157. end
  158. assert(skinIdx,"param error")
  159. -- 对所有heroId相同的英雄操作
  160. local heroIdxList = HeroLogic.getHeroListById(human,heroId)
  161. for _,idx in ipairs(heroIdxList) do
  162. human.db.heroBag[idx].skinOn = nil
  163. end
  164. -- 回复给客户端
  165. local msgRet = Msg.gc.GC_SKIN_UPDATE
  166. local data = msgRet.list[1].data
  167. data.ind = skinIdx
  168. data.isOn = 0
  169. skinNetGen(human,data.data,skinId)
  170. msgRet.list[1].op = 3
  171. msgRet.list[0] = 1
  172. Msg.send(msgRet,human.fd)
  173. end
  174. --------------------------------------------------------------
  175. function initAfterHot()
  176. for _,outConf in pairs(OutExcel) do
  177. outConf.totalWeight = 0
  178. for _,v in ipairs(outConf.out) do
  179. outConf.totalWeight = outConf.totalWeight + v[2]
  180. end
  181. end
  182. end
  183. -- 优先使用皮肤的技能,新版本没有默认返回false
  184. function setSkill(human,heroInd,heroConf,obj)
  185. return false
  186. --[[if not human then return end
  187. if not human.db.skinBag then
  188. --假人
  189. return
  190. end
  191. local bagDB = getBag(human)
  192. local skinSkillID = human.heroSkin[heroInd] and human.heroSkin[heroInd][2]
  193. if not skinSkillID then
  194. return
  195. end
  196. local skillConf = SkillExcel[skinSkillID]
  197. Skill.setSkill(obj, heroConfig,skillConf)
  198. BeSkill.setBeSkill(obj,heroConf,skillConf)
  199. return true]]
  200. end
  201. function checkHeroSkin(human, heroInd)
  202. if not heroInd then return end
  203. if not human or not human.db or not human.db.heroBag then return end
  204. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  205. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  206. if heroGrid.skinOn then
  207. return heroGrid.skinOn,true
  208. end
  209. --[[if not human.db.skinBag then
  210. return
  211. end
  212. local bagDB = getBag(human)
  213. if human.heroSkin[heroInd] then
  214. local skinInd = human.heroSkin[heroInd][1]
  215. local skinID = bagDB[skinInd].id
  216. --local skillID = human.heroSkin[heroInd][2]
  217. return skinID,true
  218. else
  219. return
  220. end]]
  221. end
  222. -- 检查是否有皮肤body
  223. function getBody(human,heroInd)
  224. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  225. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  226. local skinId = heroGrid.skinOn
  227. if not skinId then
  228. return
  229. end
  230. return SkinExcel[skinId].body,SkinExcel[skinId].head
  231. --[[if not human.db.skinBag then
  232. return
  233. end
  234. local bagDB = getBag(human)
  235. if human.heroSkin[heroInd] then
  236. local skinInd = human.heroSkin[heroInd][1]
  237. local skinID = bagDB[skinInd].id
  238. return SkinExcel[skinID].body,SkinExcel[skinID].head
  239. end]]
  240. end
  241. function getHeroSkin(human,heroInd)
  242. local heroId = HeroLogic.getHeroIdByIndex(human,heroInd)
  243. local heroGrid = HeroLogic.getHeroGrid(human,heroId,heroInd)
  244. local skinId = heroGrid.skinOn
  245. if not skinId then
  246. return
  247. end
  248. return SkillExcel[skinId]
  249. --[=[if not human or not human.db.skinBag then
  250. return
  251. end
  252. local bagDB = getBag(human)
  253. if human.heroSkin[heroInd] then
  254. local skinInd = human.heroSkin[heroInd][1]
  255. local skinID = bagDB[skinInd].id
  256. local skinConf = SkinExcel[skinID]
  257. local skillConf = SkillExcel[human.heroSkin[heroInd][2]]
  258. return skinConf,skillConf
  259. end]=]
  260. end
  261. -------------------------- protocol --------------------------
  262. -- 皮肤信息查询
  263. function skinQuery(human)
  264. local msgRet = Msg.gc.GC_SKIN_BAG
  265. local data = skinListQuery(human)
  266. local cnt = 0
  267. for _,info in pairs(data) do
  268. cnt = cnt + 1
  269. msgRet.data[cnt].ind = info.idx
  270. msgRet.data[cnt].id = info.id
  271. msgRet.data[cnt].isOn = info.state
  272. end
  273. msgRet.data[0] = cnt
  274. local maxLen = #msgRet.list -- 每次最多只能传30个
  275. cnt = 0
  276. for id in pairs(SkinExcel) do
  277. cnt = cnt + 1
  278. skinNetGen(human,msgRet.list[cnt],id)
  279. --[[if cnt >= maxLen then
  280. msgRet.list[0] = maxLen
  281. cnt = 0
  282. msgRet.isEnd = 1
  283. Msg.send(msgRet,human.fd)
  284. end]]
  285. end
  286. msgRet.list[0] = cnt
  287. msgRet.isEnd = 2
  288. Msg.send(msgRet,human.fd)
  289. end
  290. -- 皮肤穿戴功能
  291. function skinOp(human,heroIdx,skinIdx)
  292. if skinIdx == 0 then
  293. skinOff(human,heroIdx)
  294. else
  295. skinOn(human,skinIdx,heroIdx)
  296. end
  297. local retMsg = Msg.gc.GC_SKIN_ON
  298. retMsg.heroId = heroIdx
  299. retMsg.skinInd = skinIdx
  300. Msg.send(retMsg,human.fd)
  301. end
  302. -- 皮肤解锁
  303. function skinUnlock(human,id)
  304. local skinCfg = assert(SkinExcel[id],"invalid skinId is ",id)
  305. local isUnlock = false
  306. for i = 1,#human.db.skinBag do
  307. local skin = human.db.skinBag[i]
  308. if skin.id == id then
  309. isUnlock =true
  310. break
  311. end
  312. end
  313. --[[for _,skin in pairs(human.db.skinBag) do
  314. if skin.id == id then
  315. isUnlock =true
  316. break
  317. end
  318. end]]
  319. -- 皮肤已经解锁
  320. if isUnlock then
  321. -- 发放皮肤重复物品
  322. local itemList = {}
  323. for _,item in pairs(skinCfg.repeated) do
  324. itemList[#itemList+1] = {
  325. item[1],item[2]
  326. }
  327. end
  328. return BagLogic.addItemList(human,itemList,"skin")
  329. end
  330. local now = os.time()
  331. human.db.skinBag[#human.db.skinBag + 1] = {
  332. id = id,
  333. ttl = -1,
  334. state = 0,
  335. getTime = now,
  336. }
  337. RoleHeadLogic.active(human, RoleHeadLogic.HEAD_TYPE_1, skinCfg.head)
  338. -- 所有角色属性提升
  339. -- 回复给客户端
  340. local msgRet = Msg.gc.GC_SKIN_UPDATE
  341. local data = msgRet.list[1].data
  342. data.ind = id
  343. data.isOn = 0
  344. skinNetGen(human,data.data,id)
  345. msgRet.list[1].op = 1
  346. msgRet.list[0] = 1
  347. Msg.send(msgRet,human.fd)
  348. end
  349. -- 计算皮肤属性
  350. function doCalcSkinHero(human,attrs)
  351. local skinBag = human.db.skinBag
  352. local attrMap = {}
  353. for i = 1,#skinBag do
  354. local data = skinBag[i]
  355. local skinCfg = SkinExcel[data.id]
  356. for _,attr in pairs(skinCfg.attrs) do
  357. attrMap[attr[1]] = attrMap[attr[1]] or 0
  358. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  359. end
  360. end
  361. --[=[for id in pairs(skinBag) do
  362. local skinCfg = SkinExcel[id]
  363. for _,attr in pairs(skinCfg.attrs) do
  364. attrMap[attr[1]] = attrMap[attr[1]] or 0
  365. attrMap[attr[1]] = attrMap[attr[1]] + attr[2]
  366. end
  367. end]=]
  368. for attr,cnt in pairs(attrMap) do
  369. RoleAttr.updateValue(attr,cnt, attrs)
  370. end
  371. end