Browse Source

add combat controls

Eduardo Bart 9 years ago
parent
commit
ee1357a848

+ 1
- 0
TODO View File

@@ -61,6 +61,7 @@ change Align/Anchors lua API from enum to text
61 61
 
62 62
 == Client
63 63
 make possible to reload modules
64
+fix modules recursivity, it makes client crash
64 65
 implement left panel with dragging windows
65 66
 clean sprites cache periodically
66 67
 create a shader manager

+ 1
- 0
modules/client_topmenu/topmenu.otui View File

@@ -61,6 +61,7 @@ TopPanel
61 61
       type: horizontalBox
62 62
       spacing: 4
63 63
     padding: 6 4
64
+    visible: false
64 65
 
65 66
   Panel
66 67
     id: rightButtonsPanel

+ 8
- 0
modules/core_lib/const.lua View File

@@ -309,3 +309,11 @@ SpeakChannelRed = 11
309 309
 SpeakChannelOrange = 12
310 310
 SpeakMonsterSay = 13
311 311
 SpeakMonsterYell = 14
312
+
313
+
314
+FightOffensive = 1
315
+FightBalanced = 2
316
+FightDefensive = 3
317
+
318
+DontChase = 0
319
+ChaseOpponent = 1

+ 5
- 0
modules/core_lib/globals.lua View File

@@ -51,6 +51,11 @@ function displayUI(arg1, options)
51 51
   return widget
52 52
 end
53 53
 
54
+function loadUI(otui, parent)
55
+  local otuiFilePath = resolvepath(otui, 2)
56
+  return g_ui.loadUI(otuiFilePath, parent)
57
+end
58
+
54 59
 function createWidget(style, parent)
55 60
   local className = g_ui.getStyleClass(style)
56 61
   if className == "" then

+ 13
- 0
modules/core_lib/util.lua View File

@@ -57,6 +57,19 @@ function disconnect(object, signalsAndSlots)
57 57
   end
58 58
 end
59 59
 
60
+function newclass()
61
+  local class = {}
62
+  function class.internalCreate()
63
+    local instance = {}
64
+    for k,v in pairs(class) do
65
+      instance[k] = v
66
+    end
67
+    return instance
68
+  end
69
+  class.create = class.internalCreate
70
+  return class
71
+end
72
+
60 73
 function extends(base)
61 74
   local derived = {}
62 75
   function derived.internalCreate()

+ 2
- 0
modules/core_widgets/core_widgets.otmod View File

@@ -25,6 +25,8 @@ Module
25 25
     dofile 'uimessagebox'
26 26
 
27 27
     dofile 'tooltip'
28
+    dofile 'radiogroup'
29
+
28 30
     ToolTip.init()
29 31
 
30 32
   onUnload: |

+ 43
- 0
modules/core_widgets/radiogroup.lua View File

@@ -0,0 +1,43 @@
1
+RadioGroup = newclass()
2
+
3
+function RadioGroup.create()
4
+  local radiogroup = RadioGroup.internalCreate()
5
+  radiogroup.widgets = {}
6
+  return radiogroup
7
+end
8
+
9
+function RadioGroup:destroy()
10
+  while #self.widgets ~= 0 do
11
+    self:removeWidget(self.widgets[1])
12
+  end
13
+end
14
+
15
+function RadioGroup:addWidget(widget)
16
+  table.insert(self.widgets, widget)
17
+  widget.onMousePress = function(widget) self:selectWidget(widget) end
18
+end
19
+
20
+function RadioGroup:removeWidget(widget)
21
+  if self.selectedWidget == widget then
22
+    self:selectWidget(nil)
23
+  end
24
+  widget.onMousePress = nil
25
+  table.removevalue(self.widgets, widget)
26
+end
27
+
28
+function RadioGroup:selectWidget(selectedWidget)
29
+  if selectedWidget == self.selectedWidget then return end
30
+
31
+  local previousSelectedWidget = self.selectedWidget
32
+  self.selectedWidget = selectedWidget
33
+
34
+  if previousSelectedWidget then
35
+    previousSelectedWidget:setChecked(false)
36
+  end
37
+
38
+  if selectedWidget then
39
+    selectedWidget:setChecked(true)
40
+  end
41
+
42
+  signalcall(self.onSelectionChange, self, selectedWidget, previousSelectedWidget)
43
+end

+ 2
- 0
modules/core_widgets/tooltip.lua View File

@@ -57,6 +57,8 @@ function ToolTip.terminate()
57 57
   currentHoveredWidget = nil
58 58
   toolTipLabel:destroy()
59 59
   toolTipLabel = nil
60
+
61
+  ToolTip = nil
60 62
 end
61 63
 
