This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
forked from Mystery333/QSanguoshaAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart-ai.lua
6374 lines (5772 loc) · 242 KB
/
smart-ai.lua
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- This is the Smart AI, and it should be loaded and run at the server side
-- "middleclass" is the Lua OOP library written by kikito
-- more information see: https://github.com/kikito/middleclass
require "middleclass"
-- initialize the random seed for later use
math.randomseed(os.time())
-- SmartAI is the base class for all other specialized AI classes
SmartAI = class "SmartAI"
version = "QSanguosha AI 20141006 (V1.32 Alpha)"
-- checkout https://github.com/haveatry823/QSanguoshaAI for details
--- this function is only function that exposed to the host program
--- and it clones an AI instance by general name
-- @param player The ServerPlayer object that want to create the AI object
-- @return The AI object
function CloneAI(player)
return SmartAI(player).lua_ai
end
sgs.ais = {}
sgs.ai_card_intention = {}
sgs.ai_playerchosen_intention = {}
sgs.ai_Yiji_intention = {}
sgs.role_evaluation = {}
sgs.ai_role = {}
sgs.ai_keep_value = {}
sgs.ai_use_value = {}
sgs.ai_use_priority = {}
sgs.ai_suit_priority = {}
sgs.ai_chaofeng = {} -- obsolete
sgs.ai_global_flags = {}
sgs.ai_skill_invoke = {}
sgs.ai_skill_suit = {}
sgs.ai_skill_cardask = {}
sgs.ai_skill_choice = {}
sgs.ai_skill_askforag = {}
sgs.ai_skill_askforyiji = {}
sgs.ai_skill_pindian = {}
sgs.ai_filterskill_filter = {}
sgs.ai_skill_playerchosen = {}
sgs.ai_skill_discard = {}
sgs.ai_cardshow = {}
sgs.ai_nullification = {}
sgs.ai_skill_cardchosen = {}
sgs.ai_skill_use = {}
sgs.ai_cardneed = {}
sgs.ai_skill_use_func = {}
sgs.ai_skills = {}
sgs.ai_slash_weaponfilter = {}
sgs.ai_slash_prohibit = {}
sgs.ai_view_as = {}
sgs.ai_cardsview = {}
sgs.ai_cardsview_valuable = {}
sgs.dynamic_value = {
damage_card = {},
control_usecard = {},
control_card = {},
lucky_chance = {},
benefit = {}
}
sgs.ai_choicemade_filter = {
cardUsed = {},
cardResponded = {},
skillInvoke = {},
skillChoice = {},
Nullification = {},
playerChosen = {},
cardChosen = {},
Yiji = {},
viewCards = {},
pindian = {}
}
sgs.card_lack = {}
sgs.ai_need_damaged = {}
sgs.ai_debug_func = {}
sgs.ai_chat_func = {}
sgs.ai_event_callback = {}
sgs.explicit_renegade = false
sgs.ai_NeedPeach = {}
sgs.ai_damage_effect = {}
sgs.ai_current_judge = {}
for i=sgs.NonTrigger, sgs.NumOfEvents, 1 do
sgs.ai_debug_func[i] ={}
sgs.ai_chat_func[i] ={}
sgs.ai_event_callback[i]={}
end
function setInitialTables()
sgs.current_mode_players = { lord = 0, loyalist = 0, rebel = 0, renegade = 0 }
sgs.ai_type_name = {"Skill", "Basic", "Trick", "Equip"}
sgs.lose_equip_skill = "kofxiaoji|xiaoji|xuanfeng|nosxuanfeng"
sgs.need_kongcheng = "lianying|kongcheng|sijian"
sgs.masochism_skill = "guixin|yiji|fankui|jieming|xuehen|neoganglie|ganglie|vsganglie|enyuan|fangzhu|nosenyuan|langgu|quanji|" ..
"zhiyu|renjie|tanlan|tongxin|huashen"
sgs.wizard_skill = "guicai|guidao|jilve|tiandu|luoying|noszhenlie|huanshi"
sgs.wizard_harm_skill = "guicai|guidao|jilve"
sgs.priority_skill = "dimeng|haoshi|qingnang|nosjizhi|jizhi|guzheng|qixi|jieyin|guose|duanliang|jujian|fanjian|neofanjian|lijian|" ..
"noslijian|manjuan|tuxi|qiaobian|yongsi|zhiheng|luoshen|nosrende|rende|mingce|wansha|gongxin|jilve|anxu|" ..
"qice|yinling|qingcheng|houyuan|zhaoxin|shuangren"
sgs.save_skill = "jijiu|buyi|nosjiefan|chunlao|longhun"
sgs.exclusive_skill = "huilei|duanchang|wuhun|buqu|dushi"
sgs.cardneed_skill = "paoxiao|tianyi|xianzhen|shuangxiong|nosjizhi|jizhi|guose|duanliang|qixi|qingnang|yinling|luoyi|guhuo|nosguhuo|kanpo|" ..
"jieyin|renjie|zhiheng|nosrende|rende|nosjujian|guicai|guidao|longhun|luanji|qiaobian|beige|jieyuan|" ..
"mingce|nosfuhun|lirang|longluo|xuanfeng|xinzhan|dangxian|xiaoguo|neoluoyi|fuhun"
sgs.drawpeach_skill = "tuxi|qiaobian"
sgs.recover_skill = "nosrende|rende|kofkuanggu|kuanggu|zaiqi|jieyin|qingnang|yinghun|hunzi|shenzhi|longhun|nosmiji|zishou|ganlu|xueji|shangshi|" ..
"nosshangshi|ytchengxiang|buqu|miji"
sgs.use_lion_skill = "longhun|duanliang|qixi|guidao|noslijian|lijian|jujian|nosjujian|zhiheng|mingce|yongsi|fenxun|gongqi|" ..
"yinling|jilve|qingcheng|neoluoyi|diyyicong"
sgs.need_equip_skill = "shensu|mingce|jujian|beige|yuanhu|huyuan|gongqi|nosgongqi|yanzheng|qingcheng|neoluoyi|longhun|shuijian"
sgs.judge_reason = "bazhen|EightDiagram|wuhun|supply_shortage|tuntian|nosqianxi|nosmiji|indulgence|lightning|baonue"..
"|nosleiji|leiji|caizhaoji_hujia|tieji|luoshen|ganglie|neoganglie|vsganglie|kofkuanggu"
sgs.Friend_All = 0
sgs.Friend_Draw = 1
sgs.Friend_Male = 2
sgs.Friend_Female = 3
sgs.Friend_Wounded = 4
sgs.Friend_MaleWounded = 5
sgs.Friend_FemaleWounded = 6
sgs.Friend_Weak = 7
for _, aplayer in sgs.qlist(global_room:getAllPlayers()) do
table.insert(sgs.role_evaluation, aplayer:objectName())
table.insert(sgs.ai_role, aplayer:objectName())
if aplayer:isLord() then
sgs.role_evaluation[aplayer:objectName()] = {lord = 99999, rebel = 0, loyalist = 99999, renegade = 0}
sgs.ai_role[aplayer:objectName()] = "loyalist"
else
sgs.role_evaluation[aplayer:objectName()] = {rebel = 0, loyalist = 0, renegade = 0}
sgs.ai_role[aplayer:objectName()] = "neutral"
end
end
end
function SmartAI:initialize(player)
self.player = player
self.room = player:getRoom()
self.role = player:getRole()
self.lua_ai = sgs.LuaAI(player)
self.lua_ai.callback = function(full_method_name, ...)
--The __FUNCTION__ macro is defined as CLASS_NAME::SUBCLASS_NAME::FUNCTION_NAME
--in MSVC, while in gcc only FUNCTION_NAME is in place.
local method_name_start = 1
while true do
local found = string.find(full_method_name, "::", method_name_start)
if found ~= nil then
method_name_start = found + 2
else
break
end
end
local method_name = string.sub(full_method_name, method_name_start)
local method = self[method_name]
if method then
local success, result1, result2
success, result1, result2 = pcall(method, self, ...)
if not success then
self.room:writeToConsole(result1)
self.room:writeToConsole(method_name)
self.room:writeToConsole(debug.traceback())
self.room:outputEventStack()
else
return result1, result2
end
end
end
self.retain = 2
self.keepValue = {}
self.kept = {}
self.keepdata = {}
self.predictedRange = 1
self.slashAvail = 1
if not sgs.initialized then
sgs.initialized = true
sgs.ais = {}
sgs.turncount = 0
sgs.debugmode = false
global_room = self.room
global_room:writeToConsole(version .. ", Powered by " .. _VERSION)
setInitialTables()
if sgs.isRolePredictable() then
for _, aplayer in sgs.qlist(global_room:getAllPlayers()) do
if aplayer:getRole() == "renegade" then sgs.explicit_renegade = true end
if aplayer:getRole() ~= "lord" then
sgs.role_evaluation[aplayer:objectName()][aplayer:getRole()] = 65535
sgs.ai_role[aplayer:objectName()] = aplayer:getRole()
end
end
end
end
sgs.ais[player:objectName()] = self
sgs.card_lack[player:objectName()] = {}
sgs.card_lack[player:objectName()]["Slash"] = 0
sgs.card_lack[player:objectName()]["Jink"] = 0
sgs.card_lack[player:objectName()]["Peach"] = 0
sgs.ai_NeedPeach[player:objectName()] = 0
sgs.updateAlivePlayerRoles()
self:updatePlayers()
self:assignKeep(true)
end
function sgs.getCardNumAtCertainPlace(card, player, place)
if not card:isVirtualCard() and place == sgs.Player_PlaceHand then return 1
elseif card:subcardsLength() == 0 then return 0
else
local num = 0
for _, id in sgs.qlist(card:getSubcards()) do
if place == sgs.Player_PlaceHand then
if player:handCards():contains(id) then num = num + 1 end
elseif place == sgs.Player_PlaceEquip then
if player:hasEquip(sgs.Sanguosha:getCard(id)) then num = num + 1 end
end
end
return num
end
end
function sgs.getValue(player)
if not player then global_room:writeToConsole(debug.traceback()) end
return player:getHp() * 2 + player:getHandcardNum()
end
function sgs.getDefense(player)
if not player then global_room:writeToConsole(debug.traceback()) return 0 end
local current_player = global_room:getCurrent()
if not current_player then return sgs.getValue(player) end
local defense = player:getHp() * 2 + player:getHandcardNum()
if player:getArmor() and player:hasArmorEffect(player:getArmor():objectName()) then defense = defense + 2 end
if player:getDefensiveHorse() then defense = defense + 1 end
local hasEightDiagram = false
if player:hasArmorEffect("EightDiagram") or player:hasSkill("bazhen") and not player:getArmor() then
hasEightDiagram = true
end
if hasEightDiagram then
if player:hasSkill("tiandu") then defense = defense + 1 end
if player:hasSkill("gushou") then defense = defense + 1 end
if player:hasSkill("nosleiji") then defense = defense + 2 end
if player:hasSkill("leiji") then defense = defense + 2 end
if player:hasSkill("noszhenlie") then defense = defense + 1 end
if player:hasSkill("hongyan") then defense = defense + 2 end
end
local m = sgs.masochism_skill:split("|")
for _, masochism in ipairs(m) do
if player:hasSkill(masochism) and sgs.isGoodHp(player) then
defense = defense + 1
end
end
if player:hasSkill("jieming") then defense = defense + 3 end
if player:hasSkill("yiji") then defense = defense + 3 end
if player:hasSkill("guixin") then defense = defense + player:aliveCount() - 1 end
if player:hasSkill("yuce") then defense = defense + 2 end
if player:getMark("@tied") > 0 then defense = defense + 1 end
if player:hasSkill("chengxiang") then defense = defense + 2 end
if player:hasLordSkill("shichou") and player:getMark("xhate") == 1 then
for _, p in sgs.qlist(global_room:getOtherPlayers(player)) do
if p:getMark("hate_" .. player:objectName()) > 0 and p:getMark("@hate_to") > 0 then
defense = defense + p:getHp()
break
end
end
end
if player:hasSkill("nosrende") and player:getHp() > 2 and player:getHandcardNum() > 1 then defense = defense + 1 end
if player:hasSkill("rende") and player:getHp() > 2 and player:getHandcardNum() > 1 then defense = defense + 1 end
if player:hasSkill("kuanggu") and player:getHp() > 1 then defense = defense + 0.5 end
if player:hasSkill("kofkuanggu") and player:getHp() > 1 then defense = defense + 1 end
if player:hasSkill("zaiqi") and player:getHp() > 1 then defense = defense + player:getLostHp() * 0.5 end
if player:hasSkill("tianming") then defense = defense + 0.5 end
if player:hasSkill("nosmiji") then defense = defense + player:getLostHp() * 0.5 end
if player:hasSkill("keji") then defense = defense + player:getHandcardNum() * 0.25 end
if player:hasSkill("aocai") and player:getPhase() == sgs.Player_NotActive then defense = defense + 0.5 end
if player:hasSkill("wanrong") and not hasManjuanEffect(player) then defense = defense + 0.5 end
if player:hasSkill("tianxiang") then defense = defense + player:getHandcardNum() * 0.5 end
if player:getHp() > getBestHp(player) then defense = defense + 0.8 end
if player:getHp() <= 2 then defense = defense - 0.4 end
if player:hasSkill("benghuai") and player:getMaxHp() <= 5 then defense = defense - 1 end
if isLord(player) then
defense = defense - 0.4
if sgs.isLordInDanger() then defense = defense - 0.7 end
end
if not player:faceUp() then defense = defense - 1 end
if player:containsTrick("indulgence") and not player:containsTrick("YanxiaoCard") then defense = defense - 0.5 end
if player:containsTrick("supply_shortage") and not player:containsTrick("YanxiaoCard") then defense = defense - 0.5 end
if player:hasSkill("qianhuan") then defense = defense + 2 + player:getPile("sorcery"):length() end
if player:hasSkill("jijiu") then defense = defense + 2 end
if player:hasSkill("qingnang") then defense = defense + 2 end
if player:hasSkill("dimeng") then defense = defense + 2.5 end
if player:hasSkill("guzheng") then defense = defense + 2.5 end
if player:hasSkill("qiaobian") then defense = defense + 2.4 end
if player:hasSkill("jieyin") then defense = defense + 2.3 end
if player:hasSkill("noslijian") then defense = defense + 2.2 end
if player:hasSkill("lijian") then defense = defense + 2.1 end
if player:hasSkill("nosmiji") and player:isWounded() then defense = defense + 1.5 end
if player:hasSkill("xiliang") then defense = defense + 2 end
if player:hasSkill("shouye") then defense = defense + 2 end
if player:hasSkill("guhuo") then
for _, p in sgs.qlist(global_room:getOtherPlayers(player)) do
if p:hasSkill("chanyuan") then defense = defense + 1 end
end
end
if player:hasSkills("huashen+xinsheng") then defense = defense + (player:getState() == "online" and 4 or 2) end
if player:hasSkill("yishe") then defense = defense + 2 end
if player:hasSkill("paiyi") then defense = defense + 1.5 end
if player:hasSkill("yongsi") then defense = defense + 2 end
defense = defense + (player:aliveCount() - (player:getSeat() - current_player:getSeat()) % player:aliveCount()) / 4
defense = defense + player:getVisibleSkillList(true):length() * 0.25
return defense
end
function SmartAI:assignKeep(start)
self.keepValue = {}
self.kept = {}
if start then
--[[
通常的保留顺序
"peach-1" = 7,
"peach-2" = 5.8, "jink-1" = 5.2,
"peach-3" = 4.5, "analeptic-1" = 4.1,
"jink-2" = 4.0, "ExNihilo-1" = 3.9, "nullification-1" = 3.8, "thunderslash-1" = 3.66 "fireslash-1" = 3.63
"slash-1" = 3.6 indulgence-1 = 3.5 SupplyShortage-1 = 3.48 snatch-1 = 3.46 Dismantlement-1 = 3.44 Duel-1 = 3.42
Collateral-1 = 3.40 ArcheryAttack-1 = 3.38 SavageAssault-1 = 3.36 IronChain = 3.34 GodSalvation-1 = 3.32, Fireattack-1 = 3.3 "peach-4" = 3.1
"analeptic-2" = 2.9, "jink-3" = 2.7 ExNihilo-2 = 2.7 nullification-2 = 2.6 thunderslash-2 = 2.46 fireslash-2 = 2.43 slash-2 = 2.4
...
Weapon-1 = 2.08 Armor-1 = 2.06 DefensiveHorse-1 = 2.04 OffensiveHorse-1 = 2
...
AmazingGrace-1 = -9 Lightning-1 = -10
]]
self.keepdata = {}
for k, v in pairs(sgs.ai_keep_value) do
self.keepdata[k] = v
end
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
local skilltable = sgs[askill:objectName() .. "_keep_value"]
if skilltable then
for k, v in pairs(skilltable) do
self.keepdata[k] = v
end
end
end
end
if sgs.turncount <= 1 and #self.enemies == 0 then
self.keepdata.Jink = 4.2
end
if not self:isWeak() or self.player:getHandcardNum() >= 4 then
for _, friend in ipairs(self.friends_noself) do
if self:willSkipDrawPhase(friend) or self:willSkipPlayPhase(friend) then
self.keepdata.Nullification = 5.5
break
end
end
end
if self:getOverflow(self.player, true) == 1 then
self.keepdata.Analeptic = (self.keepdata.Jink or 5.2) + 0.1
-- 特殊情况下还是要留闪,待补充...
end
if not self:isWeak() then
local needDamaged = false
if self.player:getHp() > getBestHp(self.player) then needDamaged = true end
if not needDamaged and not sgs.isGoodTarget(self.player, self.friends, self) then needDamaged = true end
if not needDamaged then
for _, skill in sgs.qlist(self.player:getVisibleSkillList(true)) do
local callback = sgs.ai_need_damaged[skill:objectName()]
if type(callback) == "function" and callback(self, nil, self.player) then
needDamaged = true
break
end
end
end
if needDamaged then
self.keepdata.ThunderSlash = 5.2
self.keepdata.FireSlash = 5.1
self.keepdata.Slash = 5
self.keepdata.Jink = 4.5
end
end
for _, enemy in ipairs(self.enemies) do
if enemy:hasSkill("nosqianxi") and enemy:distanceTo(self.player) == 1 then
self.keepdata.Jink = 6
end
end
if self:isWeak() then
for _, ap in sgs.qlist(self.room:getAlivePlayers()) do
if ap:hasSkill("buyi") and self:isFriend(ap) then
self.keepdata = { Peach = 10, TrickCard = 8, EquipCard = 7.9 }
break
end
end
end
for _, card in sgs.qlist(self.player:getCards("he")) do
self.keepValue[card:getId()] = self:getKeepValue(card, self.kept, true)
end
local cards = sgs.QList2Table(self.player:getHandcards())
self:sortByKeepValue(cards, true)
local resetCards = function(allcards)
local result = {}
for _, a in ipairs(allcards) do
local found
for _, b in ipairs(self.kept) do
if a:getEffectiveId() == b:getEffectiveId() then
found = true
break
end
end
if not found then table.insert(result, a) end
end
return result
end
for i = 1, self.player:getHandcardNum() do
for _, card in ipairs(cards) do
self.keepValue[card:getId()] = self:getKeepValue(card, self.kept)
table.insert(self.kept, card)
break
end
cards = resetCards(cards)
end
end
function SmartAI:getKeepValue(card, kept, writeMode)
if type(card) == "number" then global_room:writeToConsole(debug.traceback()) return 0 end
local owner = self.room:getCardOwner(card:getEffectiveId())
if owner and owner:objectName() ~= self.player:objectName() then
self.room:writeToConsole(debug.traceback())
return sgs.ai_keep_value[card:getClassName()] or 0
end
if not kept then
return self.keepValue[card:getId()] or self.keepdata[card:getClassName()] or sgs.ai_keep_value[card:getClassName()] or 0
end
local maxvalue = self.keepdata[card:getClassName()] or sgs.ai_keep_value[card:getClassName()] or 0
local mostvaluable_class = card:getClassName()
for k, v in pairs(self.keepdata) do
if isCard(k, card, self.player) and v > maxvalue then
maxvalue = v
mostvaluable_class = k
end
end
local cardPlace = self.room:getCardPlace(card:getEffectiveId())
if writeMode then
if cardPlace == sgs.Player_PlaceEquip then
if card:isKindOf("Armor") and self:needToThrowArmor() then return -10
elseif self.player:hasSkills(sgs.lose_equip_skill) then
if card:isKindOf("OffensiveHorse") then return -10
elseif card:isKindOf("Weapon") then return -9.9
elseif card:isKindOf("OffensiveHorse") then return -9.8
else return -9.7
end
elseif self.player:hasSkills("bazhen|yizhong") and card:isKindOf("Armor") then return -8
elseif self:needKongcheng() then return 5.0
end
local value = 0
if card:isKindOf("Armor") then value = self:isWeak() and 5.2 or 3.2
elseif card:isKindOf("DefensiveHorse") then value = self:isWeak() and 4.3 or 3.19
elseif card:isKindOf("Weapon") then value = self.player:getPhase() == sgs.Player_Play and self:slashIsAvailable() and 3.39 or 3.2
elseif card:isKindOf("OffensiveHorse") then value = 3.17
else value = 3.18
end
if mostvaluable_class ~= card:getClassName() then
value = value + maxvalue
end
return value
elseif cardPlace == sgs.Player_PlaceHand then
local value_suit, value_number, newvalue = 0, 0, 0
local suit_string = card:getSuitString()
local number = card:getNumber()
local i = 0
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
if sgs[askill:objectName() .. "_suit_value"] then
local v = sgs[askill:objectName() .. "_suit_value"][suit_string]
if v then
i = i + 1
value_suit = value_suit + v
end
end
end
if i > 0 then value_suit = value_suit / i end
i = 0
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
if sgs[askill:objectName() .. "_number_value"] then
local v = sgs[askill:objectName() .. "_number_value"][tostring(number)]
if v then
i = i + 1
value_number = value_number + v
end
end
end
if i > 0 then value_number = value_number / i end
newvalue = maxvalue + value_suit + value_number
if mostvaluable_class ~= card:getClassName() then newvalue = newvalue + 0.1 end
newvalue = self:adjustKeepValue(card, newvalue)
return newvalue
else
return self.keepdata[card:getClassName()] or sgs.ai_keep_value[card:getClassName()] or 0
end
end
local newvalue = self.keepValue[card:getId()] or self.keepdata[card:getClassName()] or sgs.ai_keep_value[card:getClassName()] or 0
if cardPlace == sgs.Player_PlaceHand then
local dec = 0
for _, acard in ipairs(kept) do
if isCard(mostvaluable_class, acard, self.player) then
newvalue = newvalue - 1.2 - dec
dec = dec + 0.1
elseif acard:isKindOf("Slash") and card:isKindOf("Slash") then
newvalue = newvalue - 1.2 - dec
dec = dec + 0.1
end
end
end
return newvalue
end
function SmartAI:adjustKeepValue(card, v)
local suits = {"club", "spade", "diamond", "heart"}
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
local callback = sgs.ai_suit_priority[askill:objectName()]
if type(callback) == "function" then
suits = callback(self, card):split("|")
break
elseif type(callback) == "string" then
suits = callback:split("|")
break
end
end
table.insert(suits, "no_suit")
if card:isKindOf("Slash") then
if card:isRed() then v = v + 0.002 end
if card:isKindOf("NatureSlash") then v = v + 0.003 end
if self.player:hasSkill("jiang") and card:isRed() then v = v + 0.004 end
if self.player:hasSkill("wushen") and card:getSuit() == sgs.Card_Heart then v = v + 0.003 end
if self.player:hasSkill("jinjiu") and card:getEffectiveId() >= 0 and sgs.Sanguosha:getEngineCard(card:getEffectiveId()):isKindOf("Analeptic") then v = v - 0.002 end
end
local suits_value = {}
for index,suit in ipairs(suits) do
suits_value[suit] = index
end
v = v + (suits_value[card:getSuitString()] or 0) / 1000
v = v + card:getNumber() / 1000
return v
end
function SmartAI:getUseValue(card)
local class_name = card:getClassName()
local v = sgs.ai_use_value[class_name] or 0
if class_name == "LuaSkillCard" and card:isKindOf("LuaSkillCard") then
v = sgs.ai_use_value[card:objectName()] or 0
end
if card:isKindOf("GuhuoCard") or card:isKindOf("NosGuhuoCard") then
local userstring = card:toString()
userstring = (userstring:split(":"))[3]
local guhuocard = sgs.Sanguosha:cloneCard(userstring, card:getSuit(), card:getNumber())
local usevalue = self:getUseValue(guhuocard) + #self.enemies * 0.3
if sgs.Sanguosha:getCard(card:getSubcards():first()):objectName() == userstring
and (card:isKindOf("GuhuoCard") or card:getSuit() == sgs.Card_Heart) then usevalue = usevalue + 3 end
return usevalue
end
if card:getTypeId() == sgs.Card_TypeEquip then
if self.player:hasEquip(card) then
if card:isKindOf("OffensiveHorse") and self.player:getAttackRange() > 2 then return 5.5 end
if card:isKindOf("DefensiveHorse") and self:hasEightDiagramEffect() then return 5.5 end
return 9
end
if not self:getSameEquip(card) then v = 6.7 end
if self.weaponUsed and card:isKindOf("Weapon") then v = 2 end
if self.player:hasSkills("qiangxi|taichen|zhulou") and card:isKindOf("Weapon") then v = 2 end
if self.player:hasSkill("kurou") and card:isKindOf("Crossbow") then return 9 end
if self.player:hasSkills("bazhen|yizhong") and card:isKindOf("Armor") then v = 2 end
if self.role == "loyalist" and self.player:getKingdom()=="wei" and not self.player:hasSkill("bazhen") and getLord(self.player) and getLord(self.player):hasLordSkill("hujia") and card:isKindOf("EightDiagram") then
v = 9
end
if self.player:hasSkills(sgs.lose_equip_skill) then return 10 end
elseif card:getTypeId() == sgs.Card_TypeBasic then
if card:isKindOf("Slash") then
v = sgs.ai_use_value[class_name] or 0
if self.player:hasFlag("TianyiSuccess") or self.player:hasFlag("JiangchiInvoke")
or self:hasHeavySlashDamage(self.player, card) then v = 8.7 end
if self.player:getPhase() == sgs.Player_Play and self:slashIsAvailable() and #self.enemies > 0 and self:getCardsNum("Slash") == 1 then v = v + 5 end
if self:hasCrossbowEffect() then v = v + 4 end
if card:getSkillName() == "Spear" then v = v - 1 end
if card:getSkillName() == "longdan" and self:hasSkills("chongzhen") then v = v + 1 end
if card:getSkillName() == "fuhun" then v = v + (self.player:getPhase() == sgs.Player_Play and 1 or -1) end
elseif card:isKindOf("Jink") then
if self:getCardsNum("Jink") > 1 then v = v-6 end
if card:getSkillName() == "longdan" and self.player:hasSkill("chongzhen") then v = v + 1 end
elseif card:isKindOf("Peach") then
if self.player:isWounded() then v = v + 6 end
end
elseif card:getTypeId() == sgs.Card_TypeTrick then
if self.player:getWeapon() and not self.player:hasSkills(sgs.lose_equip_skill) and card:isKindOf("Collateral") then v = 2 end
if card:getSkillName() == "shuangxiong" then v = 6 end
if card:isKindOf("Duel") then v = v + self:getCardsNum("Slash") * 2 end
if self.player:hasSkill("nosjizhi") then v = v + 4 end
if self.player:hasSkill("jizhi") then v = v + 3 end
if self.player:hasSkill("wumou") and card:isNDTrick() and not card:isKindOf("AOE") then
if not (card:isKindOf("Duel") and self.player:hasUsed("WuqianCard")) then v = 1 end
end
end
if self:hasSkills(sgs.need_kongcheng) then
if self.player:getHandcardNum() == 1 then v = 10 end
end
if self.player:hasWeapon("Halberd") and card:isKindOf("Slash") and self.player:isLastHandCard(card) then v = 10 end
if self.player:getPhase() == sgs.Player_Play then v = self:adjustUsePriority(card, v) end
return v
end
function SmartAI:getUsePriority(card)
local class_name = card:getClassName()
local v = 0
if card:isKindOf("EquipCard") then
if self.player:hasSkills(sgs.lose_equip_skill) then return 15 end
if card:isKindOf("Armor") and not self.player:getArmor() then v = (sgs.ai_use_priority[class_name] or 0) + 5.2
elseif card:isKindOf("Weapon") and not self.player:getWeapon() then v = (sgs.ai_use_priority[class_name] or 0) + 3
elseif card:isKindOf("DefensiveHorse") and not self.player:getDefensiveHorse() then v = 5.8
elseif card:isKindOf("OffensiveHorse") and not self.player:getOffensiveHorse() then v = 5.5
end
return v
end
v = sgs.ai_use_priority[class_name] or 0
if class_name == "LuaSkillCard" and card:isKindOf("LuaSkillCard") then
v = sgs.ai_use_priority[card:objectName()] or 0
end
return self:adjustUsePriority(card, v)
end
function SmartAI:adjustUsePriority(card, v)
local suits = {"club", "spade", "diamond", "heart"}
if card:getTypeId() == sgs.Card_Skill then return v end
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
local callback = sgs.ai_suit_priority[askill:objectName()]
if type(callback) == "function" then
suits = callback(self, card):split("|")
break
elseif type(callback) == "string" then
suits = callback:split("|")
break
end
end
table.insert(suits, "no_suit")
if card:isKindOf("Slash") then
if card:getSkillName() == "Spear" then v = v - 0.1 end
if card:isRed() then
if self.slashAvail == 1 and self.player:hasSkill("jie") then v = v + 0.16 end
for _, friend in ipairs(self.friends) do
if friend:hasSkill("longyin") and friend:canDiscard(friend, "he") and not hasManjuanEffect(friend) then
v = v + 0.16
break
end
end
v = v - 0.05
end
if card:isKindOf("NatureSlash") then
if self.slashAvail == 1 then
v = v + 0.05
if card:isKindOf("FireSlash") then
for _, enemy in ipairs(self.enemies) do
if enemy:hasArmorEffect("Vine") then v = v + 0.07 break end
end
elseif card:isKindOf("ThunderSlash") then
for _, enemy in ipairs(self.enemies) do
if enemy:getMark("@fog") > 0 then v = v + 0.06 break end
end
end
else v = v - 0.05
end
end
if card:getSkillName() == "longdan" and self.player:hasSkill("chongzhen") then v = v + 0.08 end
if card:getSkillName() == "fuhun" then v = v + (self.player:getPhase() == sgs.Player_Play and 0.06 or -0.05) end
if self.player:hasSkill("jiang") and card:isRed() then v = v + 0.05 end
if self.player:hasSkill("wushen") and card:getSuit() == sgs.Card_Heart then v = v + 0.03 end
if self.player:hasSkill("jinjiu") and card:getEffectiveId() >= 0 and sgs.Sanguosha:getEngineCard(card:getEffectiveId()):isKindOf("Analeptic") then v = v + 0.03 end
if self.slashAvail == 1 then
v = v + math.min(sgs.Sanguosha:correctCardTarget(sgs.TargetModSkill_ExtraTarget, self.player, card) * 0.1, 0.5)
v = v + math.min(sgs.Sanguosha:correctCardTarget(sgs.TargetModSkill_DistanceLimit, self.player, card) * 0.05, 0.5)
if self.player:hasSkill("lihuo") and card:isKindOf("FireSlash") and card:getSkillName() == "lihuo" then v = v - 0.02 end
end
end
if self.player:hasSkill("mingzhe") and card:isRed() then v = v + (self.player:getPhase() ~= sgs.Player_NotActive and 0.05 or -0.05) end
if card:isBlack() and (card:isKindOf("Slash") or card:isNDTrick()) and self.player:hasSkill("zenhui") and not self.player:hasFlag("zenhui")
and sgs.Sanguosha:correctCardTarget(sgs.TargetModSkill_ExtraTarget, self.player, card) == 0 then
v = v + 0.1
end
local suits_value = {}
for index, suit in ipairs(suits) do
suits_value[suit] = -index
end
v = v + (suits_value[card:getSuitString()] or 0) / 1000
v = v + (13 - card:getNumber()) / 10000
return v
end
function SmartAI:getDynamicUsePriority(card)
if not card then return 0 end
if card:hasFlag("AIGlobal_KillOff") then return 15 end
if self.player:getMark("JianyingSuit") == card:getSuit() + 1 and self.player:getMark("JianyingNumber") == card:getNumber() then
return self:getUsePriority(card) + 50
end
local dynamic_value
-- direct control
if card:isKindOf("AmazingGrace") then
local zhugeliang = self.room:findPlayerBySkillName("kongcheng")
if zhugeliang and self:isEnemy(zhugeliang) and zhugeliang:isKongcheng() then
return math.max(sgs.ai_use_priority.Slash, sgs.ai_use_priority.Duel) + 0.1
end
end
if card:isKindOf("Peach") and self.player:hasSkills("kuanggu|kofkuanggu") then return 1.01 end
if card:isKindOf("YanxiaoCard") and self.player:containsTrick("YanxiaoCard") then return 0.1 end
if card:isKindOf("DelayedTrick") and not card:isKindOf("YanxiaoCard") and #card:getSkillName() > 0 then
return (sgs.ai_use_priority[card:getClassName()] or 0.01) - 0.01
end
if self.player:hasSkill("danshou") and not self.player:hasSkill("jueqing")
and (card:isKindOf("Slash") or card:isKindOf("Duel") or card:isKindOf("AOE")
or sgs.dynamic_value.damage_card[card:getClassName()]) then
return 0
end
if card:isKindOf("Duel") then
--[[if self:getCardsNum("FireAttack") > 0 and dummy_use.to and not dummy_use.to:isEmpty() then
for _, p in sgs.qlist(dummy_use.to) do
if p:getHp() == 1 then return sgs.ai_use_priority.FireAttack + 0.1 end
end
end]]
if self:hasCrossbowEffect()
or self.player:hasFlag("XianzhenSuccess")
or self.player:canSlashWithoutCrossbow()
or sgs.Sanguosha:correctCardTarget(sgs.TargetModSkill_Residue, self.player, sgs.Sanguosha:cloneCard("slash")) > 0
or self.player:hasUsed("FenxunCard") then
return sgs.ai_use_priority.Slash - 0.1
end
end
local value = self:getUsePriority(card) or 0
if card:getTypeId() == sgs.Card_TypeEquip then
if self.player:hasSkills(sgs.lose_equip_skill) then value = value + 12 end
if card:isKindOf("Weapon") and self.player:getPhase() == sgs.Player_Play and #self.enemies > 0 then
self:sort(self.enemies)
local enemy = self.enemies[1]
local v, inAttackRange = self:evaluateWeapon(card, self.player, enemy) / 20
value = value + string.format("%3.3f", v)
if inAttackRange then value = value + 0.5 end
end
end
if card:isKindOf("AmazingGrace") then
dynamic_value = 10
for _, player in sgs.qlist(self.room:getOtherPlayers(self.player)) do
dynamic_value = dynamic_value - 1
if self:isEnemy(player) then dynamic_value = dynamic_value - ((player:getHandcardNum() + player:getHp()) / player:getHp()) * dynamic_value
else dynamic_value = dynamic_value + ((player:getHandcardNum() + player:getHp()) / player:getHp()) * dynamic_value
end
end
value = value + dynamic_value
end
return value
end
function SmartAI:cardNeed(card)
if not self.friends then self.room:writeToConsole(debug.traceback()) self.room:writeToConsole(sgs.turncount) return end
local class_name = card:getClassName()
local suit_string = card:getSuitString()
local value
if card:isKindOf("Peach") then
self:sort(self.friends,"hp")
if self.friends[1]:getHp() < 2 then return 10 end
if (self.player:getHp() < 3 or self.player:getLostHp() > 1 and not self:hasSkills("longhun|buqu")) or self:hasSkills("kurou|benghuai") then return 14 end
return self:getUseValue(card)
end
local wuguotai = self.room:findPlayerBySkillName("buyi")
if wuguotai and self:isFriend(wuguotai) and not card:isKindOf("BasicCard") then
if (self.player:getHp() < 3 or self.player:getLostHp() > 1 and not self:hasSkills("longhun|buqu")) or self:hasSkills("kurou|benghuai") then return 13 end
end
if self:isWeak() and card:isKindOf("Jink") and self:getCardsNum("Jink") < 1 then return 12 end
local i = 0
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
if sgs[askill:objectName() .. "_keep_value"] then
local v = sgs[askill:objectName() .. "_keep_value"][class_name]
if v then
i = i + 1
if value then value = value + v else value = v end
end
end
end
if value then return value / i + 4 end
i = 0
for _, askill in sgs.qlist(self.player:getVisibleSkillList(true)) do
if sgs[askill:objectName() .. "_suit_value"] then
local v = sgs[askill:objectName() .. "_suit_value"][suit_string]
if v then
i = i + 1
if value then value = value + v else value = v end
end
end
end
if value then return value / i + 4 end
if card:isKindOf("Slash") and self:getCardsNum("Slash") == 0 then return 5.9 end
if card:isKindOf("Analeptic") then
if self.player:getHp() < 2 then return 10 end
end
if card:isKindOf("Slash") and (self:getCardsNum("Slash") > 0) then return 4 end
if card:isKindOf("Crossbow") and self.player:hasSkills("luoshen|yongsi|kurou|keji|wusheng|wushen") then return 20 end
if card:isKindOf("Axe") and self.player:hasSkills("luoyi|jiushi|jiuchi|pojun") then return 15 end
if card:isKindOf("Weapon") and (not self.player:getWeapon()) and (self:getCardsNum("Slash") > 1) then return 6 end
if card:isKindOf("Nullification") and self:getCardsNum("Nullification") == 0 then
if self:willSkipPlayPhase() or self:willSkipDrawPhase() then return 10 end
for _, friend in ipairs(self.friends) do
if self:willSkipPlayPhase(friend) or self:willSkipDrawPhase(friend) then return 9 end
end
return 6
end
return self:getUseValue(card)
end
-- compare functions
sgs.ai_compare_funcs = {
hp = function(a, b)
local c1 = a:getHp()
local c2 = b:getHp()
if c1 == c2 then
return sgs.ai_compare_funcs.defense(a, b)
else
return c1 < c2
end
end,
handcard = function(a, b)
local c1 = a:getHandcardNum()
local c2 = b:getHandcardNum()
if c1 == c2 then
return sgs.ai_compare_funcs.defense(a, b)
else
return c1 < c2
end
end,
handcard_defense = function(a, b)
local c1 = a:getHandcardNum()
local c2 = b:getHandcardNum()
if c1 == c2 then
return sgs.ai_compare_funcs.defense(a, b)
else
return c1 < c2
end
end,
value = function(a, b)
return sgs.getValue(a) < sgs.getValue(b)
end,
chaofeng = function(a, b)
return sgs.getDefense(a) > sgs.getDefense(b)
end,
defense = function(a, b)
return sgs.getDefenseSlash(a) < sgs.getDefenseSlash(b)
end,
threat = function(a, b)
local players = sgs.QList2Table(a:getRoom():getOtherPlayers(a))
local d1 = a:getHandcardNum()
for _, player in ipairs(players) do
if a:canSlash(player) then
d1 = d1 + 10 / (sgs.getDefense(player))
end
end
players = sgs.QList2Table(b:getRoom():getOtherPlayers(b))
local d2 = b:getHandcardNum()
for _, player in ipairs(players) do
if b:canSlash(player) then
d2 = d2 + 10 / (sgs.getDefense(player))
end
end
return d1 > d2
end,
}
function SmartAI:sort(players, key)
if not players then self.room:writeToConsole(debug.traceback()) end
if #players == 0 then return end
local func
if not key or key == "defense" or key == "defenseSlash" then
func = function(a, b)
return sgs.getDefenseSlash(a, self) < sgs.getDefenseSlash(b, self)
end
elseif key == "hp" then
func = function(a, b)
local c1 = a:getHp()
local c2 = b:getHp()
if c1 == c2 then
return sgs.getDefenseSlash(a, self) < sgs.getDefenseSlash(b, self)
else
return c1 < c2
end
end
elseif key == "handcard" then
func = function(a, b)
local c1 = a:getHandcardNum()
local c2 = b:getHandcardNum()
if c1 == c2 then
return sgs.getDefenseSlash(a, self) < sgs.getDefenseSlash(b, self)
else
return c1 < c2
end
end
elseif key == "handcard_defense" then
func = function(a, b, self)
local c1 = a:getHandcardNum()
local c2 = b:getHandcardNum()
if c1 == c2 then
return sgs.getDefenseSlash(a, self) < sgs.getDefenseSlash(b, self)
else
return c1 < c2
end
end
else
func = sgs.ai_compare_funcs[key]
end
if not func then self.room:writeToConsole(debug.traceback()) return end
function _sort(players, key)
table.sort(players, func)
end
if not pcall(_sort, players, key) then self.room:writeToConsole(debug.traceback()) end
end
function SmartAI:sortByKeepValue(cards, inverse, kept)
local compare_func = function(a, b)
local v1 = self:getKeepValue(a)
local v2 = self:getKeepValue(b)
if v1 ~= v2 then
if inverse then return v1 > v2 end
return v1 < v2
else
if not inverse then return a:getNumber() > b:getNumber() end
return a:getNumber() < b:getNumber()
end
end