No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

combatcontrols.lua 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. combatControlsButton = nil
  2. combatControlsWindow = nil
  3. fightOffensiveBox = nil
  4. fightBalancedBox = nil
  5. fightDefensiveBox = nil
  6. chaseModeButton = nil
  7. safeFightButton = nil
  8. whiteDoveBox = nil
  9. whiteHandBox = nil
  10. yellowHandBox = nil
  11. redFistBox = nil
  12. mountButton = nil
  13. pvpModesPanel = nil
  14. fightModeRadioGroup = nil
  15. pvpModeRadioGroup = nil
  16. function init()
  17. combatControlsButton = modules.client_topmenu.addRightGameToggleButton('combatControlsButton',
  18. tr('Combat Controls'), '/images/topbuttons/combatcontrols', toggle)
  19. combatControlsButton:setOn(true)
  20. combatControlsWindow = g_ui.loadUI('combatcontrols', modules.game_interface.getRightPanel())
  21. combatControlsWindow:disableResize()
  22. fightOffensiveBox = combatControlsWindow:recursiveGetChildById('fightOffensiveBox')
  23. fightBalancedBox = combatControlsWindow:recursiveGetChildById('fightBalancedBox')
  24. fightDefensiveBox = combatControlsWindow:recursiveGetChildById('fightDefensiveBox')
  25. chaseModeButton = combatControlsWindow:recursiveGetChildById('chaseModeBox')
  26. safeFightButton = combatControlsWindow:recursiveGetChildById('safeFightBox')
  27. mountButton = combatControlsWindow:recursiveGetChildById('mountButton')
  28. mountButton.onClick = onMountButtonClick
  29. pvpModesPanel = combatControlsWindow:recursiveGetChildById('pvpModesPanel')
  30. whiteDoveBox = combatControlsWindow:recursiveGetChildById('whiteDoveBox')
  31. whiteHandBox = combatControlsWindow:recursiveGetChildById('whiteHandBox')
  32. yellowHandBox = combatControlsWindow:recursiveGetChildById('yellowHandBox')
  33. redFistBox = combatControlsWindow:recursiveGetChildById('redFistBox')
  34. fightModeRadioGroup = UIRadioGroup.create()
  35. fightModeRadioGroup:addWidget(fightOffensiveBox)
  36. fightModeRadioGroup:addWidget(fightBalancedBox)
  37. fightModeRadioGroup:addWidget(fightDefensiveBox)
  38. pvpModeRadioGroup = UIRadioGroup.create()
  39. pvpModeRadioGroup:addWidget(whiteDoveBox)
  40. pvpModeRadioGroup:addWidget(whiteHandBox)
  41. pvpModeRadioGroup:addWidget(yellowHandBox)
  42. pvpModeRadioGroup:addWidget(redFistBox)
  43. connect(fightModeRadioGroup, { onSelectionChange = onSetFightMode })
  44. connect(pvpModeRadioGroup, { onSelectionChange = onSetPVPMode })
  45. connect(chaseModeButton, { onCheckChange = onSetChaseMode })
  46. connect(safeFightButton, { onCheckChange = onSetSafeFight })
  47. connect(g_game, {
  48. onGameStart = online,
  49. onGameEnd = offline,
  50. onFightModeChange = update,
  51. onChaseModeChange = update,
  52. onSafeFightChange = update,
  53. onPVPModeChange = update,
  54. onWalk = check,
  55. onAutoWalk = check
  56. })
  57. connect(LocalPlayer, { onOutfitChange = onOutfitChange })
  58. if g_game.isOnline() then
  59. online()
  60. end
  61. combatControlsWindow:setup()
  62. end
  63. function terminate()
  64. if g_game.isOnline() then
  65. offline()
  66. end
  67. fightModeRadioGroup:destroy()
  68. pvpModeRadioGroup:destroy()
  69. combatControlsWindow:destroy()
  70. combatControlsButton:destroy()
  71. disconnect(g_game, {
  72. onGameStart = online,
  73. onGameEnd = offline,
  74. onFightModeChange = update,
  75. onChaseModeChange = update,
  76. onSafeFightChange = update,
  77. onPVPModeChange = update,
  78. onWalk = check,
  79. onAutoWalk = check
  80. })
  81. disconnect(LocalPlayer, { onOutfitChange = onOutfitChange })
  82. end
  83. function update()
  84. local fightMode = g_game.getFightMode()
  85. if fightMode == FightOffensive then
  86. fightModeRadioGroup:selectWidget(fightOffensiveBox)
  87. elseif fightMode == FightBalanced then
  88. fightModeRadioGroup:selectWidget(fightBalancedBox)
  89. else
  90. fightModeRadioGroup:selectWidget(fightDefensiveBox)
  91. end
  92. local chaseMode = g_game.getChaseMode()
  93. chaseModeButton:setChecked(chaseMode == ChaseOpponent)
  94. local safeFight = g_game.isSafeFight()
  95. safeFightButton:setChecked(not safeFight)
  96. if g_game.getFeature(GamePVPMode) then
  97. local pvpMode = g_game.getPVPMode()
  98. local pvpWidget = getPVPBoxByMode(pvpMode)
  99. if pvpWidget then
  100. pvpModeRadioGroup:selectWidget(pvpWidget)
  101. end
  102. end
  103. end
  104. function check()
  105. if modules.client_options.getOption('autoChaseOverride') then
  106. if g_game.isAttacking() and g_game.getChaseMode() == ChaseOpponent then
  107. g_game.setChaseMode(DontChase)
  108. end
  109. end
  110. end
  111. function online()
  112. local player = g_game.getLocalPlayer()
  113. if player then
  114. local char = g_game.getCharacterName()
  115. local lastCombatControls = g_settings.getNode('LastCombatControls')
  116. if not table.empty(lastCombatControls) then
  117. if lastCombatControls[char] then
  118. g_game.setFightMode(lastCombatControls[char].fightMode)
  119. g_game.setChaseMode(lastCombatControls[char].chaseMode)
  120. g_game.setSafeFight(lastCombatControls[char].safeFight)
  121. if lastCombatControls[char].pvpMode then
  122. g_game.setPVPMode(lastCombatControls[char].pvpMode)
  123. end
  124. end
  125. end
  126. if g_game.getFeature(GamePlayerMounts) then
  127. mountButton:setVisible(true)
  128. mountButton:setChecked(player:isMounted())
  129. else
  130. mountButton:setVisible(false)
  131. end
  132. if g_game.getFeature(GamePVPMode) then
  133. pvpModesPanel:setVisible(true)
  134. combatControlsWindow:setHeight(combatControlsWindow.extendedControlsHeight)
  135. else
  136. pvpModesPanel:setVisible(false)
  137. combatControlsWindow:setHeight(combatControlsWindow.simpleControlsHeight)
  138. end
  139. end
  140. update()
  141. end
  142. function offline()
  143. local lastCombatControls = g_settings.getNode('LastCombatControls')
  144. if not lastCombatControls then
  145. lastCombatControls = {}
  146. end
  147. local player = g_game.getLocalPlayer()
  148. if player then
  149. local char = g_game.getCharacterName()
  150. lastCombatControls[char] = {
  151. fightMode = g_game.getFightMode(),
  152. chaseMode = g_game.getChaseMode(),
  153. safeFight = g_game.isSafeFight()
  154. }
  155. if g_game.getFeature(GamePVPMode) then
  156. lastCombatControls[char].pvpMode = g_game.getPVPMode()
  157. end
  158. -- save last combat control settings
  159. g_settings.setNode('LastCombatControls', lastCombatControls)
  160. end
  161. end
  162. function toggle()
  163. if combatControlsButton:isOn() then
  164. combatControlsWindow:close()
  165. combatControlsButton:setOn(false)
  166. else
  167. combatControlsWindow:open()
  168. combatControlsButton:setOn(true)
  169. end
  170. end
  171. function onSetFightMode(self, selectedFightButton)
  172. if selectedFightButton == nil then return end
  173. local buttonId = selectedFightButton:getId()
  174. local fightMode
  175. if buttonId == 'fightOffensiveBox' then
  176. fightMode = FightOffensive
  177. elseif buttonId == 'fightBalancedBox' then
  178. fightMode = FightBalanced
  179. else
  180. fightMode = FightDefensive
  181. end
  182. g_game.setFightMode(fightMode)
  183. end
  184. function onSetChaseMode(self, checked)
  185. local chaseMode
  186. if checked then
  187. chaseMode = ChaseOpponent
  188. else
  189. chaseMode = DontChase
  190. end
  191. g_game.setChaseMode(chaseMode)
  192. end
  193. function onSetSafeFight(self, checked)
  194. g_game.setSafeFight(not checked)
  195. end
  196. function onSetPVPMode(self, selectedPVPButton)
  197. if selectedPVPButton == nil then
  198. return
  199. end
  200. local buttonId = selectedPVPButton:getId()
  201. local pvpMode = PVPWhiteDove
  202. if buttonId == 'whiteDoveBox' then
  203. pvpMode = PVPWhiteDove
  204. elseif buttonId == 'whiteHandBox' then
  205. pvpMode = PVPWhiteHand
  206. elseif buttonId == 'yellowHandBox' then
  207. pvpMode = PVPYellowHand
  208. elseif buttonId == 'redFistBox' then
  209. pvpMode = PVPRedFist
  210. end
  211. g_game.setPVPMode(pvpMode)
  212. end
  213. function onMiniWindowClose()
  214. combatControlsButton:setOn(false)
  215. end
  216. function onMountButtonClick(self, mousePos)
  217. local player = g_game.getLocalPlayer()
  218. if player then
  219. player:toggleMount()
  220. end
  221. end
  222. function onOutfitChange(localPlayer, outfit, oldOutfit)
  223. if outfit.mount == oldOutfit.mount then
  224. return
  225. end
  226. mountButton:setChecked(outfit.mount ~= nil and outfit.mount > 0)
  227. end
  228. function getPVPBoxByMode(mode)
  229. local widget = nil
  230. if mode == PVPWhiteDove then
  231. widget = whiteDoveBox
  232. elseif mode == PVPWhiteHand then
  233. widget = whiteHandBox
  234. elseif mode == PVPYellowHand then
  235. widget = yellowHandBox
  236. elseif mode == PVPRedFist then
  237. widget = redFistBox
  238. end
  239. return widget
  240. end