62 64
 function ToolTip.display(text)

+ 3
- 5
modules/core_widgets/uicheckbox.lua View File

@@ -7,9 +7,7 @@ function UICheckBox.create()
7 7
   return checkbox
8 8
 end
9 9
 
10
-function UICheckBox:onMouseRelease(mousePos, mouseButton)
11
-  if self:isPressed() and self:containsPoint(mousePos) then
12
-    self:setChecked(not self:isChecked())
13
-    return true
14
-  end
10
+function UICheckBox:onMousePress(mousePos, mouseButton)
11
+  self:setChecked(not self:isChecked())
12
+  return true
15 13
 end

+ 120
- 1
modules/game_combatcontrols/combatcontrols.lua View File

@@ -1,13 +1,132 @@
1 1
 CombatControls = {}
2 2
 
3
+-- private variables
3 4
 local combatControlsButton
5
+local combatControlsWindow
6
+local fightOffensiveBox
7
+local fightBalancedBox
8
+local fightDefensiveBox
9
+local chaseModeButton
10
+local safeFightButton
11
+local fightModeRadioGroup
4 12
 
13
+-- private functions
14
+local function onFightModeChange(self, selectedFightButton)
15
+  if selectedFightButton == nil then return end
16
+  local buttonId = selectedFightButton:getId()
17
+  local fightMode
18
+  if buttonId == 'fightOffensiveBox' then
19
+    fightMode = FightOffensive
20
+  elseif buttonId == 'fightBalancedBox' then
21
+    fightMode = FightBalanced
22
+  else
23
+    fightMode = FightDefensive
24
+  end
25
+  if Game.getFightMode ~= fightMode then
26
+    Game.setFightMode(fightMode)
27
+  end
28
+end
29
+
30
+local function onChaseModeChange(self, checked)
31
+  local chaseMode
32
+  if checked then
33
+    chaseMode = ChaseOpponent
34
+  else
35
+    chaseMode = DontChase
36
+  end
37
+  if Game.getChaseMode() ~= chaseMode then
38
+    Game.setChaseMode(chaseMode)
39
+  end
40
+end
41
+
42
+local function onSafeFightChange(self, checked)
43
+  local safeFight = not checked
44
+  if Game.isSafeFight() ~= safeFight then
45
+    Game.setSafeFight(not checked)
46
+  end
47
+end
48
+
49
+-- public functions
5 50
 function CombatControls.init()
6 51
   combatControlsButton = TopMenu.addGameButton('combatControlsButton', 'Combat Controls', 'combatcontrols.png', CombatControls.toggle)
7
-end
52
+  combatControlsButton:setOn(true)
53
+  combatControlsWindow = loadUI('combatcontrols.otui')
8 54
 
55
+  fightOffensiveBox = combatControlsWindow:getChildById('fightOffensiveBox')
56
+  fightBalancedBox = combatControlsWindow:getChildById('fightBalancedBox')
57
+  fightDefensiveBox = combatControlsWindow:getChildById('fightDefensiveBox')
58
+  chaseModeButton = combatControlsWindow:getChildById('chaseModeBox')
59
+  safeFightButton = combatControlsWindow:getChildById('safeFightBox')
60
+
61
+  fightModeRadioGroup = RadioGroup.create()
62
+  fightModeRadioGroup:addWidget(fightOffensiveBox)
63
+  fightModeRadioGroup:addWidget(fightBalancedBox)
64
+  fightModeRadioGroup:addWidget(fightDefensiveBox)
65
+
66
+  connect(fightModeRadioGroup, { onSelectionChange = onFightModeChange })
67
+  connect(chaseModeButton, { onCheckChange = onChaseModeChange })
68
+  connect(safeFightButton, { onCheckChange = onSafeFightChange })
69
+  connect(Game, { onLogin = CombatControls.online })
70
+  connect(Game, { onLogout = CombatControls.offline })
71
+
72
+  if Game.isOnline() then
73
+    CombatControls.online()
74
+  end
75
+end
9 76
 
10 77
 function CombatControls.terminate()
78
+  if Game.isOnline() then
79
+    CombatControls.offline()
80
+  end
81
+
82
+  fightModeRadioGroup:destroy()
83
+  fightModeRadioGroup = nil
84
+
85
+  fightOffensiveBox = nil
86
+  fightBalancedBox = nil
87
+  fightDefensiveBox = nil
88
+  chaseModeButton = nil
89
+  safeFightButton = nil
90
+
11 91
   combatControlsButton:destroy()
12 92
   combatControlsButton = nil
93
+
94
+  combatControlsWindow:destroy()
95
+  combatControlsWindow = nil
96
+
97
+  disconnect(Game, { onLogin = CombatControls.online })
98
+  disconnect(Game, { onLogout = CombatControls.offline })
99
+
100
+  CombatControls = nil
13 101
 end
