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 pathdebug-ai.lua
256 lines (234 loc) · 10.2 KB
/
debug-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
sgs.ai_debug_func[sgs.EventPhaseStart].debugfunc=function(self, player, data)
if player:getPhase()== sgs.Player_Start then
debugFunc(self, self.room, player, data)
end
end
sgs.ai_debug_func[sgs.CardUsed].debugfunc=function(self, player, data)
local card= data:toCardUse().card
if card:isKindOf("Peach") or card:isKindOf("Nullification") then
debugFunc(self, self.room, player, data)
end
end
function debugFunc(self, room, player, data)
local owner =room:getOwner()
local choices = {"showVisiblecards","showHandcards","objectiveLevel","getDefenseSlash"}
local debugmsg =function(fmt,...)
if type(fmt)=="boolean" then fmt = fmt and "true" or "false" end
local msg=string.format(fmt, ...)
player:speak(msg)
logmsg("ai.html","<pre>"..msg.."</pre>")
end
local players = sgs.QList2Table(room:getAlivePlayers())
repeat
local choice=room:askForChoice(owner,"aidebug","cancel+"..table.concat(choices,"+"))
if choice=="cancel" then break end
if choice == "showVisiblecards" then
debugmsg(" ")
debugmsg("===================")
debugmsg("查看已知牌; 当前AI是: %s[%s]",sgs.Sanguosha:translate(player:getGeneralName()),sgs.Sanguosha:translate(player:getRole()) )
for i=1, #players, 1 do
local msg=string.format("%s已知牌:",sgs.Sanguosha:translate(players[i]:getGeneralName()))
local cards = sgs.QList2Table(players[i]:getHandcards())
for _, card in ipairs(cards) do
local flag=string.format("%s_%s_%s","visible",player:objectName(),players[i]:objectName())
if card:hasFlag("visible") or card:hasFlag(flag) then
msg = msg .. card:getLogName() ..", "
end
end
debugmsg(msg)
end
end
if choice == "showHandcards" then
debugmsg(" ")
debugmsg("===================")
debugmsg("查看手牌; 当前AI是: %s[%s]",sgs.Sanguosha:translate(player:getGeneralName()),sgs.Sanguosha:translate(player:getRole()) )
for i=1, #players, 1 do
local msg=string.format("%s手牌:",sgs.Sanguosha:translate(players[i]:getGeneralName()))
local cards = sgs.QList2Table(players[i]:getHandcards())
for _, card in ipairs(cards) do
msg = msg .. card:getLogName() ..", "
end
debugmsg(msg)
end
end
if choice == "objectiveLevel" then
debugmsg(" ")
debugmsg("============%s(%.1f)", sgs.gameProcess(room), sgs.gameProcess(room,1))
debugmsg("查看关系; 当前AI是: %s[%s]",sgs.Sanguosha:translate(player:getGeneralName()),sgs.Sanguosha:translate(player:getRole()) )
for i=1, #players, 1 do
local level=self:objectiveLevel(players[i])
local rel =level>0 and "敌对" or (level<0 and "友好" or "中立")
rel = rel .. " " .. level
debugmsg("%s[%s]: %d:%d:%d %s",
sgs.Sanguosha:translate(players[i]:getGeneralName()),
sgs.Sanguosha:translate(sgs.evaluatePlayerRole(players[i])),
sgs.role_evaluation[players[i]:objectName()]["rebel"],
sgs.role_evaluation[players[i]:objectName()]["loyalist"],
sgs.role_evaluation[players[i]:objectName()]["renegade"],
rel)
end
end
if choice == "getDefenseSlash" then
debugmsg(" ")
debugmsg("===================")
debugmsg("查看对杀的防御; 当前AI是: %s[%s]",sgs.Sanguosha:translate(player:getGeneralName()),sgs.Sanguosha:translate(player:getRole()) )
for i=1, #players, 1 do
debugmsg("%s:%.2f",sgs.Sanguosha:translate(players[i]:getGeneralName()),sgs.getDefenseSlash(players[i]))
end
end
until false
end
function logmsg(fname,fmt,...)
local fp = io.open(fname,"ab")
if type(fmt)=="boolean" then fmt = fmt and "true" or "false" end
fp:write(string.format(fmt, ...).."\r\n")
fp:close()
end
function endlessNiepan(who)
local room = who:getRoom()
if who:getGeneral2() or who:getHp() > 0 then return end
local rebel_value = sgs.role_evaluation[who:objectName()]["rebel"]
local renegade_value = sgs.role_evaluation[who:objectName()]["renegade"]
local loyalist_value = sgs.role_evaluation[who:objectName()]["loyalist"]
for _,skill in sgs.qlist(who:getVisibleSkillList()) do
if skill:getLocation()==sgs.Skill_Right then
room:detachSkillFromPlayer(who, skill:objectName())
end
end
local names = sgs.Sanguosha:getRandomGenerals(1)
room:changeHero(who, names[1] , true, true, false, true)
room:setPlayerProperty(who, "maxhp", sgs.QVariant(5))
room:setPlayerProperty(who, "kingdom", sgs.QVariant(who:getGeneral():getKingdom()))
who:setGender(who:getGeneral():getGender())
room:setTag("SwapPile",sgs.QVariant(0))
who:bury()
who:drawCards(5)
sgs.role_evaluation[who:objectName()]["rebel"] = rebel_value
sgs.role_evaluation[who:objectName()]["renegade"] = renegade_value
sgs.role_evaluation[who:objectName()]["loyalist"] = loyalist_value
end
function SmartAI:printStand()
self.room:output(self.player:getRole())
self.room:output("enemies:")
for _, player in ipairs(self.enemies) do
self.room:output(player:getGeneralName())
end
self.room:output("end of enemies")
self.room:output("friends:")
for _, player in ipairs(self.friends) do
self.room:output(player:getGeneralName())
end
self.room:output("end of friends")
end
function SmartAI:printFEList()
for _, player in ipairs (self.enemies) do
self.room:writeToConsole("enemy "..player:getGeneralName()..(sgs.role_evaluation[player:objectName()][player:getRole()] or "") .. player:getRole())
end
for _, player in ipairs (self.friends_noself) do
self.room:writeToConsole("friend "..player:getGeneralName()..(sgs.role_evaluation[player:objectName()][player:getRole()] or "") .. player:getRole())
end
self.room:writeToConsole(self.player:getGeneralName().." list end")
end
function SmartAI:log(outString)
self.room:output(outString)
end
function outputPlayersEvaluation()
if not global_room:getLord() then return end
global_room:writeToConsole("=========== MISJUDGE START ===========" )
for _, player in sgs.qlist(global_room:getOtherPlayers(global_room:getLord())) do
local evaluate_role = sgs.evaluatePlayerRole(player)
global_room:writeToConsole("<------- " .. player:getGeneralName() .. " ------->")
global_room:writeToConsole("Role: " .. player:getRole() .. " Evaluate role: " .. evaluate_role)
global_room:writeToConsole("Rebel:" .. sgs.role_evaluation[player:objectName()]["rebel"] .. " Loyalist:"
.. sgs.role_evaluation[player:objectName()]["loyalist"] .. " Renegade:"
.. sgs.role_evaluation[player:objectName()]["renegade"])
end
global_room:writeToConsole("================ END ================" )
end
function sgs.checkMisjudge(player)
if not global_room:getLord() then return end
local room = global_room
local mode = room:getMode()
if player then
if sgs.current_mode_players[player:getRole()] > sgs.mode_player[mode][player:getRole()] or sgs.current_mode_players[player:getRole()] < 0 then
player:getRoom():writeToConsole("Misjudge--------> Role:" .. player:getRole() .. " Current players:" .. sgs.current_mode_players[player:getRole()]
.. " Valid players:" .. sgs.mode_player[mode][player:getRole()])
end
else
local rebel_num, loyalist_num, renegade_num = 0, 0, 0
local evaluate_rebel, evaluate_loyalist, evaluate_renegade = 0, 0, 0
for _, p in sgs.qlist(room:getOtherPlayers(room:getLord())) do
local role = p:getRole()
if role == "rebel" then rebel_num = rebel_num + 1
elseif role == "loyalist" then loyalist_num = loyalist_num + 1
elseif role == "renegade" then renegade_num = renegade_num + 1
end
end
for _, p in sgs.qlist(room:getOtherPlayers(room:getLord())) do
if sgs.evaluatePlayerRole(p) == "rebel" and rebel_num > 0 then evaluate_rebel = evaluate_rebel + 1
elseif sgs.evaluatePlayerRole(p) == "loyalist" and loyalist_num > 0 then evaluate_loyalist = evaluate_loyalist + 1
elseif sgs.evaluatePlayerRole(p) == "renegade" and renegade_num > 0 then evaluate_renegade = evaluate_renegade + 1
end
end
if evaluate_renegade < 1 then
if (evaluate_rebel >= rebel_num + renegade_num and evaluate_rebel > rebel_num)
or (evaluate_loyalist >= loyalist_num + renegade_num and evaluate_loyalist > loyalist_num)
or (evaluate_rebel == rebel_num + 1 and evaluate_loyalist == loyalist_num + 1) then
outputPlayersEvaluation()
if evaluate_rebel >= rebel_num + renegade_num and evaluate_rebel > rebel_num then
sgs.modifiedRoleTrends("rebel")
elseif evaluate_loyalist >= loyalist_num + renegade_num and evaluate_loyalist > loyalist_num and rebel_num > 0 then
sgs.modifiedRoleTrends("loyalist")
elseif evaluate_rebel > rebel_num and evaluate_loyalist > loyalist_num then
sgs.modifiedRoleTrends("rebel")
sgs.modifiedRoleTrends("loyalist")
end
end
else
if evaluate_rebel > rebel_num or evaluate_loyalist > loyalist_num or evaluate_renegade > renegade_num then
outputPlayersEvaluation()
if evaluate_rebel > rebel_num then sgs.modifiedRoleTrends("rebel") end
if evaluate_loyalist > loyalist_num then sgs.modifiedRoleTrends("loyalist") end
if evaluate_renegade > renegade_num then sgs.modifiedRoleTrends("renegade") end
end
end
end
end
local cardparse = sgs.Card_Parse
function sgs.Card_Parse(str)
if not str then global_room:writeToConsole(debug.traceback()) end
if type(str) ~= "string" and type(str) ~= "number" and str.toString() then
global_room:writeToConsole(str:toString())
end
return cardparse(str)
end
function sgs.broadcastRole(role_type)
role = role_type == "r" and "rebel"
or role_type == "R" and "renegade"
or role_type == "l" and "loyalist"
for _, p in sgs.qlist(global_room:getAlivePlayers()) do
if role and p:getRole() == role then global_room:broadcastProperty(p, "role")
elseif not role then global_room:broadcastProperty(p, "role") end
end
sgs.evaluateAlivePlayersRole()
end
function sgs.printFEList(player)
global_room:writeToConsole("")
global_room:writeToConsole("gameProcess -> " .. sgs.gameProcess())
for _, p in sgs.qlist(global_room:getAlivePlayers()) do
if player and p:objectName() ~= player:objectName() then continue end
global_room:writeToConsole("==== " .. p:getGeneralName() .. " Role::" .. p:getRole() .. " ====")
local sgsself = sgs.ais[p:objectName()]
sgsself:updatePlayers()
local msge = "enemies:"
for _, player in ipairs(sgsself.enemies) do
msge = msge .. player:getGeneralName() .. ", "
end
global_room:writeToConsole(msge)
local msgf = "friends:"
for _, player in ipairs(sgsself.friends) do
msgf = msgf .. player:getGeneralName() .. ", "
end
global_room:writeToConsole(msgf)
end
end