SkinLogic.lua 12 KB

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