2023-04-09 05:35:35 +00:00
|
|
|
-- SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-02-26 07:01:14 +00:00
|
|
|
---@class RandomAI: AI
|
|
|
|
local RandomAI = AI:subclass("RandomAI")
|
|
|
|
|
|
|
|
---@param self RandomAI
|
|
|
|
---@param skill ActiveSkill
|
2023-12-03 11:35:14 +00:00
|
|
|
---@param card? Card
|
2023-12-03 10:45:25 +00:00
|
|
|
function RandomAI:useActiveSkill(skill, card)
|
2023-02-26 07:01:14 +00:00
|
|
|
local room = self.room
|
|
|
|
local player = self.player
|
2023-02-26 08:51:29 +00:00
|
|
|
|
2024-04-01 17:00:10 +00:00
|
|
|
if skill:isInstanceOf(ViewAsSkill) then return "" end
|
|
|
|
|
2023-02-26 07:01:14 +00:00
|
|
|
local filter_func = skill.cardFilter
|
|
|
|
if card then
|
2023-12-09 13:57:47 +00:00
|
|
|
filter_func = Util.FalseFunc
|
2023-02-26 07:01:14 +00:00
|
|
|
end
|
|
|
|
|
2023-12-09 15:25:26 +00:00
|
|
|
if self.command == "PlayCard" and (not skill:canUse(player, card) or player:prohibitUse(card)) then
|
2023-02-26 07:01:14 +00:00
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
local max_try_times = 100
|
|
|
|
local selected_targets = {}
|
|
|
|
local selected_cards = {}
|
2023-12-09 15:25:26 +00:00
|
|
|
-- FIXME: ...
|
|
|
|
-- local min = skill:getMinTargetNum()
|
|
|
|
-- local max = skill:getMaxTargetNum(player, card)
|
|
|
|
-- local min_card = skill:getMinCardNum()
|
|
|
|
-- local max_card = skill:getMaxCardNum()
|
2024-01-24 19:13:57 +00:00
|
|
|
-- FIXME: ViewAsSkill can be buggy here
|
2023-07-07 17:05:54 +00:00
|
|
|
for _ = 0, max_try_times do
|
|
|
|
if skill:feasible(selected_targets, selected_cards, self.player, card) then break end
|
2023-12-09 13:57:47 +00:00
|
|
|
local avail_targets = table.filter(room:getAlivePlayers(), function(p)
|
2023-03-04 17:28:59 +00:00
|
|
|
local ret = skill:targetFilter(p.id, selected_targets, selected_cards, card or Fk:cloneCard'zixing')
|
2023-02-26 07:01:14 +00:00
|
|
|
if ret and card then
|
2023-11-07 13:14:51 +00:00
|
|
|
if player:isProhibited(p, card) then
|
2023-08-25 00:10:39 +00:00
|
|
|
ret = false
|
2023-02-26 07:01:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end)
|
|
|
|
avail_targets = table.map(avail_targets, function(p) return p.id end)
|
|
|
|
local avail_cards = table.filter(player:getCardIds{ Player.Hand, Player.Equip }, function(id)
|
|
|
|
return filter_func(skill, id, selected_cards, selected_targets)
|
|
|
|
end)
|
|
|
|
|
|
|
|
if #avail_targets == 0 and #avail_cards == 0 then break end
|
|
|
|
table.insertIfNeed(selected_targets, table.random(avail_targets))
|
|
|
|
table.insertIfNeed(selected_cards, table.random(avail_cards))
|
|
|
|
end
|
|
|
|
if skill:feasible(selected_targets, selected_cards, self.player, card) then
|
|
|
|
local ret = json.encode{
|
|
|
|
card = card and card.id or json.encode{
|
|
|
|
skill = skill.name,
|
|
|
|
subcards = selected_cards,
|
|
|
|
},
|
|
|
|
targets = selected_targets,
|
|
|
|
}
|
2023-03-04 17:28:59 +00:00
|
|
|
-- print(ret)
|
2023-02-26 07:01:14 +00:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param self RandomAI
|
|
|
|
---@param skill ViewAsSkill
|
2023-12-03 10:45:25 +00:00
|
|
|
function RandomAI:useVSSkill(skill, pattern, cancelable, extra_data)
|
2023-02-26 07:01:14 +00:00
|
|
|
local player = self.player
|
|
|
|
local room = self.room
|
|
|
|
local precondition
|
2024-04-01 17:00:10 +00:00
|
|
|
if not skill then return nil end
|
2023-02-26 07:01:14 +00:00
|
|
|
|
|
|
|
if self.command == "PlayCard" then
|
|
|
|
precondition = skill:enabledAtPlay(player)
|
|
|
|
if not precondition then return nil end
|
|
|
|
local exp = Exppattern:Parse(skill.pattern)
|
|
|
|
local cnames = {}
|
|
|
|
for _, m in ipairs(exp.matchers) do
|
|
|
|
if m.name then table.insertTable(cnames, m.name) end
|
|
|
|
end
|
|
|
|
for _, n in ipairs(cnames) do
|
|
|
|
local c = Fk:cloneCard(n)
|
|
|
|
precondition = c.skill:canUse(Self, c)
|
|
|
|
if precondition then break end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
precondition = skill:enabledAtResponse(player)
|
|
|
|
if not precondition then return nil end
|
|
|
|
local exp = Exppattern:Parse(pattern)
|
|
|
|
precondition = exp:matchExp(skill.pattern)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (not precondition) or math.random() < 0.2 then return nil end
|
|
|
|
|
|
|
|
local selected_cards = {}
|
|
|
|
local max_try_time = 100
|
|
|
|
|
2023-07-07 17:05:54 +00:00
|
|
|
for _ = 0, max_try_time do
|
2023-02-26 07:01:14 +00:00
|
|
|
local avail_cards = table.filter(player:getCardIds{ Player.Hand, Player.Equip }, function(id)
|
|
|
|
return skill:cardFilter(id, selected_cards)
|
|
|
|
end)
|
|
|
|
if #avail_cards == 0 then break end
|
|
|
|
table.insert(selected_cards, table.random(avail_cards))
|
|
|
|
if skill:viewAs(selected_cards) then
|
|
|
|
return {
|
|
|
|
skill = skill.name,
|
|
|
|
subcards = selected_cards,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2023-02-26 08:51:29 +00:00
|
|
|
return nil
|
2023-02-26 07:01:14 +00:00
|
|
|
end
|
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
---@type table<string, fun(self: RandomAI, jsonData: string): string>
|
2023-02-26 07:01:14 +00:00
|
|
|
local random_cb = {}
|
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
random_cb["AskForUseActiveSkill"] = function(self, jsonData)
|
2023-02-26 07:01:14 +00:00
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local skill = Fk.skills[data[1]]
|
|
|
|
local cancelable = data[3]
|
|
|
|
if cancelable and math.random() < 0.25 then return "" end
|
2023-12-03 10:45:25 +00:00
|
|
|
local extra_data = data[4]
|
2023-02-26 07:01:14 +00:00
|
|
|
for k, v in pairs(extra_data) do
|
|
|
|
skill[k] = v
|
|
|
|
end
|
2024-01-24 19:13:57 +00:00
|
|
|
if skill:isInstanceOf(ViewAsSkill) then
|
|
|
|
return RandomAI.useVSSkill(skill)
|
|
|
|
end
|
2023-12-03 10:45:25 +00:00
|
|
|
return RandomAI.useActiveSkill(self, skill)
|
2023-02-26 07:01:14 +00:00
|
|
|
end
|
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
random_cb["AskForSkillInvoke"] = function(self, jsonData)
|
2023-02-26 07:01:14 +00:00
|
|
|
return table.random{"1", ""}
|
|
|
|
end
|
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
random_cb["AskForUseCard"] = function(self, jsonData)
|
2023-08-25 00:10:39 +00:00
|
|
|
local player = self.player
|
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local card_name = data[1]
|
|
|
|
local pattern = data[2] or card_name
|
|
|
|
local cancelable = data[4] or true
|
|
|
|
local exp = Exppattern:Parse(pattern)
|
2023-08-27 12:21:37 +00:00
|
|
|
|
2023-11-07 13:14:51 +00:00
|
|
|
local avail_cards = table.map(player:getCardIds("he"), Util.Id2CardMapper)
|
2023-10-06 19:22:57 +00:00
|
|
|
avail_cards = table.filter(avail_cards, function(c)
|
|
|
|
return exp:match(c) and not player:prohibitUse(c)
|
2023-08-25 00:10:39 +00:00
|
|
|
end)
|
|
|
|
if #avail_cards > 0 then
|
|
|
|
if math.random() < 0.25 then return "" end
|
|
|
|
for _, card in ipairs(avail_cards) do
|
2023-10-06 19:22:57 +00:00
|
|
|
local skill = card.skill
|
2023-08-25 00:10:39 +00:00
|
|
|
local max_try_times = 100
|
|
|
|
local selected_targets = {}
|
|
|
|
local min = skill:getMinTargetNum()
|
|
|
|
local max = skill:getMaxTargetNum(player, card)
|
|
|
|
local min_card = skill:getMinCardNum()
|
|
|
|
local max_card = skill:getMaxCardNum()
|
|
|
|
for _ = 0, max_try_times do
|
2023-10-06 19:22:57 +00:00
|
|
|
if skill:feasible(selected_targets, { card.id }, self.player, card) then
|
|
|
|
return json.encode{
|
|
|
|
card = table.random(avail_cards).id,
|
|
|
|
targets = selected_targets,
|
|
|
|
}
|
|
|
|
end
|
2023-08-25 00:10:39 +00:00
|
|
|
local avail_targets = table.filter(self.room:getAlivePlayers(), function(p)
|
2023-10-06 19:22:57 +00:00
|
|
|
return skill:targetFilter(p.id, selected_targets, {card.id}, card or Fk:cloneCard'zixing')
|
2023-08-25 00:10:39 +00:00
|
|
|
end)
|
|
|
|
avail_targets = table.map(avail_targets, function(p) return p.id end)
|
|
|
|
|
|
|
|
if #avail_targets == 0 and #avail_cards == 0 then break end
|
|
|
|
table.insertIfNeed(selected_targets, table.random(avail_targets))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ""
|
|
|
|
end
|
2023-02-26 07:01:14 +00:00
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
random_cb["AskForResponseCard"] = function(self, jsonData)
|
2023-02-26 07:01:14 +00:00
|
|
|
local data = json.decode(jsonData)
|
|
|
|
local pattern = data[2]
|
|
|
|
local cancelable = true
|
|
|
|
local exp = Exppattern:Parse(pattern)
|
|
|
|
local avail_cards = table.filter(self.player:getCardIds{ Player.Hand, Player.Equip }, function(id)
|
|
|
|
return exp:match(Fk:getCardById(id))
|
|
|
|
end)
|
|
|
|
if #avail_cards > 0 then return json.encode{
|
|
|
|
card = table.random(avail_cards),
|
|
|
|
targets = {},
|
|
|
|
} end
|
|
|
|
-- TODO: vs skill
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2023-10-07 15:05:27 +00:00
|
|
|
random_cb["PlayCard"] = function(self, jsonData)
|
2023-11-07 13:14:51 +00:00
|
|
|
local cards = table.map(self.player:getCardIds(Player.Hand), Util.Id2CardMapper)
|
2023-02-26 07:01:14 +00:00
|
|
|
local actives = table.filter(self.player:getAllSkills(), function(s)
|
|
|
|
return s:isInstanceOf(ActiveSkill)
|
|
|
|
end)
|
|
|
|
local vss = table.filter(self.player:getAllSkills(), function(s)
|
|
|
|
return s:isInstanceOf(ViewAsSkill)
|
|
|
|
end)
|
|
|
|
table.insertTable(cards, actives)
|
|
|
|
table.insertTable(cards, vss)
|
|
|
|
|
|
|
|
while #cards > 0 do
|
|
|
|
local sth = table.random(cards)
|
|
|
|
if sth:isInstanceOf(Card) then
|
|
|
|
local card = sth
|
|
|
|
local skill = card.skill ---@type ActiveSkill
|
|
|
|
if math.random() > 0.15 then
|
2023-12-03 10:45:25 +00:00
|
|
|
local ret = RandomAI.useActiveSkill(self, skill, card)
|
2023-02-26 07:01:14 +00:00
|
|
|
if ret ~= "" then return ret end
|
|
|
|
table.removeOne(cards, card)
|
|
|
|
else
|
|
|
|
table.removeOne(cards, card)
|
|
|
|
end
|
|
|
|
elseif sth:isInstanceOf(ActiveSkill) then
|
|
|
|
local active = sth
|
|
|
|
if math.random() > 0.30 then
|
2023-12-03 10:45:25 +00:00
|
|
|
local ret = RandomAI.useActiveSkill(self, active, nil)
|
2023-02-26 07:01:14 +00:00
|
|
|
if ret ~= "" then return ret end
|
|
|
|
end
|
|
|
|
table.removeOne(cards, active)
|
|
|
|
else
|
|
|
|
local vs = sth
|
|
|
|
if math.random() > 0.20 then
|
2023-12-03 10:45:25 +00:00
|
|
|
local ret = self:useVSSkill(vs)
|
2023-02-26 07:01:14 +00:00
|
|
|
-- TODO: handle vs result
|
|
|
|
end
|
|
|
|
table.removeOne(cards, vs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2023-12-03 10:45:25 +00:00
|
|
|
-- FIXME: for smart ai
|
|
|
|
RandomAI.cb_table = random_cb
|
|
|
|
|
2023-02-26 07:01:14 +00:00
|
|
|
function RandomAI:initialize(player)
|
|
|
|
AI.initialize(self, player)
|
|
|
|
self.cb_table = random_cb
|
|
|
|
end
|
|
|
|
|
|
|
|
return RandomAI
|