From 2ab79a26fb3cf11baf759705bb1dfecc711bafde Mon Sep 17 00:00:00 2001 From: notify Date: Mon, 16 Jan 2023 21:04:28 +0800 Subject: [PATCH] Better trigger (#45) * set gamerule a refresh trigger skill * cost and use of trigger skill --- lua/core/skill_type/trigger.lua | 31 +++++++++++++++++++++++++++++-- lua/fk_ex.lua | 5 +++++ packages/standard/game_rule.lua | 6 +++--- packages/standard/init.lua | 12 +++++------- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/lua/core/skill_type/trigger.lua b/lua/core/skill_type/trigger.lua index 48a4a339..21419590 100644 --- a/lua/core/skill_type/trigger.lua +++ b/lua/core/skill_type/trigger.lua @@ -41,15 +41,42 @@ function TriggerSkill:triggerable(event, target, player, data) and (self.global or (target:isAlive() and target:hasSkill(self))) end ----Trigger this skill +-- Determine how to cost this skill. ---@param event Event @ TriggerEvent ---@param target ServerPlayer @ Player who triggered this event ---@param player ServerPlayer @ Player who is operating ---@param data any @ useful data of the event ---@return boolean @ returns true if trigger is broken function TriggerSkill:trigger(event, target, player, data) + return self:doCost(event, target, player, data) +end + +-- do cost and skill effect. +-- DO NOT modify this function +function TriggerSkill:doCost(event, target, player, data) + local ret = self:cost(event, target, player, data) + if ret then + local room = player.room + room:notifySkillInvoked(player, self.name) + ret = self:use(event, target, player, data) + return ret + end +end + +-- ask player how to use this skill. +---@param event Event @ TriggerEvent +---@param target ServerPlayer @ Player who triggered this event +---@param player ServerPlayer @ Player who is operating +---@param data any @ useful data of the event +---@return boolean @ returns true if trigger is broken +function TriggerSkill:cost(event, target, player, data) + local ret = false + if self.frequency == Skill.Compulsory then + return true + end + if player.room:askForSkillInvoke(player, self.name) then - return self:use(event, target, player, data) + return true end return false end diff --git a/lua/fk_ex.lua b/lua/fk_ex.lua index 233aadd2..5f65a765 100644 --- a/lua/fk_ex.lua +++ b/lua/fk_ex.lua @@ -26,6 +26,8 @@ _, Weapon, Armor, DefensiveRide, OffensiveRide, Treasure = table.unpack(Equip) ---@field priority number | table ---@field on_trigger TrigFunc ---@field can_trigger TrigFunc +---@field on_cost TrigFunc +---@field on_use TrigFunc ---@field on_refresh TrigFunc ---@field can_refresh TrigFunc @@ -59,6 +61,9 @@ function fk.CreateTriggerSkill(spec) skill.triggerable = spec.can_trigger end + if spec.on_cost then skill.cost = spec.on_cost end + if spec.on_use then skill.use = spec.on_use end + if spec.can_refresh then skill.canRefresh = spec.can_refresh end diff --git a/packages/standard/game_rule.lua b/packages/standard/game_rule.lua index 9d26a5af..5b061f0c 100644 --- a/packages/standard/game_rule.lua +++ b/packages/standard/game_rule.lua @@ -38,7 +38,7 @@ end GameRule = fk.CreateTriggerSkill{ name = "game_rule", - events = { + refresh_events = { fk.GameStart, fk.DrawInitialCards, fk.TurnStart, fk.EventPhaseProceeding, fk.EventPhaseEnd, fk.EventPhaseChanging, fk.AskForPeaches, fk.AskForPeachesDone, @@ -46,11 +46,11 @@ GameRule = fk.CreateTriggerSkill{ }, priority = 0, - can_trigger = function(self, event, target, player, data) + can_refresh = function(self, event, target, player, data) return (target == player) or (target == nil) end, - on_trigger = function(self, event, target, player, data) + on_refresh = function(self, event, target, player, data) if RoomInstance.tag["SkipGameRule"] then RoomInstance.tag["SkipGameRule"] = false return false diff --git a/packages/standard/init.lua b/packages/standard/init.lua index 9df6a710..800f97e9 100644 --- a/packages/standard/init.lua +++ b/packages/standard/init.lua @@ -31,16 +31,14 @@ local fankui = fk.CreateTriggerSkill{ local from = room:getPlayerById(data.from) return from ~= nil and target == player and - target:hasSkill(self.name) + target:hasSkill(self.name) and + not target.dead end, - on_trigger = function(self, event, target, player, data) + on_use = function(self, event, target, player, data) local room = player.room local from = room:getPlayerById(data.from) - if (not from) or from:isNude() then return false end - if room:askForSkillInvoke(player, self.name) then - local card = room:askForCardChosen(player, from, "he", self.name) - room:obtainCard(player.id, card, false, fk.ReasonPrey) - end + local card = room:askForCardChosen(player, from, "he", self.name) + room:obtainCard(player.id, card, false) end } local simayi = General:new(extension, "simayi", "wei", 3)