102
+
103
+function CombatControls.online()
104
+  Game.gameRightPanel:addChild(combatControlsWindow)
105
+  combatControlsWindow:setVisible(combatControlsButton:isOn())
106
+
107
+  local fightMode = Game.getFightMode()
108
+  if fightMode == FightOffensive then
109
+    fightModeRadioGroup:selectWidget(fightOffensiveBox)
110
+  elseif fightMode == FightBalanced then
111
+    fightModeRadioGroup:selectWidget(fightBalancedBox)
112
+  else
113
+    fightModeRadioGroup:selectWidget(fightDefensiveBox)
114
+  end
115
+
116
+  local chaseMode = Game.getChaseMode()
117
+  chaseModeButton:setChecked(chaseMode == ChaseOpponent)
118
+
119
+  local safeFight = Game.isSafeFight()
120
+  safeFightButton:setChecked(not safeFight)
121
+end
122
+
123
+function CombatControls.offline()
124
+  Game.gameRightPanel:removeChild(combatControlsWindow)
125
+end
126
+
127
+function CombatControls.toggle()
128
+  local visible = not combatControlsWindow:isExplicitlyVisible()
129
+  combatControlsWindow:setVisible(visible)
130
+  combatControlsButton:setOn(visible)
131
+end
132
+

+ 43
- 0
modules/game_combatcontrols/combatcontrols.otui View File

@@ -0,0 +1,43 @@
1
+CombatBox < UICheckBox
2
+  size: 20 20
3
+  image-clip: 0 0 20 20
4
+  anchors.top: parent.top
5
+  margin-left: 5
6
+  margin-right: 5
7
+
8
+  $checked:
9
+    image-clip: 0 20 20 20
10
+
11
+FightOffensiveBox < CombatBox
12
+  image-source: /game_combatcontrols/icons/fightoffensive.png
13
+FightBalancedBox < CombatBox
14
+  image-source: /game_combatcontrols/icons/fightbalanced.png
15
+FightDefensiveBox < CombatBox
16
+  image-source: /game_combatcontrols/icons/fightdefensive.png
17
+ChaseModeBox < CombatBox
18
+  image-source: /game_combatcontrols/icons/chasemode.png
19
+SafeFightBox < CombatBox
20
+  image-source: /game_combatcontrols/icons/safefight.png
21
+
22
+UIWindow
23
+  width: 130
24
+  height: 30
25
+  margin-top: 10
26
+  margin-left: 6
27
+  margin-right: 6
28
+
29
+  FightOffensiveBox
30
+    id: fightOffensiveBox
31
+    anchors.right: next.left
32
+  FightBalancedBox
33
+    id: fightBalancedBox
34
+    anchors.right: next.left
35
+  FightDefensiveBox
36
+    id: fightDefensiveBox
37
+    anchors.horizontalCenter: parent.horizontalCenter
38
+  ChaseModeBox
39
+    id: chaseModeBox
40
+    anchors.left: prev.right
41
+  SafeFightBox
42
+    id: safeFightBox
43
+    anchors.left: prev.right

BIN
modules/game_combatcontrols/icons/chasemode.png View File


BIN
modules/game_combatcontrols/icons/fightbalanced.png View File


BIN
modules/game_combatcontrols/icons/fightdefensive.png View File


BIN
modules/game_combatcontrols/icons/fightoffensive.png View File


BIN
modules/game_combatcontrols/icons/safefight.png View File


+ 0
- 1
modules/game_inventory/inventory.otui View File

@@ -4,7 +4,6 @@ UIWindow
4 4
   margin-top: 10
5 5
   margin-left: 6
6 6
   margin-right: 6
7
-  move-policy: free updated
8 7
 
9 8
   Item
10 9
     // head

+ 1
- 1
src/otclient/const.h View File

@@ -211,7 +211,7 @@ namespace Otc
211 211
     };
212 212
 
213 213
     enum ChaseModes {
214
-        StandWhileFighting = 0,
214
+        DontChase = 0,
215 215
         ChaseOpponent = 1
216 216
     };
217 217
 

+ 33
- 0
src/otclient/core/game.cpp View File

@@ -77,6 +77,12 @@ void Game::processLogin(const LocalPlayerPtr& localPlayer, int serverBeat)
77 77
     m_localPlayer = localPlayer;
78 78
     m_serverBeat = serverBeat;
79 79
 
80
+    // synchronize fight modes with the server
81
+    m_fightMode = Otc::FightBalanced;
82
+    m_chaseMode = Otc::DontChase;
83
+    m_safeFight = true;
84
+    m_protocolGame->sendFightTatics(m_fightMode, m_chaseMode, m_safeFight);
85
+
80 86
     // NOTE: the entire map description is not known yet
