3
3
using TaleWorlds . Core ;
4
4
using TaleWorlds . MountAndBlade ;
5
5
6
- namespace ArmyArrowCounter
7
- {
8
- class ArrowCounter
9
- {
6
+ namespace ArmyArrowCounter {
7
+ class ArrowCounter {
10
8
public event Action < int > RemainingArrowsUpdateEvent ;
11
9
public event Action < int > MaxArrowsUpdateEvent ;
12
10
public int RemainingArrows { get ; private set ; }
@@ -15,8 +13,7 @@ class ArrowCounter
15
13
private readonly AacMissionBehavior AacMissionBehavior ;
16
14
private readonly Dictionary < int , short > AgentHashCodeToCurrentArrows = new Dictionary < int , short > ( ) ;
17
15
18
- public ArrowCounter ( AacMissionBehavior aacMissionBehavior )
19
- {
16
+ public ArrowCounter ( AacMissionBehavior aacMissionBehavior ) {
20
17
AacMissionBehavior = aacMissionBehavior ;
21
18
aacMissionBehavior . SiegeBattleStartEvent += OnSiegeBattleStart ;
22
19
aacMissionBehavior . HideoutBattleStartEvent += OnHideoutBattleStart ;
@@ -27,125 +24,102 @@ public ArrowCounter(AacMissionBehavior aacMissionBehavior)
27
24
aacMissionBehavior . OnAllyPickedUpAmmoEvent += OnAllyPickedUpAmmo ;
28
25
}
29
26
30
- private void OnSiegeBattleStart ( )
31
- {
27
+ private void OnSiegeBattleStart ( ) {
32
28
CountAllAlliedAgents ( ) ;
33
29
}
34
30
35
- private void OnHideoutBattleStart ( )
36
- {
31
+ private void OnHideoutBattleStart ( ) {
37
32
CountAllAlliedAgents ( ) ;
38
33
}
39
34
40
- private void OnPlayerBuilt ( )
41
- {
35
+ private void OnPlayerBuilt ( ) {
42
36
CountAllAlliedAgents ( ) ;
43
37
}
44
38
45
- private void OnAllyAgentBuilt ( Agent agent )
46
- {
39
+ private void OnAllyAgentBuilt ( Agent agent ) {
47
40
AddAgent ( agent ) ;
48
41
}
49
42
50
- private void OnAllyAgentRemoved ( Agent agent )
51
- {
43
+ private void OnAllyAgentRemoved ( Agent agent ) {
52
44
RemoveAgent ( agent ) ;
53
45
}
54
46
55
- private void OnAllyFiredMissile ( Agent agent )
56
- {
57
- if ( AgentHashCodeToCurrentArrows . ContainsKey ( agent . GetHashCode ( ) ) )
58
- {
47
+ private void OnAllyFiredMissile ( Agent agent ) {
48
+ if ( AgentHashCodeToCurrentArrows . ContainsKey ( agent . GetHashCode ( ) ) ) {
59
49
AgentHashCodeToCurrentArrows [ agent . GetHashCode ( ) ] -- ;
60
50
}
61
51
AddToRemainingArrows ( - 1 ) ;
62
52
}
63
53
64
- private void OnAllyPickedUpAmmo ( Agent agent , SpawnedItemEntity item )
65
- {
66
- if ( ! AgentHashCodeToCurrentArrows . ContainsKey ( agent . GetHashCode ( ) ) )
67
- {
54
+ private void OnAllyPickedUpAmmo ( Agent agent , SpawnedItemEntity item ) {
55
+ if ( ! AgentHashCodeToCurrentArrows . ContainsKey ( agent . GetHashCode ( ) ) ) {
68
56
return ;
69
57
}
70
58
71
59
short lastKnownAmmoOnAgent = AgentHashCodeToCurrentArrows [ agent . GetHashCode ( ) ] ;
72
60
short newAmmoOnAgent = CalculateRemainingAmmo ( agent ) ;
73
- short amountPickedUp = ( short ) ( newAmmoOnAgent - lastKnownAmmoOnAgent ) ;
61
+ short amountPickedUp = ( short ) ( newAmmoOnAgent - lastKnownAmmoOnAgent ) ;
74
62
AgentHashCodeToCurrentArrows [ agent . GetHashCode ( ) ] += amountPickedUp ;
75
63
AddToRemainingArrows ( amountPickedUp ) ;
76
64
}
77
65
78
- internal void CountAllAlliedAgents ( bool countRemainingArrows = false )
79
- {
66
+ internal void CountAllAlliedAgents ( bool countRemainingArrows = false ) {
80
67
foreach ( Agent agent in AacMissionBehavior . Mission . Agents ) // todo: can instead maybe get player's MBTeam an iterate through friendly agents directly
81
68
{
82
- if ( Utils . IsPlayerAlly ( agent , AacMissionBehavior . PlayerAgent ) )
83
- {
69
+ if ( Utils . IsPlayerAlly ( agent , AacMissionBehavior . PlayerAgent ) ) {
84
70
AddAgent ( agent , countRemainingArrows ) ;
85
71
}
86
72
}
87
73
}
88
74
89
- internal void ForgetState ( )
90
- {
75
+ internal void ForgetState ( ) {
91
76
AgentHashCodeToCurrentArrows . Clear ( ) ;
92
77
AddToRemainingArrows ( - RemainingArrows ) ;
93
78
AddToMaxArrows ( - MaxArrows ) ;
94
79
}
95
80
96
- internal void RecountAllAlliedAgents ( )
97
- {
81
+ internal void RecountAllAlliedAgents ( ) {
98
82
ForgetState ( ) ;
99
83
CountAllAlliedAgents ( true ) ;
100
84
}
101
85
102
- internal void AddToRemainingArrows ( int deltaRemainingArrows )
103
- {
86
+ internal void AddToRemainingArrows ( int deltaRemainingArrows ) {
104
87
RemainingArrows += deltaRemainingArrows ;
105
88
RemainingArrowsUpdateEvent ? . Invoke ( RemainingArrows ) ;
106
89
}
107
90
108
- internal void AddToMaxArrows ( int deltaMaxArrows )
109
- {
91
+ internal void AddToMaxArrows ( int deltaMaxArrows ) {
110
92
MaxArrows += deltaMaxArrows ;
111
93
MaxArrowsUpdateEvent ? . Invoke ( MaxArrows ) ;
112
94
}
113
95
114
- internal void AddAgent ( Agent agent , bool countRemaining = false )
115
- {
96
+ internal void AddAgent ( Agent agent , bool countRemaining = false ) {
116
97
int agentHashCode = agent . GetHashCode ( ) ;
117
- if ( AgentHashCodeToCurrentArrows . ContainsKey ( agentHashCode ) )
118
- {
98
+ if ( AgentHashCodeToCurrentArrows . ContainsKey ( agentHashCode ) ) {
119
99
return ;
120
100
}
121
101
122
- if ( agent . Equipment == null )
123
- {
102
+ if ( agent . Equipment == null ) {
124
103
return ;
125
104
}
126
105
127
106
short maxAmmo = CalculateMaxAmmo ( agent ) ;
128
107
AddToMaxArrows ( maxAmmo ) ;
129
108
130
109
short remainingAmmo ;
131
- if ( countRemaining )
132
- {
110
+ if ( countRemaining ) {
133
111
remainingAmmo = CalculateRemainingAmmo ( agent ) ;
134
- }
135
- else
136
- {
112
+ } else {
137
113
remainingAmmo = maxAmmo ;
138
114
}
139
115
140
116
AgentHashCodeToCurrentArrows . Add ( agentHashCode , remainingAmmo ) ;
141
117
AddToRemainingArrows ( remainingAmmo ) ;
142
118
}
143
119
144
- internal void RemoveAgent ( Agent agent )
145
- {
120
+ internal void RemoveAgent ( Agent agent ) {
146
121
int agentHashCode = agent . GetHashCode ( ) ;
147
- if ( ! AgentHashCodeToCurrentArrows . ContainsKey ( agentHashCode ) )
148
- {
122
+ if ( ! AgentHashCodeToCurrentArrows . ContainsKey ( agentHashCode ) ) {
149
123
return ;
150
124
}
151
125
@@ -156,30 +130,28 @@ internal void RemoveAgent(Agent agent)
156
130
AddToMaxArrows ( - maxAmmo ) ;
157
131
}
158
132
159
- private static short CalculateRemainingAmmo ( Agent agent )
160
- {
133
+ private static short CalculateRemainingAmmo ( Agent agent ) {
161
134
MissionWeapon weaponFromSlot0 = agent . Equipment [ EquipmentIndex . Weapon0 ] ;
162
- short ammoFromSlot0 = weaponFromSlot0 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot0 . IsShield ( ) ? ( short ) 0 : weaponFromSlot0 . Amount ;
135
+ short ammoFromSlot0 = weaponFromSlot0 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot0 . IsShield ( ) ? ( short ) 0 : weaponFromSlot0 . Amount ;
163
136
MissionWeapon weaponFromSlot1 = agent . Equipment [ EquipmentIndex . Weapon1 ] ;
164
- short ammoFromSlot1 = weaponFromSlot1 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot1 . IsShield ( ) ? ( short ) 0 : weaponFromSlot1 . Amount ;
137
+ short ammoFromSlot1 = weaponFromSlot1 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot1 . IsShield ( ) ? ( short ) 0 : weaponFromSlot1 . Amount ;
165
138
MissionWeapon weaponFromSlot2 = agent . Equipment [ EquipmentIndex . Weapon2 ] ;
166
- short ammoFromSlot2 = weaponFromSlot2 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot2 . IsShield ( ) ? ( short ) 0 : weaponFromSlot2 . Amount ;
139
+ short ammoFromSlot2 = weaponFromSlot2 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot2 . IsShield ( ) ? ( short ) 0 : weaponFromSlot2 . Amount ;
167
140
MissionWeapon weaponFromSlot3 = agent . Equipment [ EquipmentIndex . Weapon3 ] ;
168
- short ammoFromSlot3 = weaponFromSlot3 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot3 . IsShield ( ) ? ( short ) 0 : weaponFromSlot3 . Amount ;
141
+ short ammoFromSlot3 = weaponFromSlot3 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot3 . IsShield ( ) ? ( short ) 0 : weaponFromSlot3 . Amount ;
169
142
MissionWeapon weaponFromSlot4 = agent . Equipment [ EquipmentIndex . Weapon4 ] ;
170
- short ammoFromSlot4 = weaponFromSlot4 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot4 . IsShield ( ) ? ( short ) 0 : weaponFromSlot4 . Amount ;
143
+ short ammoFromSlot4 = weaponFromSlot4 . Equals ( MissionWeapon . Invalid ) || weaponFromSlot4 . IsShield ( ) ? ( short ) 0 : weaponFromSlot4 . Amount ;
171
144
172
- return ( short ) ( ammoFromSlot0 + ammoFromSlot1 + ammoFromSlot2 + ammoFromSlot3 + ammoFromSlot4 ) ;
145
+ return ( short ) ( ammoFromSlot0 + ammoFromSlot1 + ammoFromSlot2 + ammoFromSlot3 + ammoFromSlot4 ) ;
173
146
}
174
147
175
- private static short CalculateMaxAmmo ( Agent agent )
176
- {
148
+ private static short CalculateMaxAmmo ( Agent agent ) {
177
149
int arrowAmmo = agent . Equipment . GetMaxAmmo ( WeaponClass . Arrow ) ;
178
150
int boltAmmo = agent . Equipment . GetMaxAmmo ( WeaponClass . Bolt ) ;
179
151
int javelinAmmo = agent . Equipment . GetMaxAmmo ( WeaponClass . Javelin ) ;
180
152
int axeAmmo = agent . Equipment . GetMaxAmmo ( WeaponClass . ThrowingAxe ) ;
181
153
182
- return ( short ) ( arrowAmmo + boltAmmo + javelinAmmo + axeAmmo ) ;
154
+ return ( short ) ( arrowAmmo + boltAmmo + javelinAmmo + axeAmmo ) ;
183
155
}
184
156
}
185
157
}
0 commit comments