AppearInfo.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. ---@class AppearInfo
  2. AppearInfo = class()
  3. local this = AppearInfo
  4. ---@type table<EEquipSlotType,number>
  5. local serverAppear = {}
  6. ---变身戒指缓存,在变身卡牌结束后再切回变身戒指变身
  7. ---@type table<number,number>
  8. local cacheMonsterId = {}
  9. local checkPos = {
  10. EEquipSlotType.Helmet,
  11. EEquipSlotType.Armor,
  12. EEquipSlotType.Boot,
  13. EEquipSlotType.Pant,
  14. EEquipSlotType.Glove,
  15. EEquipSlotType.FashionTotalArmor,
  16. EEquipSlotType.Banner,
  17. }
  18. ---@class EquipSlot2AngleSlot
  19. local EquipSlot2AngleSlot = {
  20. [EEquipSlotType.ArchangelWeapon] = EEquipSlotType.Weapon,
  21. [EEquipSlotType.Weapon] = EEquipSlotType.ArchangelWeapon,
  22. [EEquipSlotType.ArchangelOffHandWeapon] = EEquipSlotType.OffHandWeapon,
  23. [EEquipSlotType.OffHandWeapon] = EEquipSlotType.ArchangelOffHandWeapon,
  24. [EEquipSlotType.ArchangelTwoHandWeapon] = EEquipSlotType.TwoHandWeapon,
  25. [EEquipSlotType.TwoHandWeapon] = EEquipSlotType.ArchangelTwoHandWeapon,
  26. [EEquipSlotType.ArchangelHelmet] = EEquipSlotType.Helmet,
  27. [EEquipSlotType.Helmet] = EEquipSlotType.ArchangelHelmet,
  28. [EEquipSlotType.ArchangelArmor] = EEquipSlotType.Armor,
  29. [EEquipSlotType.Armor] = EEquipSlotType.ArchangelArmor,
  30. [EEquipSlotType.ArchangelGlove] = EEquipSlotType.Glove,
  31. [EEquipSlotType.Glove] = EEquipSlotType.ArchangelGlove,
  32. [EEquipSlotType.ArchangelPant] = EEquipSlotType.Pant,
  33. [EEquipSlotType.Pant] = EEquipSlotType.ArchangelPant,
  34. [EEquipSlotType.ArchangelBoot] = EEquipSlotType.Boot,
  35. [EEquipSlotType.Boot] = EEquipSlotType.ArchangelBoot,
  36. }
  37. ---@class EquipSlot2FashionSlot
  38. local EquipSlot2FashionSlot = {
  39. [EEquipSlotType.FashionHelmet] = EEquipSlotType.Helmet,
  40. [EEquipSlotType.Helmet] = EEquipSlotType.FashionHelmet,
  41. [EEquipSlotType.FashionArmor] = EEquipSlotType.Armor,
  42. [EEquipSlotType.Armor] = EEquipSlotType.FashionArmor,
  43. [EEquipSlotType.FashionWeapon] = EEquipSlotType.Weapon,
  44. [EEquipSlotType.Weapon] = EEquipSlotType.FashionWeapon,
  45. [EEquipSlotType.FashionTwoHandWeapon] = EEquipSlotType.TwoHandWeapon,
  46. [EEquipSlotType.TwoHandWeapon] = EEquipSlotType.FashionTwoHandWeapon,
  47. [EEquipSlotType.FashionOffHandWeapon] = EEquipSlotType.OffHandWeapon,
  48. [EEquipSlotType.OffHandWeapon] = EEquipSlotType.FashionOffHandWeapon,
  49. [EEquipSlotType.FashionGlove] = EEquipSlotType.Glove,
  50. [EEquipSlotType.Glove] = EEquipSlotType.FashionGlove,
  51. [EEquipSlotType.FashionPant] = EEquipSlotType.Pant,
  52. [EEquipSlotType.Pant] = EEquipSlotType.FashionPant,
  53. [EEquipSlotType.FashionBoot] = EEquipSlotType.Boot,
  54. [EEquipSlotType.Boot] = EEquipSlotType.FashionBoot,
  55. [EEquipSlotType.FashionWing] = EEquipSlotType.Wing,
  56. [EEquipSlotType.Wing] = EEquipSlotType.FashionWing,
  57. [EEquipSlotType.Guard] = EEquipSlotType.FashionGuard,
  58. [EEquipSlotType.FashionGuard] = EEquipSlotType.Guard,
  59. [EEquipSlotType.LeftArtifact] = EEquipSlotType.LeftArtifact,
  60. [EEquipSlotType.RightArtifact] = EEquipSlotType.RightArtifact,
  61. [EEquipSlotType.Banner] = EEquipSlotType.Banner,
  62. }
  63. ---@type number
  64. local curTitleId
  65. ---@type number
  66. local curMonsterId
  67. ---@param message table<EEquipSlotType,number>
  68. local function OnEquipAppearSaveSucc(_, message)
  69. if not message then
  70. return
  71. end
  72. table.clear(serverAppear)
  73. for slot, cfgId in pairs(message) do
  74. slot = tonumber(slot)
  75. serverAppear[slot] = cfgId
  76. end
  77. local meId = SL:GetMetaValue("UID")
  78. SL:RefreshAppear(meId, serverAppear)
  79. end
  80. ---@param message {rid:number,appear:{slot:number,cfgId:number}[],ring:number,title:number}
  81. local function OnOtherRoleAppearChange(_, message)
  82. ---@type table<EEquipSlotType,number>
  83. local dt = {}
  84. if message.appear then
  85. for _, kp in pairs(message.appear) do
  86. local slot = tonumber(kp.slot)
  87. dt[slot] = kp.cfgId
  88. end
  89. end
  90. SL:RefreshAppear(message.rid, dt)
  91. local monsterId
  92. local ringId = tonumber(message.ring)
  93. if ringId and ringId > 0 then
  94. ---@type cfg_item_column
  95. local itemTbl = SL:GetConfig("cfg_item", ringId)
  96. monsterId = tonumber(itemTbl.useParam)
  97. end
  98. cacheMonsterId[message.rid] = monsterId
  99. if SL:GetMetaValue("ACTOR_IS_MAINPLAYER", message.rid) then
  100. ---变身卡牌优先级更高
  101. if InfoManager.shapeShiftCardInfo:isShiftMonster(message.rid) then
  102. monsterId = InfoManager.shapeShiftCardInfo:GetMonsterModelId()
  103. end
  104. end
  105. SL:RefreshToShowMonster(message.rid, monsterId)
  106. local titleId = tonumber(message.title)
  107. SL:RefreshTitle(message.rid, titleId)
  108. end
  109. local function GetEquipItem(pos, cfgId)
  110. ---@type CommonProtos.Item[]
  111. local allEquips = SL:GetMetaValue("EQUIP_DATA_LIST", pos)
  112. for _, equipItem in pairs(allEquips) do
  113. if equipItem.cfgId == cfgId then
  114. return equipItem
  115. end
  116. end
  117. end
  118. ---@param equip CommonProtos.Item
  119. local function GetGrade(equip)
  120. return (equip and equip.itemExtInfo and equip.itemExtInfo.equipExtInfo and equip.itemExtInfo.equipExtInfo.grade) or 0
  121. end
  122. -- 穿装后处理,返回哪些装备要显示到外观上
  123. -- 时装部位显示高于装备部位(cfg_item/strPart)
  124. -- 道具品阶越高显示优先级越高(cfg_equip_quality/id)
  125. -- 品阶相同则道具id越低显示优先级越高(cfg_item/id)
  126. ---@param equip CommonProtos.Item
  127. ---@return table<EEquipSlotType,number>
  128. local function PutEquipAndGetAppearIdx(putIndex, equip)
  129. local pos = putIndex & 255
  130. local isFashion = pos > 100
  131. local isAngle = pos >= 71 and pos <= 78
  132. ---@type table<EEquipSlotType,number>
  133. local appearIdx = table.copy(serverAppear)
  134. local cfgId = equip.cfgId
  135. ---@type cfg_item_column
  136. local itemCfg = SL:GetConfig("cfg_item", cfgId)
  137. if not itemCfg then
  138. return serverAppear
  139. end
  140. --全身时装需要替换所有部件,头盔,护甲,鞋子,裤子, 护手,包括已经装的全身时装
  141. if pos == EEquipSlotType.FashionTotalArmor and isFashion then
  142. for _, slot in pairs(checkPos) do
  143. appearIdx[slot] = nil
  144. local rp = EquipSlot2FashionSlot[slot]
  145. if rp then
  146. appearIdx[rp] = nil
  147. end
  148. end
  149. appearIdx[pos] = cfgId
  150. return appearIdx
  151. end
  152. if isFashion then
  153. local normalSlot = EquipSlot2FashionSlot[pos]
  154. --穿戴的是fashion 普通去掉 ,如果有大天使,使用大天使(如果有fashion,同时将fashion去除 )
  155. appearIdx[normalSlot] = nil
  156. local angelSlot = EquipSlot2AngleSlot[normalSlot]
  157. if appearIdx[angelSlot] then
  158. appearIdx[pos] = nil
  159. return appearIdx
  160. end
  161. elseif isAngle then
  162. --穿戴大天使 普通去掉,fashion去掉
  163. local normalSlot = EquipSlot2AngleSlot[pos]
  164. appearIdx[normalSlot] = nil
  165. local fashionSlot = EquipSlot2FashionSlot[normalSlot]
  166. appearIdx[fashionSlot] = nil
  167. else
  168. local angelSlot = EquipSlot2AngleSlot[pos]
  169. if angelSlot and appearIdx[angelSlot] then
  170. --穿戴的是普通 有大天使 使用大天使,同时如果有fashion,将fashion去除
  171. local fashionSlot = EquipSlot2FashionSlot[pos]
  172. if fashionSlot then
  173. appearIdx[fashionSlot] = nil
  174. end
  175. return appearIdx
  176. end
  177. local fashionSlot = EquipSlot2FashionSlot[pos]
  178. if fashionSlot and appearIdx[fashionSlot] then
  179. --穿戴的是普通 没有大天使 同时如果有fashion,使用fashion
  180. return appearIdx
  181. end
  182. end
  183. local grade = GetGrade(equip)
  184. if appearIdx[pos] then
  185. ---@type CommonProtos.Item
  186. local useItem = GetEquipItem(pos, appearIdx[pos])
  187. local useGrade = GetGrade(useItem)
  188. if grade > useGrade or (grade == useGrade and cfgId < useItem.cfgId) then
  189. appearIdx[pos] = cfgId
  190. end
  191. return appearIdx
  192. end
  193. appearIdx[pos] = cfgId
  194. return appearIdx
  195. end
  196. ---@return CommonProtos.Item
  197. local function GetHightestGradeEquip(pos)
  198. ---@type CommonProtos.Item[]
  199. local allEquips = SL:GetMetaValue("EQUIP_DATA_LIST", pos)
  200. local hItem
  201. local grade = -1
  202. local cfgId = 0
  203. for _, equipItem in pairs(allEquips) do
  204. local tempGrade = GetGrade(equipItem)
  205. if tempGrade > grade or (tempGrade == grade and equipItem.cfgId < cfgId) then
  206. hItem = equipItem
  207. grade = tempGrade
  208. cfgId = equipItem.cfgId
  209. end
  210. end
  211. return hItem
  212. end
  213. ---@param appearIdx table<EEquipSlotType,number>
  214. ---@param slot EEquipSlotType
  215. local function InsertToAppear(appearIdx, slot)
  216. ---@type CommonProtos.Item
  217. local equipItem = GetHightestGradeEquip(slot)
  218. if equipItem then
  219. appearIdx[slot] = equipItem.cfgId
  220. end
  221. end
  222. -- 脱装后处理,返回哪些装备要显示到外观上
  223. -- 脱下当前外观装备时,外观选中所穿戴的高阶装备
  224. -- 没有其他装备则为裸模
  225. -- 脱下全身时装时, 按规则显示对应的5个防具
  226. -- 脱下主手,副手时装时,找普通装备的最高阶
  227. -- 脱下普通装备, 不找时装, 只找该pos下的最高阶
  228. ---@param equip CommonProtos.Item
  229. ---@return table<EEquipSlotType,number>
  230. local function TakeOffEquipAndGetAppearIdx(takeOffIndex, equip)
  231. local pos = takeOffIndex & 255
  232. ---@type table<EEquipSlotType,number>
  233. local appearIdx = table.copy(serverAppear)
  234. if not appearIdx[pos] or appearIdx[pos] ~= equip.cfgId then
  235. return appearIdx
  236. end
  237. if appearIdx[pos] and appearIdx[pos] == equip.cfgId then
  238. appearIdx[pos] = nil
  239. end
  240. if pos == EEquipSlotType.FashionTotalArmor then
  241. InsertToAppear(appearIdx, EEquipSlotType.Helmet)
  242. InsertToAppear(appearIdx, EEquipSlotType.Armor)
  243. InsertToAppear(appearIdx, EEquipSlotType.Boot)
  244. InsertToAppear(appearIdx, EEquipSlotType.Glove)
  245. InsertToAppear(appearIdx, EEquipSlotType.Pant)
  246. return appearIdx
  247. end
  248. local isFashion = pos > 100
  249. local isAngle = pos >= 71 and pos <= 78
  250. if isFashion then
  251. local normalSlot = EquipSlot2FashionSlot[pos]
  252. --脱下的是fashion ,如果有大天使,使用大天使,没有用普通
  253. local angelSlot = EquipSlot2AngleSlot[normalSlot]
  254. if appearIdx[angelSlot] then
  255. appearIdx[pos] = nil
  256. appearIdx[normalSlot] = nil
  257. return appearIdx
  258. end
  259. InsertToAppear(appearIdx, angelSlot)
  260. if appearIdx[angelSlot] then
  261. appearIdx[pos] = nil
  262. appearIdx[normalSlot] = nil
  263. return appearIdx
  264. end
  265. InsertToAppear(appearIdx, normalSlot)
  266. return appearIdx
  267. elseif isAngle then
  268. --脱下大天使 如果有 fashion使用fashion,没有fashion使用普通的
  269. local normalSlot = EquipSlot2AngleSlot[pos]
  270. local fashionSlot = EquipSlot2FashionSlot[normalSlot]
  271. InsertToAppear(appearIdx, pos)
  272. if appearIdx[pos] then
  273. appearIdx[fashionSlot] = nil
  274. appearIdx[normalSlot] = nil
  275. return appearIdx
  276. end
  277. if appearIdx[fashionSlot] then
  278. appearIdx[normalSlot] = nil
  279. return appearIdx
  280. end
  281. InsertToAppear(appearIdx, fashionSlot)
  282. if appearIdx[fashionSlot] then
  283. appearIdx[normalSlot] = nil
  284. return appearIdx
  285. end
  286. InsertToAppear(appearIdx, normalSlot)
  287. return appearIdx
  288. end
  289. InsertToAppear(appearIdx, pos)
  290. return appearIdx
  291. end
  292. ---@param message EquipProto.EquipIndex
  293. local function OnEquipTakeOn(_, message)
  294. ---@type table<EEquipSlotType,number>
  295. local roleAppear = PutEquipAndGetAppearIdx(message.index, message.equip)
  296. ---@type {slot:number,cfgId:number}[]
  297. local dt = {}
  298. for slot, cfgId in pairs(roleAppear) do
  299. table.insert(dt, { slot = slot, cfgId = cfgId })
  300. --为了解决新创建弓箭手第一次进入场景中,再同一帧会连续给角色穿戴一个箭袋一个弓箭所导致外观保存发出的第一个请求还没有回包,serverAppear数据没有修改,第二个装备保存覆盖第一个导致弓箭不显示,这样处理是不严谨的
  301. serverAppear[slot] = cfgId
  302. end
  303. SL:SendLuaNetMsg(LuaMessageIdToSever.SETTING_EQUIP_APPEAR, { equipindex = dt })
  304. end
  305. ---@param message EquipProto.EquipIndex
  306. local function OnEquipTakeOff(_, message)
  307. ---@type table<EEquipSlotType,number>
  308. local roleAppear = TakeOffEquipAndGetAppearIdx(message.index, message.equip)
  309. ---@type {slot:number,cfgId:number}[]
  310. local dt = {}
  311. for slot, cfgId in pairs(roleAppear) do
  312. table.insert(dt, { slot = slot, cfgId = cfgId })
  313. end
  314. SL:SendLuaNetMsg(LuaMessageIdToSever.SETTING_EQUIP_APPEAR, { equipindex = dt })
  315. end
  316. local function OnRingSaveSucc(_, message)
  317. local ring = tonumber(message)
  318. curMonsterId = nil
  319. if ring and ring > 0 then
  320. ---@type cfg_item_column
  321. local itemTbl = SL:GetConfig("cfg_item", ring)
  322. curMonsterId = tonumber(itemTbl.useParam)
  323. end
  324. --local meId = SL:GetMetaValue("UID")
  325. local meId = SL:GetMetaValue(EMetaVarGetKey.UID)
  326. cacheMonsterId[meId] = curMonsterId
  327. SL:RefreshToShowMonster(meId, curMonsterId)
  328. end
  329. local function OnTitleSaveSucc(_, titleId)
  330. local meId = SL:GetMetaValue("UID")
  331. curTitleId = titleId
  332. SL:RefreshTitle(meId, titleId)
  333. end
  334. ---@param message {title:TitleData[],curEquipTitle:number,hidden:boolean}
  335. local function OnGetTitle(_, message)
  336. curTitleId = tonumber(message.curEquipTitle)
  337. end
  338. ---@param message {curWearRing:number,title:RingData[]}
  339. local function OnGetRing(_, message)
  340. curMonsterId = nil
  341. local ring = tonumber(message.curWearRing)
  342. if ring and ring > 0 then
  343. ---@type cfg_item_column
  344. local itemTbl = SL:GetConfig("cfg_item", ring)
  345. curMonsterId = tonumber(itemTbl.useParam)
  346. end
  347. end
  348. --返回外观
  349. ---@param message {slot:number,cfgId:number}[]
  350. local function OnGetAppear(_, message)
  351. table.clear(serverAppear)
  352. for _, kp in pairs(message) do
  353. local slot = tonumber(kp.slot)
  354. serverAppear[slot] = kp.cfgId
  355. end
  356. end
  357. ---@param message {rid:number,curEquipTitle:number}
  358. local function OnTitleChange(_, message)
  359. if not message or not message.rid then
  360. return
  361. end
  362. SL:RefreshTitle(message.rid, message.curEquipTitle)
  363. end
  364. ---@param message {rid:number,curEquipRing:number}
  365. local function OnRingChange(_, message)
  366. if not message or not message.rid then
  367. return
  368. end
  369. local ring = tonumber(message.curEquipRing)
  370. curMonsterId = nil
  371. if ring and ring > 0 then
  372. ---@type cfg_item_column
  373. local itemTbl = SL:GetConfig("cfg_item", ring)
  374. curMonsterId = tonumber(itemTbl.useParam)
  375. end
  376. cacheMonsterId[message.rid] = curMonsterId
  377. local monsterId = curMonsterId
  378. ---变身卡牌优先级更高
  379. if InfoManager.shapeShiftCardInfo:isShiftMonster(message.rid) then
  380. monsterId = InfoManager.shapeShiftCardInfo:GetMonsterModelId()
  381. end
  382. SL:RefreshToShowMonster(message.rid, monsterId)
  383. end
  384. local function OnPlayerEnterMap()
  385. --进入地图需要请求数据
  386. SL:SendLuaNetMsg(LuaMessageIdToSever.GET_EQUIP_APPEAR)
  387. SL:SendLuaNetMsg(LuaMessageIdToSever.GET_TITLE)
  388. SL:SendLuaNetMsg(LuaMessageIdToSever.GET_SHAPE_RING)
  389. end
  390. local function RegistNetMsgs()
  391. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_SETTING_EQUIP_APPEAR, OnEquipAppearSaveSucc)
  392. SL:RegisterLuaNetMsg(LuaMessageIdToClient.OTHER_ROLE_APPEAR, OnOtherRoleAppearChange)
  393. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_TITLE_CHANGE_UPDATE, OnTitleChange)
  394. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_RING_CHANGE_UPDATE, OnRingChange)
  395. SL:RegisterLuaNetMsg(LuaMessageIdToClient.RES_PUTOON_RING, OnRingSaveSucc)
  396. SL:RegisterLuaNetMsg(LuaMessageIdToClient.SETTING_TITLE_APPEAR, OnTitleSaveSucc)
  397. SL:RegisterLuaNetMsg(LuaMessageIdToClient.GET_TITLE, OnGetTitle)
  398. SL:RegisterLuaNetMsg(LuaMessageIdToClient.GET_SHAPE_RING, OnGetRing)
  399. SL:RegisterLuaNetMsg(LuaMessageIdToClient.GET_EQUIP_APPEAR, OnGetAppear)
  400. SL:RegisterLUAEvent(LUA_EVENT_TAKE_ON_EQUIP, OnEquipTakeOn)
  401. SL:RegisterLUAEvent(LUA_EVENT_TAKE_OFF_EQUIP, OnEquipTakeOff)
  402. SL:RegisterLUAEvent(LUA_EVENT_PLAYER_ENTER_MAP, OnPlayerEnterMap)
  403. end
  404. local function Init()
  405. RegistNetMsgs()
  406. end
  407. local function Reset()
  408. table.clear(serverAppear)
  409. table.clear(cacheMonsterId)
  410. ---@type number
  411. curTitleId = nil
  412. ---@type number
  413. curMonsterId = nil
  414. end
  415. ---==对外==============================================================================================================
  416. this.Init = Init
  417. this.Reset = Reset
  418. this.GetCurAppearList = function()
  419. return serverAppear
  420. end
  421. this.GetCurTitleId = function()
  422. return curTitleId
  423. end
  424. this.GetCurMonsterId = function()
  425. return curMonsterId
  426. end
  427. this.GetCacheMeMonsterId = function(rid)
  428. return cacheMonsterId[rid]
  429. end