2022-03-30 08:33:56 +00:00
|
|
|
---@class Engine : Object
|
2022-04-01 12:51:01 +00:00
|
|
|
---@field packages table<string, Package>
|
2022-04-15 10:37:20 +00:00
|
|
|
---@field package_names string[]
|
2022-04-01 12:51:01 +00:00
|
|
|
---@field skills table<string, Skill>
|
|
|
|
---@field related_skills table<string, Skill[]>
|
|
|
|
---@field global_trigger TriggerSkill[]
|
2023-01-04 06:21:29 +00:00
|
|
|
---@field global_status_skill table<class, Skill[]>
|
2022-04-01 12:51:01 +00:00
|
|
|
---@field generals table<string, General>
|
|
|
|
---@field lords string[]
|
|
|
|
---@field cards Card[]
|
2023-02-27 02:23:48 +00:00
|
|
|
---@field translations table<string, table<string, string>>
|
2022-03-28 14:24:30 +00:00
|
|
|
local Engine = class("Engine")
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:initialize()
|
2022-04-30 07:27:56 +00:00
|
|
|
-- Engine should be singleton
|
|
|
|
if Fk ~= nil then
|
|
|
|
error("Engine has been initialized")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Fk = self
|
|
|
|
|
|
|
|
self.packages = {} -- name --> Package
|
|
|
|
self.package_names = {}
|
|
|
|
self.skills = {} -- name --> Skill
|
|
|
|
self.related_skills = {} -- skillName --> relatedSkill[]
|
|
|
|
self.global_trigger = {}
|
2023-01-04 06:21:29 +00:00
|
|
|
self.global_status_skill = {}
|
2022-04-30 07:27:56 +00:00
|
|
|
self.generals = {} -- name --> General
|
|
|
|
self.lords = {} -- lordName[]
|
|
|
|
self.cards = {} -- Card[]
|
|
|
|
self.translations = {} -- srcText --> translated
|
|
|
|
|
|
|
|
self:loadPackages()
|
2022-09-15 03:17:13 +00:00
|
|
|
self:addSkills(AuxSkills)
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param pack Package
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:loadPackage(pack)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(pack:isInstanceOf(Package))
|
2023-02-26 08:51:29 +00:00
|
|
|
if self.packages[pack.name] ~= nil then
|
2022-04-30 07:27:56 +00:00
|
|
|
error(string.format("Duplicate package %s detected", pack.name))
|
|
|
|
end
|
|
|
|
self.packages[pack.name] = pack
|
|
|
|
table.insert(self.package_names, pack.name)
|
|
|
|
|
|
|
|
-- add cards, generals and skills to Engine
|
|
|
|
if pack.type == Package.CardPack then
|
|
|
|
self:addCards(pack.cards)
|
|
|
|
elseif pack.type == Package.GeneralPack then
|
|
|
|
self:addGenerals(pack.generals)
|
|
|
|
end
|
|
|
|
self:addSkills(pack:getSkills())
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:loadPackages()
|
2022-04-30 07:27:56 +00:00
|
|
|
local directories = FileIO.ls("packages")
|
|
|
|
|
|
|
|
-- load standard & standard_cards first
|
|
|
|
self:loadPackage(require("packages.standard"))
|
|
|
|
self:loadPackage(require("packages.standard_cards"))
|
|
|
|
table.removeOne(directories, "standard")
|
|
|
|
table.removeOne(directories, "standard_cards")
|
|
|
|
|
|
|
|
for _, dir in ipairs(directories) do
|
2023-02-15 11:54:35 +00:00
|
|
|
if (not string.find(dir, ".disabled")) and FileIO.isDir("packages/" .. dir)
|
|
|
|
and FileIO.exists("packages/" .. dir .. "/init.lua") then
|
2022-04-30 07:27:56 +00:00
|
|
|
local pack = require(string.format("packages.%s", dir))
|
|
|
|
-- Note that instance of Package is a table too
|
|
|
|
-- so dont use type(pack) == "table" here
|
2023-02-21 05:44:24 +00:00
|
|
|
if type(pack) == "table" then
|
|
|
|
if pack[1] ~= nil then
|
|
|
|
for _, p in ipairs(pack) do
|
|
|
|
self:loadPackage(p)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self:loadPackage(pack)
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param t table
|
2023-02-27 02:23:48 +00:00
|
|
|
function Engine:loadTranslationTable(t, lang)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(type(t) == "table")
|
2023-02-27 02:23:48 +00:00
|
|
|
lang = lang or "zh_CN"
|
|
|
|
self.translations[lang] = self.translations[lang] or {}
|
2022-04-30 07:27:56 +00:00
|
|
|
for k, v in pairs(t) do
|
2023-02-27 02:23:48 +00:00
|
|
|
self.translations[lang][k] = v
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-05-01 10:37:13 +00:00
|
|
|
function Engine:translate(src)
|
2023-02-27 02:23:48 +00:00
|
|
|
local lang = Config.language or "zh_CN"
|
|
|
|
if not self.translations[lang] then lang = "zh_CN" end
|
|
|
|
local ret = self.translations[lang][src]
|
|
|
|
return ret or src
|
2022-05-01 10:37:13 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param skill Skill
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addSkill(skill)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(skill.class:isSubclassOf(Skill))
|
|
|
|
if self.skills[skill.name] ~= nil then
|
|
|
|
error(string.format("Duplicate skill %s detected", skill.name))
|
|
|
|
end
|
|
|
|
self.skills[skill.name] = skill
|
2023-01-04 06:21:29 +00:00
|
|
|
|
|
|
|
if skill.global then
|
|
|
|
if skill:isInstanceOf(TriggerSkill) then
|
|
|
|
table.insert(self.global_trigger, skill)
|
|
|
|
else
|
|
|
|
local t = self.global_status_skill
|
|
|
|
t[skill.class] = t[skill.class] or {}
|
|
|
|
table.insert(t[skill.class], skill)
|
|
|
|
end
|
|
|
|
end
|
2023-01-29 10:11:41 +00:00
|
|
|
|
|
|
|
for _, s in ipairs(skill.related_skills) do
|
|
|
|
self:addSkill(s)
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param skills Skill[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addSkills(skills)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(type(skills) == "table")
|
|
|
|
for _, skill in ipairs(skills) do
|
|
|
|
self:addSkill(skill)
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param general General
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addGeneral(general)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(general:isInstanceOf(General))
|
|
|
|
if self.generals[general.name] ~= nil then
|
|
|
|
error(string.format("Duplicate general %s detected", general.name))
|
|
|
|
end
|
|
|
|
self.generals[general.name] = general
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param generals General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addGenerals(generals)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(type(generals) == "table")
|
|
|
|
for _, general in ipairs(generals) do
|
|
|
|
self:addGeneral(general)
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
local cardId = 1
|
2023-01-16 11:13:07 +00:00
|
|
|
local _card_name_table = {}
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param card Card
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addCard(card)
|
2022-04-30 07:27:56 +00:00
|
|
|
assert(card.class:isSubclassOf(Card))
|
|
|
|
card.id = cardId
|
|
|
|
cardId = cardId + 1
|
|
|
|
table.insert(self.cards, card)
|
2023-02-15 11:54:35 +00:00
|
|
|
if _card_name_table[card.name] == nil then
|
|
|
|
_card_name_table[card.name] = card
|
|
|
|
end
|
2022-03-28 14:24:30 +00:00
|
|
|
end
|
|
|
|
|
2022-03-31 05:29:23 +00:00
|
|
|
---@param cards Card[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:addCards(cards)
|
2022-04-30 07:27:56 +00:00
|
|
|
for _, card in ipairs(cards) do
|
|
|
|
self:addCard(card)
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2023-01-16 11:13:07 +00:00
|
|
|
---@param name string
|
|
|
|
---@param suit Suit
|
|
|
|
---@param number integer
|
|
|
|
---@return Card
|
|
|
|
function Engine:cloneCard(name, suit, number)
|
|
|
|
local cd = _card_name_table[name]
|
|
|
|
assert(cd, "Attempt to clone a card that not added to engine")
|
2023-02-15 11:54:35 +00:00
|
|
|
local ret = cd:clone(suit, number)
|
|
|
|
ret.package = cd.package
|
|
|
|
return ret
|
2023-01-16 11:13:07 +00:00
|
|
|
end
|
|
|
|
|
2022-04-01 12:51:01 +00:00
|
|
|
---@param num integer
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param generalPool General[]
|
|
|
|
---@param except string[]
|
|
|
|
---@param filter function
|
2022-04-02 13:39:44 +00:00
|
|
|
---@return General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:getGeneralsRandomly(num, generalPool, except, filter)
|
2022-04-30 07:27:56 +00:00
|
|
|
if filter then
|
|
|
|
assert(type(filter) == "function")
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
generalPool = generalPool or self.generals
|
|
|
|
except = except or {}
|
2023-02-15 16:54:39 +00:00
|
|
|
for _, g in ipairs(self.packages["test_p_0"].generals) do
|
|
|
|
table.insert(except, g.name)
|
|
|
|
end
|
2022-04-01 12:51:01 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
local availableGenerals = {}
|
|
|
|
for _, general in pairs(generalPool) do
|
|
|
|
if not table.contains(except, general.name) and not (filter and filter(general)) then
|
|
|
|
table.insert(availableGenerals, general)
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if #availableGenerals == 0 then
|
|
|
|
return {}
|
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
local result = {}
|
|
|
|
for i = 1, num do
|
|
|
|
local randomGeneral = math.random(1, #availableGenerals)
|
|
|
|
table.insert(result, availableGenerals[randomGeneral])
|
|
|
|
table.remove(availableGenerals, randomGeneral)
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if #availableGenerals == 0 then
|
|
|
|
break
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return result
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-03-30 08:33:56 +00:00
|
|
|
---@param except General[]
|
|
|
|
---@return General[]
|
2022-03-28 14:24:30 +00:00
|
|
|
function Engine:getAllGenerals(except)
|
2022-04-30 07:27:56 +00:00
|
|
|
local result = {}
|
|
|
|
for _, general in ipairs(self.generals) do
|
|
|
|
if not (except and table.contains(except, general)) then
|
|
|
|
table.insert(result, general)
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-03-25 04:28:07 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return result
|
2022-03-25 04:28:07 +00:00
|
|
|
end
|
|
|
|
|
2022-04-08 10:39:58 +00:00
|
|
|
---@param except integer[]
|
|
|
|
---@return integer[]
|
|
|
|
function Engine:getAllCardIds(except)
|
2022-04-30 07:27:56 +00:00
|
|
|
local result = {}
|
|
|
|
for _, card in ipairs(self.cards) do
|
|
|
|
if not (except and table.contains(except, card.id)) then
|
|
|
|
table.insert(result, card.id)
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
2022-04-30 07:27:56 +00:00
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
return result
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
2023-02-15 11:54:35 +00:00
|
|
|
local filtered_cards = {}
|
|
|
|
|
2022-04-08 10:39:58 +00:00
|
|
|
---@param id integer
|
2023-02-15 11:54:35 +00:00
|
|
|
---@param ignoreFilter boolean
|
2022-04-08 10:39:58 +00:00
|
|
|
---@return Card
|
2023-02-15 11:54:35 +00:00
|
|
|
function Engine:getCardById(id, ignoreFilter)
|
|
|
|
local ret = self.cards[id]
|
|
|
|
if not ignoreFilter then
|
|
|
|
ret = filtered_cards[id] or self.cards[id]
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param id integer
|
|
|
|
---@param player Player
|
|
|
|
---@param data any @ may be JudgeStruct
|
|
|
|
function Engine:filterCard(id, player, data)
|
|
|
|
local card = self:getCardById(id, true)
|
|
|
|
if player == nil then
|
|
|
|
filtered_cards[id] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local skills = player:getAllSkills()
|
|
|
|
local filters = {}
|
|
|
|
for _, s in ipairs(skills) do
|
|
|
|
if s:isInstanceOf(FilterSkill) then
|
|
|
|
table.insert(filters, s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #filters == 0 then
|
|
|
|
filtered_cards[id] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local modify = false
|
|
|
|
if data and type(data) == "table" and data.card
|
|
|
|
and type(data.card) == "table" and data.card:isInstanceOf(Card) then
|
|
|
|
modify = true
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, f in ipairs(filters) do
|
|
|
|
if f:cardFilter(card) then
|
|
|
|
local _card = f:viewAs(card)
|
|
|
|
_card.id = id
|
|
|
|
_card.skillName = f.name
|
|
|
|
if modify and RoomInstance then
|
|
|
|
if not f.mute then
|
|
|
|
RoomInstance:broadcastSkillInvoke(f.name)
|
|
|
|
end
|
|
|
|
RoomInstance:doAnimate("InvokeSkill", {
|
|
|
|
name = f.name,
|
|
|
|
player = player.id,
|
|
|
|
skill_type = f.anim_type,
|
|
|
|
})
|
|
|
|
RoomInstance:sendLog{
|
|
|
|
type = "#FilterCard",
|
|
|
|
arg = f.name,
|
|
|
|
from = player.id,
|
|
|
|
arg2 = card:toLogString(),
|
|
|
|
arg3 = _card:toLogString(),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
card = _card
|
|
|
|
end
|
|
|
|
if card == nil then
|
|
|
|
card = self:getCardById(id)
|
|
|
|
end
|
|
|
|
filtered_cards[id] = card
|
|
|
|
end
|
|
|
|
|
|
|
|
if modify then
|
|
|
|
filtered_cards[id] = nil
|
|
|
|
data.card = card
|
|
|
|
return
|
|
|
|
end
|
2022-04-08 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
2022-09-14 05:01:10 +00:00
|
|
|
function Engine:currentRoom()
|
2023-02-26 07:01:14 +00:00
|
|
|
if RoomInstance then
|
|
|
|
return RoomInstance
|
2022-09-14 05:01:10 +00:00
|
|
|
end
|
2023-02-26 07:01:14 +00:00
|
|
|
return ClientInstance
|
2022-09-14 05:01:10 +00:00
|
|
|
end
|
|
|
|
|
2023-02-15 11:54:35 +00:00
|
|
|
function Engine:getDescription(name)
|
|
|
|
return self:translate(":" .. name)
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:24:30 +00:00
|
|
|
return Engine
|