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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. CombatControls = {}
  2. -- private variables
  3. local combatControlsButton
  4. local combatControlsWindow
  5. local fightOffensiveBox
  6. local fightBalancedBox
  7. local fightDefensiveBox
  8. local chaseModeButton
  9. local safeFightButton
  10. local fightModeRadioGroup
  11. -- private functions
  12. local function onSetFightMode(self, selectedFightButton)
  13. if selectedFightButton == nil then return end
  14. local buttonId = selectedFightButton:getId()
  15. local fightMode
  16. if buttonId == 'fightOffensiveBox' then
  17. fightMode = FightOffensive
  18. elseif buttonId == 'fightBalancedBox' then
  19. fightMode = FightBalanced
  20. else
  21. fightMode = FightDefensive
  22. end
  23. g_game.setFightMode(fightMode)
  24. end
  25. local function onSetChaseMode(self, checked)
  26. local chaseMode
  27. if checked then
  28. chaseMode = ChaseOpponent
  29. else
  30. chaseMode = DontChase
  31. end
  32. g_game.setChaseMode(chaseMode)
  33. end
  34. local function onSetSafeFight(self, checked)
  35. g_game.setSafeFight(not checked)
  36. end
  37. -- public functions
  38. function CombatControls.init()
  39. combatControlsButton = TopMenu.addRightGameToggleButton('combatControlsButton', tr('Combat Controls'), 'combatcontrols.png', CombatControls.toggle)
  40. combatControlsButton:setOn(true)
  41. combatControlsWindow = g_ui.loadUI('combatcontrols.otui', GameInterface.getRightPanel())
  42. combatControlsWindow:disableResize()
  43. fightOffensiveBox = combatControlsWindow:recursiveGetChildById('fightOffensiveBox')
  44. fightBalancedBox = combatControlsWindow:recursiveGetChildById('fightBalancedBox')
  45. fightDefensiveBox = combatControlsWindow:recursiveGetChildById('fightDefensiveBox')
  46. chaseModeButton = combatControlsWindow:recursiveGetChildById('chaseModeBox')
  47. safeFightButton = combatControlsWindow:recursiveGetChildById('safeFightBox')
  48. fightModeRadioGroup = UIRadioGroup.create()
  49. fightModeRadioGroup:addWidget(fightOffensiveBox)
  50. fightModeRadioGroup:addWidget(fightBalancedBox)
  51. fightModeRadioGroup:addWidget(fightDefensiveBox)
  52. connect(fightModeRadioGroup, { onSelectionChange = onSetFightMode })
  53. connect(chaseModeButton, { onCheckChange = onSetChaseMode })
  54. connect(safeFightButton, { onCheckChange = onSetSafeFight })
  55. connect(g_game, {
  56. onGameStart = CombatControls.online,
  57. onGameEnd = CombatControls.offline,
  58. onFightModeChange = CombatControls.update,
  59. onChaseModeChange = CombatControls.update,
  60. onSafeFightChange = CombatControls.update,
  61. onWalk = CombatControls.check
  62. })
  63. if g_game.isOnline() then
  64. CombatControls.online()
  65. end
  66. end
  67. function CombatControls.terminate()
  68. if g_game.isOnline() then
  69. CombatControls.offline()
  70. end
  71. fightModeRadioGroup:destroy()
  72. fightModeRadioGroup = nil
  73. fightOffensiveBox = nil
  74. fightBalancedBox = nil
  75. fightDefensiveBox = nil
  76. chaseModeButton = nil
  77. safeFightButton = nil
  78. combatControlsButton:destroy()
  79. combatControlsButton = nil
  80. combatControlsWindow:destroy()
  81. combatControlsWindow = nil
  82. disconnect(g_game, {
  83. onGameStart = CombatControls.online,
  84. onGameEnd = CombatControls.offline,
  85. onFightModeChange = CombatControls.update,
  86. onChaseModeChange = CombatControls.update,
  87. onSafeFightChange = CombatControls.update,
  88. onWalk = CombatControls.check
  89. })
  90. CombatControls = nil
  91. end
  92. function CombatControls.update()
  93. local fightMode = g_game.getFightMode()
  94. if fightMode == FightOffensive then
  95. fightModeRadioGroup:selectWidget(fightOffensiveBox)
  96. elseif fightMode == FightBalanced then
  97. fightModeRadioGroup:selectWidget(fightBalancedBox)
  98. else
  99. fightModeRadioGroup:selectWidget(fightDefensiveBox)
  100. end
  101. local chaseMode = g_game.getChaseMode()
  102. chaseModeButton:setChecked(chaseMode == ChaseOpponent)
  103. local safeFight = g_game.isSafeFight()
  104. safeFightButton:setChecked(not safeFight)
  105. end
  106. function CombatControls.check()
  107. if(Options.getOption('autoChaseOverride')) then
  108. if(g_game.isAttacking() and g_game.getChaseMode() == ChaseOpponent) then
  109. g_game.setChaseMode(DontChase)
  110. end
  111. end
  112. end
  113. function CombatControls.online()
  114. local player = g_game.getLocalPlayer()
  115. if(player) then
  116. local char = player:getName()
  117. local lastCombatControls = g_settings.getNode('LastCombatControls')
  118. if(not table.empty(lastCombatControls)) then
  119. if(lastCombatControls[char]) then
  120. g_game.setFightMode(lastCombatControls[char].fightMode)
  121. g_game.setChaseMode(lastCombatControls[char].chaseMode)
  122. g_game.setSafeFight(lastCombatControls[char].safeFight)
  123. end
  124. end
  125. end
  126. combatControlsWindow:setVisible(combatControlsButton:isOn())
  127. CombatControls.update()
  128. end
  129. function CombatControls.offline()
  130. local lastCombatControls = g_settings.getNode('LastCombatControls')
  131. if(not lastCombatControls) then
  132. lastCombatControls = {}
  133. end
  134. local player = g_game.getLocalPlayer()
  135. if(player) then
  136. local char = player:getName()
  137. lastCombatControls[char] = {
  138. fightMode = g_game.getFightMode(),
  139. chaseMode = g_game.getChaseMode(),
  140. safeFight = g_game.isSafeFight()
  141. }
  142. -- save last combat control settings
  143. g_settings.setNode('LastCombatControls', lastCombatControls)
  144. end
  145. end
  146. function CombatControls.toggle()
  147. if combatControlsButton:isOn() then
  148. combatControlsWindow:close()
  149. combatControlsButton:setOn(false)
  150. else
  151. combatControlsWindow:open()
  152. combatControlsButton:setOn(true)
  153. end
  154. end
  155. function CombatControls.onMiniWindowClose()
  156. combatControlsButton:setOn(false)
  157. end