81 87
     g_lua.callGlobalField("Game", "onLogin", localPlayer);
82 88
 }
@@ -384,6 +390,33 @@ void Game::move(const ThingPtr& thing, const Position& toPos, int count)
384 390
     m_protocolGame->sendThrow(thing->getPosition(), thing->getId(), thing->getStackpos(), toPos, count);
385 391
 }
386 392
 
393
+void Game::setChaseMode(Otc::ChaseModes chaseMode)
394
+{
395
+    if(!isOnline() || !checkBotProtection())
396
+        return;
397
+
398
+    m_chaseMode = chaseMode;
399
+    m_protocolGame->sendFightTatics(m_fightMode, m_chaseMode, m_safeFight);
400
+}
401
+
402
+void Game::setFightMode(Otc::FightModes fightMode)
403
+{
404
+    if(!isOnline() || !checkBotProtection())
405
+        return;
406
+
407
+    m_fightMode = fightMode;
408
+    m_protocolGame->sendFightTatics(m_fightMode, m_chaseMode, m_safeFight);
409
+}
410
+
411
+void Game::setSafeFight(bool on)
412
+{
413
+    if(!isOnline() || !checkBotProtection())
414
+        return;
415
+
416
+    m_safeFight = on;
417
+    m_protocolGame->sendFightTatics(m_fightMode, m_chaseMode, m_safeFight);
418
+}
419
+
387 420
 void Game::attack(const CreaturePtr& creature)
388 421
 {
389 422
     if(!isOnline() || !creature || !checkBotProtection())

+ 12
- 0
src/otclient/core/game.h View File

@@ -74,6 +74,14 @@ public:
74 74
     void useInventoryItemWith(int itemId, const ThingPtr& toThing);
75 75
     void move(const ThingPtr &thing, const Position& toPos, int count);
76 76
 
77
+    // fight tatics related
78
+    void setChaseMode(Otc::ChaseModes chaseMode);
79
+    void setFightMode(Otc::FightModes fightMode);
80
+    void setSafeFight(bool on);
81
+    Otc::ChaseModes getChaseMode() { return m_chaseMode; }
82
+    Otc::FightModes getFightMode() { return m_fightMode; }
83
+    bool isSafeFight() { return m_safeFight; }
84
+
77 85
     // attack/follow related
78 86
     void attack(const CreaturePtr& creature);
79 87
     void cancelAttack();
@@ -124,6 +132,10 @@ private:
124 132
     ProtocolGamePtr m_protocolGame;
125 133
     bool m_dead;
126 134
     int m_serverBeat;
135
+
136
+    Otc::FightModes m_fightMode;
137
+    Otc::ChaseModes m_chaseMode;
138
+    bool m_safeFight;
127 139
 };
128 140
 
129 141
 extern Game g_game;

+ 6
- 0
src/otclient/luafunctions.cpp View File

@@ -204,6 +204,12 @@ void OTClient::registerLuaFunctions()
204 204
     g_lua.bindClassStaticFunction<Game>("turn", std::bind(&Game::turn, &g_game, _1));
205 205
     g_lua.bindClassStaticFunction<Game>("walk", std::bind(&Game::walk, &g_game, _1));
206 206
     g_lua.bindClassStaticFunction<Game>("forceWalk", std::bind(&Game::forceWalk, &g_game, _1));
207
+    g_lua.bindClassStaticFunction<Game>("setChaseMode", std::bind(&Game::setChaseMode, &g_game, _1));
208
+    g_lua.bindClassStaticFunction<Game>("setFightMode", std::bind(&Game::setFightMode, &g_game, _1));
209
+    g_lua.bindClassStaticFunction<Game>("setSafeFight", std::bind(&Game::setSafeFight, &g_game, _1));
210
+    g_lua.bindClassStaticFunction<Game>("getChaseMode", std::bind(&Game::getChaseMode, &g_game));
211
+    g_lua.bindClassStaticFunction<Game>("getFightMode", std::bind(&Game::getFightMode, &g_game));
212
+    g_lua.bindClassStaticFunction<Game>("isSafeFight", std::bind(&Game::isSafeFight, &g_game));
207 213
     g_lua.bindClassStaticFunction<Game>("attack", std::bind(&Game::attack, &g_game, _1));
208 214
     g_lua.bindClassStaticFunction<Game>("cancelAttack", std::bind(&Game::cancelAttack, &g_game));
209 215
     g_lua.bindClassStaticFunction<Game>("follow", std::bind(&Game::follow, &g_game, _1));

Loading…
Cancel
Save