Mount.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. Mount = {}
  2. local this = {}
  3. MOUNT_LIMIT_TIME = 60 * 60
  4. TIME_LIMIT_MOUNT = "T$timeLimitMount"
  5. -- 发送当前骑乘坐骑信息
  6. function Mount.sendCurrentRideMount(actor)
  7. local curRideMountId = getrolefield(actor,"role.rolemount.curridemountid")
  8. sendluamsg(actor, LuaMessageIdToClient.RES_CURRENT_RIDE_MOUNT, curRideMountId)
  9. end
  10. -- 坐骑穿戴装备
  11. function Mount.putOnItem(actor, msgData)
  12. local mountEquip = ConfigDataManager.getTableValue("cfg_mount","mountEquip","id",msgData[1])
  13. if mountEquip == "0" then
  14. noticeTip.noticeinfo(actor, StringIdConst.TEXT359)
  15. end
  16. local putOnResult = mountputitem(actor, msgData[1],msgData[2],msgData[3])
  17. local result = {}
  18. result["result"] = putOnResult
  19. result["pos"] = msgData[3]
  20. result["itemcfgid"] = msgData[4]
  21. result["itemid"] = msgData[5]
  22. sendluamsg(actor, LuaMessageIdToClient.RES_MOUNT_PUT_ON_ITEM, result)
  23. Mount.mountDetail(actor,msgData)
  24. Mount.sendCurrentRideMount(actor)
  25. end
  26. -- 坐骑脱下装备
  27. function Mount.takeOffItem(actor, msgData)
  28. local takeOffResult = mountoffitem(actor, msgData[1],msgData[2])
  29. local result = {}
  30. result["result"] = takeOffResult
  31. result["pos"] = msgData[2]
  32. sendluamsg(actor, LuaMessageIdToClient.RES_MOUNT_TAKE_OFF_ITEM, result)
  33. Mount.mountDetail(actor,msgData)
  34. Mount.sendCurrentRideMount(actor)
  35. end
  36. -- 穿戴坐骑信息 需要穿戴坐骑背包索引
  37. function Mount.mountBagToStore(actor, msgData)
  38. local itemcfgid = msgData[2]
  39. local mountConfig = ConfigDataManager.getTable("cfg_mount","itemId",itemcfgid)
  40. if table.isEmpty(mountConfig) then
  41. error("坐骑配置信息中没有itemId为:" .. itemcfgid.."的配置项")
  42. return
  43. end
  44. local mountInfo = mountConfig[1]
  45. jprint("mountInfo",mountInfo)
  46. local limitedtype = mountInfo.limitedtype
  47. if limitedtype and limitedtype == "1" then
  48. -- 限时坐骑
  49. jprint("限时坐骑")
  50. local allTimeLimitMount = getplaydef(actor, TIME_LIMIT_MOUNT)
  51. local timeLimitMount = {}
  52. if table.isEmpty(allTimeLimitMount) then
  53. allTimeLimitMount = {}
  54. timeLimitMount = {
  55. time = getbaseinfo(actor,"nowsec"),
  56. itemCfgId = tonumber(mountInfo.icon),
  57. id = tonumber(mountInfo.id),
  58. endTime = getbaseinfo(actor,"nowsec") + tonumber(mountInfo.limitedtime) * MOUNT_LIMIT_TIME,
  59. }
  60. else
  61. local have = false
  62. for index, timeLimitMount in pairs(allTimeLimitMount) do
  63. if timeLimitMount.itemCfgId == tonumber(mountInfo.icon) then
  64. have = true
  65. end
  66. end
  67. if have then
  68. tipinfo(actor,"已经激活过该坐骑")
  69. return
  70. end
  71. timeLimitMount = {
  72. time = getbaseinfo(actor,"now"),
  73. itemCfgId = tonumber(mountInfo.icon),
  74. id = tonumber(mountInfo.id),
  75. endTime = getbaseinfo(actor,"nowsec") + tonumber(mountInfo.limitedtime) * MOUNT_LIMIT_TIME
  76. }
  77. end
  78. table.insert(allTimeLimitMount,timeLimitMount)
  79. setplaydef(actor, TIME_LIMIT_MOUNT,allTimeLimitMount)
  80. end
  81. local putOnResult = putonmount(actor, msgData[1])
  82. local result = {}
  83. result["result"] = putOnResult
  84. result["mountid"] = msgData
  85. sendluamsg(actor, LuaMessageIdToClient.RES_MOUNT_BAG_TO_STORE, result)
  86. Mount.sendCurrentRideMount(actor)
  87. Mount.setMountLogo(actor)
  88. end
  89. -- 设置坐骑标志
  90. function Mount.setMountLogo(actor)
  91. local mountLogo = getplaydef(actor,"T$mountlogo")
  92. if not mountLogo then
  93. local mountLogo = {}
  94. mountLogo["rank"] = 0
  95. end
  96. mountLogo["logo"] = 1
  97. setplaydef(actor,"T$mountlogo",mountLogo)
  98. Mount.sendMountLogo(actor)
  99. end
  100. -- 设置坐骑排行
  101. function Mount.setMountRank(actor,msgData)
  102. local mountLogo = getplaydef(actor,"T$mountlogo")
  103. mountLogo["rank"] = msgData
  104. setplaydef(actor,"T$mountlogo",mountLogo)
  105. Mount.sendMountLogo(actor)
  106. end
  107. -- 发送坐骑默认骑乘信息和排行信息
  108. function Mount.sendMountLogo(actor)
  109. local mountLogo = getplaydef(actor,"T$mountlogo")
  110. if not mountLogo then
  111. mountLogo = {}
  112. mountLogo["logo"] = 0
  113. mountLogo["rank"] = 0
  114. setplaydef(actor,"T$mountlogo",mountLogo)
  115. end
  116. sendluamsg(actor,LuaMessageIdToClient.RES_MOUNT_LOGO,mountLogo)
  117. end
  118. -- 发送其他人坐骑默认骑乘信息和排行信息
  119. function Mount.sendOtherMountRank(actor,otherRid)
  120. local otherActor = nil
  121. if actor:toString() == tostring(otherRid) then
  122. otherActor = actor
  123. elseif actor:toString() ~= tostring(otherRid) then
  124. otherActor = getactor(actor,otherRid)
  125. end
  126. local mountLogo = getplaydef(otherActor,"T$mountlogo")
  127. local rank = nil
  128. if not mountLogo then
  129. rank = 0
  130. mountLogo = {}
  131. mountLogo["logo"] = 0
  132. mountLogo["rank"] = 0
  133. setplaydef(otherActor,"T$mountlogo",mountLogo)
  134. elseif mountLogo then
  135. rank = mountLogo["rank"]
  136. end
  137. sendluamsg(actor,LuaMessageIdToClient.RES_OTHER_MOUNT_RANK,rank)
  138. end
  139. -- 检查坐骑详细信息
  140. function Mount.checkMountTime(actor)
  141. local allTimeLimitMount = getplaydef(actor, TIME_LIMIT_MOUNT)
  142. if table.isEmpty(allTimeLimitMount) then
  143. return
  144. end
  145. local time = getbaseinfo(actor,"nowsec")
  146. local newAllTimeLimitMount = {}
  147. for index, timeLimitMount in pairs(allTimeLimitMount) do
  148. if time > timeLimitMount.endTime then
  149. local msgData = {}
  150. msgData[1] = timeLimitMount.id
  151. msgData[2] = 1
  152. Mount.mountStoreToBag(actor, msgData)
  153. else
  154. table.insert(newAllTimeLimitMount,timeLimitMount)
  155. end
  156. end
  157. setplaydef(actor, TIME_LIMIT_MOUNT,newAllTimeLimitMount)
  158. end
  159. -- 脱下坐骑信息 需要脱下坐骑id
  160. function Mount.mountStoreToBag(actor, msgData)
  161. local takeOffResult = takeoffmount(actor, msgData[1],msgData[2])
  162. local result = {}
  163. result["result"] = takeOffResult
  164. result["mountid"] = msgData
  165. sendluamsg(actor, LuaMessageIdToClient.RES_MOUNT_STORE_TO_BAG, result)
  166. local mountLogo = getplaydef(actor,"T$mountlogo")
  167. if mountLogo then
  168. local rank = mountLogo["rank"]
  169. if rank and rank == msgData then
  170. mountLogo["rank"] = 0
  171. setplaydef(actor,"T$mountlogo",mountLogo)
  172. Mount.sendMountLogo(actor)
  173. end
  174. end
  175. local allMount = getallmount(actor)
  176. Mount.allMountInfo(actor)
  177. Mount.allAttInfo(actor, allMount)
  178. Mount.sendCurrentRideMount(actor)
  179. end
  180. -- 骑乘坐骑 需要骑乘坐骑id
  181. function Mount.ridingMount(actor, msgData)
  182. if not msgData or not msgData[1] then
  183. local curWearMountId = getrolefield(actor,"role.rolemount.curwearmountid")
  184. if curWearMountId == 0 then
  185. noticeTip.noticeinfo(actor, StringIdConst.TEXT360)
  186. return
  187. end
  188. msgData = {0}
  189. -- error(msgData)
  190. end
  191. local rideResult = ridemount(actor, msgData[1])
  192. local result = {}
  193. result["mountid"] = rideResult
  194. sendluamsg(actor, LuaMessageIdToClient.RES_RIDING_MOUNT, result)
  195. -- 骑乘后属性发生改变
  196. local allMount = getallmount(actor)
  197. Mount.allAttInfo(actor, allMount)
  198. Mount.sendCurrentRideMount(actor)
  199. end
  200. -- 取消骑乘坐骑 可以什么都不传,也可以穿当前骑乘坐骑id
  201. function Mount.cancelRidingMount(actor, msgData)
  202. local cancelRideResult = cancelridemount(actor, msgData[1])
  203. local result = {}
  204. result["result"] = cancelRideResult
  205. result["mountid"] = msgData[1]
  206. sendluamsg(actor, LuaMessageIdToClient.RES_CANCEL_RIDING_MOUNT, result)
  207. -- 取消骑乘后属性发生改变
  208. local allMount = getallmount(actor)
  209. Mount.allAttInfo(actor, allMount)
  210. Mount.sendCurrentRideMount(actor)
  211. end
  212. -- 获取坐骑信息
  213. function Mount.allMountInfo(actor)
  214. local allMount = getallmount(actor)
  215. if table.isEmpty(allMount) then
  216. -- 没有坐骑,发送空的坐骑信息
  217. jprint(actor,"没有坐骑,发送空的坐骑信息")
  218. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_MOUNT_INFO, allMount)
  219. Mount.allAttInfo(actor, allMount)
  220. return
  221. end
  222. local allTimeLimitMount = getplaydef(actor, TIME_LIMIT_MOUNT)
  223. jprint("allTimeLimitMount",allTimeLimitMount)
  224. if table.isEmpty(allTimeLimitMount) then
  225. -- 没有限时坐骑,直接发送坐骑信息即可
  226. jprint(actor,"没有限时坐骑,直接发送坐骑信息即可")
  227. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_MOUNT_INFO, allMount)
  228. Mount.allAttInfo(actor, allMount)
  229. return
  230. end
  231. for index, timeLimitMount in pairs(allTimeLimitMount) do
  232. for index, mountInfo in pairs(allMount) do
  233. if timeLimitMount.id == tonumber(mountInfo.mountcfgid) then
  234. mountInfo.time = timeLimitMount.endTime
  235. jprint("设置成功",mountInfo)
  236. end
  237. end
  238. end
  239. jprint("全部坐骑信息",allMount)
  240. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_MOUNT_INFO, allMount)
  241. Mount.allAttInfo(actor, allMount)
  242. end
  243. -- 获取坐骑信息
  244. function Mount.mountDetail(actor,msgData)
  245. local mount = mountdetailinfo(actor,msgData[1])
  246. local mountAtt = Mount.mountAttInfo(actor, mount,1)
  247. local result = {}
  248. result["item"] = mount["itematt"]
  249. result["att"] = mountAtt
  250. sendluamsg(actor,LuaMessageIdToClient.RES_MOUNT_DETAIL_INFO,result)
  251. end
  252. -- 设置坐骑显示与隐藏
  253. function Mount.mountShow(actor,msgData)
  254. local curRideMountId = getrolefield(actor,"role.rolemount.curridemountid")
  255. if curRideMountId == 0 then
  256. noticeTip.noticeinfo(actor, StringIdConst.TEXT361)
  257. return
  258. end
  259. mountisshow(actor,msgData)
  260. end
  261. -- 计算单个坐骑的属性
  262. function Mount.mountAttInfo(actor, mount,mountDetail)
  263. local mountAtt = {}
  264. local mountCfgId = mount["mountcfgid"]
  265. local wear = mount["wear"]
  266. if tonumber(wear) == 1 or mountDetail == 1 then
  267. local speed = ConfigDataManager.getTableValue("cfg_mount", "speed", "id", mountCfgId)
  268. Mount.mergemountatt(mountAtt, "318022", tonumber(speed))
  269. end
  270. local mountAttString = ConfigDataManager.getTableValue("cfg_mount", "attribute", "id", mountCfgId)
  271. local mountAttS = string.split(mountAttString, "|")
  272. for _, attInfo in pairs(mountAttS) do
  273. local attDetail = string.split(attInfo, "#")
  274. Mount.mergemountatt(mountAtt, attDetail[1], tonumber(attDetail[2]))
  275. end
  276. local mountEquip = ConfigDataManager.getTableValue("cfg_mount", "mountEquip", "id", mountCfgId)
  277. local itematt = mount["itematt"]
  278. if itematt and tonumber(mountEquip) == 1 then
  279. for index, itemInfo in pairs(itematt) do
  280. local itemDetailAtt = itemInfo["att"]
  281. for attCfgId, attValue in pairs(itemDetailAtt) do
  282. Mount.mergemountatt(mountAtt, attCfgId, tonumber(attValue))
  283. end
  284. end
  285. end
  286. return mountAtt
  287. end
  288. -- 计算坐骑全部属性
  289. function Mount.allAttInfo(actor, allMount)
  290. local allMountAttInfo = {}
  291. if allMount then
  292. for index, mount in pairs(allMount) do
  293. local mountAtt = Mount.mountAttInfo(actor,mount,0)
  294. if mountAtt then
  295. Mount.totalMountAtt(allMountAttInfo, mountAtt)
  296. end
  297. end
  298. end
  299. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_MOUNT_ATT_INFO, allMountAttInfo)
  300. end
  301. function Mount.totalMountAtt(allMountAttInfo, mountAtt)
  302. for attCfgId, attValue in pairs(mountAtt) do
  303. Mount.mergemountatt(allMountAttInfo, attCfgId, attValue)
  304. end
  305. end
  306. function Mount.mergemountatt(allAtt, attCfgId, attValue)
  307. local num = allAtt[attCfgId]
  308. if num == 0 or num == nil then
  309. allAtt[attCfgId] = tonumber(attValue)
  310. else
  311. allAtt[attCfgId] = num + tonumber(attValue)
  312. end
  313. end