-
Notifications
You must be signed in to change notification settings - Fork 9
/
aitry.go
337 lines (310 loc) · 8.66 KB
/
aitry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2014,2015,2016,2017,2018,2019,2020,2021 SeukWon Kang (kasworld@gmail.com)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clientai
import (
"github.com/kasworld/goguelike/config/leveldata"
"github.com/kasworld/goguelike/enum/condition"
"github.com/kasworld/goguelike/enum/equipslottype"
"github.com/kasworld/goguelike/enum/fieldobjacttype"
"github.com/kasworld/goguelike/enum/potiontype"
"github.com/kasworld/goguelike/enum/scrolltype"
"github.com/kasworld/goguelike/enum/way9type"
"github.com/kasworld/goguelike/game/bias"
"github.com/kasworld/goguelike/protocol_c2t/c2t_idcmd"
"github.com/kasworld/goguelike/protocol_c2t/c2t_obj"
"github.com/kasworld/goguelike/protocol_c2t/c2t_packet"
)
var tryAutoActFn = []func(cai *ClientAI) bool{
tryAutoBattle,
tryAutoPickup,
tryAutoEquip,
tryAutoUsePotion,
tryAutoRecyclePotionScroll,
tryAutoRecycleEquip,
}
func (cai *ClientAI) actByControlMode() {
if cai.OLNotiData == nil || cai.OLNotiData.ActiveObj.HP <= 0 {
return
}
for _, v := range tryAutoActFn {
if v(cai) {
return
}
}
}
func tryAutoBattle(cai *ClientAI) bool {
cf := cai.CurrentFloor
if cai.OLNotiData == nil {
return false
}
playerX, playerY := cai.GetPlayerXY()
if !cf.IsValidPos(playerX, playerY) {
return false
}
if !cf.Tiles[playerX][playerY].CanBattle() {
return false
}
w, h := cf.Tiles.GetXYLen()
for _, ao := range cai.OLNotiData.ActiveObjList {
if !ao.Alive {
continue
}
if ao.UUID == cai.AccountInfo.ActiveObjUUID {
continue
}
if !cf.IsValidPos(ao.X, ao.Y) {
continue
}
if !cf.Tiles[ao.X][ao.Y].CanBattle() {
continue
}
isContact, dir := way9type.CalcContactDirWrappedXY(
playerX, playerY, ao.X, ao.Y, w, h)
if isContact && dir != way9type.Center {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Attack,
&c2t_obj.ReqAttack_data{Dir: dir},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}
func tryAutoPickup(cai *ClientAI) bool {
cf := cai.CurrentFloor
if cai.OLNotiData == nil {
return false
}
if cai.OLNotiData.ActiveObj.Conditions.TestByCondition(condition.Float) {
return false
}
playerX, playerY := cai.GetPlayerXY()
w, h := cf.Tiles.GetXYLen()
for _, po := range cai.OLNotiData.CarryObjList {
isContact, dir := way9type.CalcContactDirWrappedXY(
playerX, playerY, po.X, po.Y, w, h)
if !isContact {
continue
}
if dir == way9type.Center {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Pickup,
&c2t_obj.ReqPickup_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
} else {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Move,
&c2t_obj.ReqMove_data{Dir: dir},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}
func tryAutoEquip(cai *ClientAI) bool {
if cai.OLNotiData == nil {
return false
}
for _, po := range cai.OLNotiData.ActiveObj.EquippedPo {
if cai.needUnEquipCarryObj(po.GetBias()) {
cai.ReqWithRspFnWithAuth(c2t_idcmd.UnEquip,
&c2t_obj.ReqUnEquip_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
for _, po := range cai.OLNotiData.ActiveObj.EquipBag {
if cai.isBetterCarryObj(po.EquipType, po.GetBias()) {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Equip,
&c2t_obj.ReqEquip_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}
func tryAutoUsePotion(cai *ClientAI) bool {
if cai.OLNotiData == nil {
return false
}
for _, po := range cai.OLNotiData.ActiveObj.PotionBag {
if cai.needUsePotion(po) {
cai.ReqWithRspFnWithAuth(c2t_idcmd.DrinkPotion,
&c2t_obj.ReqDrinkPotion_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
for _, po := range cai.OLNotiData.ActiveObj.ScrollBag {
if cai.needUseScroll(po) {
cai.ReqWithRspFnWithAuth(c2t_idcmd.ReadScroll,
&c2t_obj.ReqReadScroll_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}
func tryAutoRecycleEquip(cai *ClientAI) bool {
if cai.OLNotiData == nil {
return false
}
if cai.OLNotiData.ActiveObj.Conditions.TestByCondition(condition.Float) {
return false
}
if cai.onFieldObj == nil {
return false
}
if cai.onFieldObj.ActType != fieldobjacttype.RecycleCarryObj {
return false
}
return cai.recycleEqbag()
}
func tryAutoRecyclePotionScroll(cai *ClientAI) bool {
if cai.OLNotiData == nil {
return false
}
if cai.OLNotiData.ActiveObj.Conditions.TestByCondition(condition.Float) {
return false
}
if cai.onFieldObj == nil {
return false
}
if cai.onFieldObj.ActType != fieldobjacttype.RecycleCarryObj {
return false
}
if cai.recycleUselessPotion() {
return true
}
if cai.recycleUselessScroll() {
return true
}
return false
}
/////////
func (cai *ClientAI) isBetterCarryObj(EquipType equipslottype.EquipSlotType, PoBias bias.Bias) bool {
aoEnvBias := cai.TowerBias().Add(cai.CurrentFloor.GetBias()).Add(cai.OLNotiData.ActiveObj.Bias)
newBiasAbs := aoEnvBias.Add(PoBias).AbsSum()
for _, v := range cai.OLNotiData.ActiveObj.EquippedPo {
if v.EquipType == EquipType {
return newBiasAbs > aoEnvBias.Add(v.GetBias()).AbsSum()+1
}
}
return newBiasAbs > aoEnvBias.AbsSum()+1
}
func (cai *ClientAI) needUnEquipCarryObj(PoBias bias.Bias) bool {
aoEnvBias := cai.TowerBias().Add(cai.CurrentFloor.GetBias()).Add(cai.OLNotiData.ActiveObj.Bias)
currentBias := aoEnvBias.Add(PoBias)
newBias := aoEnvBias
return newBias.AbsSum() > currentBias.AbsSum()+1
}
func (cai *ClientAI) needUseScroll(po *c2t_obj.ScrollClient) bool {
cf := cai.CurrentFloor
switch po.ScrollType {
case scrolltype.FloorMap:
if cf.Visited.CalcCompleteRate() < 1.0 {
return true
}
}
return false
}
func (cai *ClientAI) needUsePotion(po *c2t_obj.PotionClient) bool {
pao := cai.OLNotiData.ActiveObj
switch po.PotionType {
case potiontype.RecoverHP10:
return pao.HPMax-pao.HP > 10
case potiontype.RecoverHP50:
return pao.HPMax-pao.HP > 50
case potiontype.RecoverHP100:
return pao.HPMax-pao.HP > 100
case potiontype.RecoverSP10:
return pao.SPMax-pao.SP > 10
case potiontype.RecoverSP50:
return pao.SPMax-pao.SP > 50
case potiontype.RecoverSP100:
return pao.SPMax-pao.SP > 100
case potiontype.RecoverHPRate10:
return pao.HPMax-pao.HP > pao.HPMax/10
case potiontype.RecoverHPRate50:
return pao.HPMax/2 > pao.HP
case potiontype.RecoverHPFull:
return pao.HPMax/10 > pao.HP
case potiontype.RecoverSPRate10:
return pao.SPMax-pao.SP > pao.SPMax/10
case potiontype.RecoverSPRate50:
return pao.SPMax/2 > pao.SP
case potiontype.RecoverSPFull:
return pao.SPMax/10 > pao.SP
case potiontype.BuffRecoverHP1:
return pao.HPMax/2 > pao.HP
case potiontype.BuffRecoverSP1:
return pao.SPMax/2 > pao.SP
case potiontype.BuffSight1:
return pao.Sight <= leveldata.Sight(cai.level)
case potiontype.BuffSight5:
return pao.Sight <= leveldata.Sight(cai.level)
case potiontype.BuffSightMax:
return pao.Sight <= leveldata.Sight(cai.level)
}
return false
}
func (cai *ClientAI) recycleEqbag() bool {
var poList c2t_obj.CarryObjEqByLen
poList = append(poList, cai.OLNotiData.ActiveObj.EquipBag...)
if len(poList) == 0 {
return false
}
poList.Sort()
cai.ReqWithRspFnWithAuth(c2t_idcmd.Recycle,
&c2t_obj.ReqRecycle_data{UUID: poList[0].UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
func (cai *ClientAI) recycleUselessPotion() bool {
for _, po := range cai.OLNotiData.ActiveObj.PotionBag {
if potiontype.AIRecycleMap[po.PotionType] {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Recycle,
&c2t_obj.ReqRecycle_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}
func (cai *ClientAI) recycleUselessScroll() bool {
for _, po := range cai.OLNotiData.ActiveObj.ScrollBag {
if scrolltype.AIRecycleMap[po.ScrollType] {
cai.ReqWithRspFnWithAuth(c2t_idcmd.Recycle,
&c2t_obj.ReqRecycle_data{UUID: po.UUID},
func(hd c2t_packet.Header, rsp interface{}) error {
return nil
})
return true
}
}
return false
}