| 赞 | 1 |
| VIP | |
| 好人卡 | |
| 积分 | 2 |
| 经验 | |
| 最后登录 | 2026-11-20 |
| 在线时间 | 20 小时 |
- 梦石
- 0
- 星屑
- 199
- 在线时间
- 20 小时
- 注册时间
- 2025-8-9
- 回帖
- 0
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
因为个人需求让AI写了一个这种脚本,考虑到可能会有其他需要的人遂发布
使用方法见注释,可以判断因为什么条件结束而决定是否赋予下一个状态
- # encoding: utf-8
- #==============================================================================
- # ■ 状态连锁(State Chain)
- #------------------------------------------------------------------------------
- # 让一个状态在解除时,自动为同一个角色/敌人附加另一个状态。
- #
- # ◆ 备注写法(写在备注栏里)
- #
- # <Next:2> 该状态解除后,附加 2 号状态
- # <Next:2> <Type:1> 只有满足 Type 指定的解除原因时才附加
- #
- # <Type> 省略时等同于 <Type:0>。
- # <Next> 可以写多个 ID,用逗号隔开,例如 <Next:2,3,4>。
- #
- # ◆ Type调用:
- #
- # 0 任何方式解除都连锁(默认)
- # 1 战斗结束解除 / 回合数到期解除 / 步数到期解除
- # 2 受到伤害解除 / 因行动限制解除
- # 3 使用物品或技能治愈(「状态解除」效果)
- #
- # ◆ 注意
- #
- # · <Type:1>~<Type:3> 只是「连锁的触发条件」,
- # 状态本身何时解除,仍由数据库里的勾选项决定。
- # 例如要触发「受到伤害解除」,需要勾选「受到伤害时解除」;
- # 要触发「因行动限制解除」,需要勾选「因行动限制而解除」。
- #
- # · 战斗不能、撤退、完全恢复会直接清空状态表,
- # 不经过逐个解除,因此不会触发连锁。
- #
- # · 目标状态能否附加成功,仍受「状态有效度」等抗性影响;
- # 角色处于战斗不能状态时也不会附加。
- #
- # · 连锁不会在同一个状态上无限循环(有次数上限保护)。
- #
- #==============================================================================
- module StateChain
- #--------------------------------------------------------------------------
- # ● 备注标签
- #--------------------------------------------------------------------------
- NEXT_PATTERN = /<\s*next\s*:\s*([0-9]+(?:\s*[,,、]\s*[0-9]+)*)\s*>/i
- TYPE_PATTERN = /<\s*type\s*:\s*([0-9]+)\s*>/i
- #--------------------------------------------------------------------------
- # ● 解除原因的名称(仅用于调试输出)
- #--------------------------------------------------------------------------
- REASON_NAMES = {
- :battle_end => "战斗结束",
- :turn => "回合到期",
- :step => "步数到期",
- :damage => "受到伤害",
- :restrict => "行动限制",
- :cure => "物品/技能治愈",
- :other => "其他方式",
- }
- #--------------------------------------------------------------------------
- # ● 每种 Type 允许的解除原因(nil 表示不限制)
- #--------------------------------------------------------------------------
- TRIGGERS = {
- 0 => nil,
- 1 => [:battle_end, :turn, :step],
- 2 => [:damage, :restrict],
- 3 => [:cure],
- }
- #--------------------------------------------------------------------------
- # ● 设定
- #--------------------------------------------------------------------------
- # 目标状态已经存在时,是否重新计算持续时间
- REFRESH_EXISTING = false
- # 角色处于战斗不能状态时,是否仍然附加
- APPLY_WHEN_DEAD = false
- # 一次连锁最多处理多少个状态(防止 A → B → A 的死循环)
- MAX_TRANSFER = 32
- # 是否在控制台输出调试信息
- DEBUG = false
- class << self
- #------------------------------------------------------------------------
- # ● 取得状态的连锁设定
- # 返回值:[目标状态 ID 数组, Type],没有设定时返回 nil
- #------------------------------------------------------------------------
- def info(state_id)
- state = $data_states[state_id]
- return nil unless state
- @cache ||= {}
- entry = @cache[state_id]
- return entry[1] if entry && entry[0].equal?(state)
- parsed = parse_note(state)
- @cache[state_id] = [state, parsed]
- parsed
- end
- #------------------------------------------------------------------------
- # ● 解析备注
- #------------------------------------------------------------------------
- def parse_note(state)
- note = state.note.to_s
- matched = NEXT_PATTERN.match(note)
- return nil unless matched
- ids = []
- matched[1].scan(/[0-9]+/) {|num| ids.push(num.to_i) }
- ids = ids.uniq
- ids.delete_if {|id| id <= 0 || $data_states[id].nil? }
- return nil if ids.empty?
- type_matched = TYPE_PATTERN.match(note)
- type = type_matched ? type_matched[1].to_i : 0
- [ids, type]
- end
- #------------------------------------------------------------------------
- # ● 判断这次解除是否应该连锁,返回目标状态 ID 数组
- #------------------------------------------------------------------------
- def chain_for(state_id, reason)
- data = info(state_id)
- return nil unless data
- ids, type = data
- allowed = TRIGGERS.key?(type) ? TRIGGERS[type] : TRIGGERS[0]
- return nil if allowed && !allowed.include?(reason)
- ids
- end
- #------------------------------------------------------------------------
- # ● 批量处理
- #------------------------------------------------------------------------
- def bulk
- @depth = (@depth || 0) + 1
- begin
- yield
- ensure
- @depth -= 1
- flush if @depth <= 0
- end
- end
- #------------------------------------------------------------------------
- # ● 记录一次待附加的连锁
- #------------------------------------------------------------------------
- def enqueue(battler, ids)
- return if ids.nil? || ids.empty?
- (@queue ||= []).push([battler, ids])
- flush if (@depth || 0) <= 0
- end
- #------------------------------------------------------------------------
- # ● 执行所有待附加的连锁
- #------------------------------------------------------------------------
- def flush
- return if @flushing
- queue = (@queue ||= [])
- return if queue.empty?
- @flushing = true
- begin
- count = 0
- until queue.empty?
- battler, ids = queue.shift
- count += 1
- if count > MAX_TRANSFER
- log("连锁次数超过 #{MAX_TRANSFER},已强制中止")
- queue.clear
- break
- end
- apply(battler, ids)
- end
- ensure
- @flushing = false
- end
- end
- #------------------------------------------------------------------------
- # ● 对某个战斗者附加连锁状态
- #------------------------------------------------------------------------
- def apply(battler, ids)
- return if battler.nil?
- ids.each do |state_id|
- state = $data_states[state_id]
- next if state.nil?
- if battler.dead? && !APPLY_WHEN_DEAD
- log("角色已战斗不能,跳过状态 #{state_id}")
- next
- end
- if battler.state?(state_id) && !REFRESH_EXISTING
- log("角色已拥有状态 #{state_id},跳过")
- next
- end
- battler.add_state(state_id)
- if battler.state?(state_id)
- log("附加状态 #{state_id}「#{state.name}」")
- else
- log("状态 #{state_id}「#{state.name}」附加失败(抗性/限制)")
- end
- end
- end
- #------------------------------------------------------------------------
- # ● 解除原因的名称
- #------------------------------------------------------------------------
- def reason_name(reason)
- REASON_NAMES[reason] || reason.to_s
- end
- #------------------------------------------------------------------------
- # ● 调试输出
- #------------------------------------------------------------------------
- def log(text)
- return unless DEBUG
- p("[StateChain] #{text}")
- end
- end
- end
- #==============================================================================
- # ■ Game_Battler
- #------------------------------------------------------------------------------
- # 记录「解除原因」,并在状态解除后执行连锁。
- #==============================================================================
- class Game_Battler
- #--------------------------------------------------------------------------
- # ● 当前的解除原因
- #--------------------------------------------------------------------------
- def sc_reason
- @sc_reason
- end
- def sc_reason=(reason)
- @sc_reason = reason
- end
- #--------------------------------------------------------------------------
- # ● 在指定原因下执行一段处理
- #--------------------------------------------------------------------------
- def sc_transaction(reason)
- StateChain.bulk do
- previous = @sc_reason
- @sc_reason = reason
- begin
- yield
- ensure
- @sc_reason = previous
- end
- end
- end
- #--------------------------------------------------------------------------
- # ● 解除状态
- #--------------------------------------------------------------------------
- alias sc_remove_state remove_state
- def remove_state(state_id)
- return unless state?(state_id)
- reason = sc_reason || :other
- chain = StateChain.chain_for(state_id, reason)
- StateChain.log("解除状态 #{state_id}(#{StateChain.reason_name(reason)})" +
- "→ 连锁 #{chain.inspect}") if chain
- sc_remove_state(state_id)
- StateChain.enqueue(self, chain) if chain
- end
- #--------------------------------------------------------------------------
- # ● 战斗结束时的状态解除
- #--------------------------------------------------------------------------
- alias sc_remove_battle_states remove_battle_states
- def remove_battle_states
- sc_transaction(:battle_end) { sc_remove_battle_states }
- end
- #--------------------------------------------------------------------------
- # ● 回合数到期/行动结束时的自动解除
- #--------------------------------------------------------------------------
- alias sc_remove_states_auto remove_states_auto
- def remove_states_auto(timing)
- sc_transaction(:turn) { sc_remove_states_auto(timing) }
- end
- #--------------------------------------------------------------------------
- # ● 受到伤害时的状态解除
- #--------------------------------------------------------------------------
- alias sc_remove_states_by_damage remove_states_by_damage
- def remove_states_by_damage
- sc_transaction(:damage) { sc_remove_states_by_damage }
- end
- #--------------------------------------------------------------------------
- # ● 因行动限制而解除状态
- #--------------------------------------------------------------------------
- alias sc_on_restrict on_restrict
- def on_restrict
- sc_transaction(:restrict) { sc_on_restrict }
- end
- #--------------------------------------------------------------------------
- # ● 物品/技能的「状态解除」效果
- #--------------------------------------------------------------------------
- alias sc_item_effect_remove_state item_effect_remove_state
- def item_effect_remove_state(user, item, effect)
- sc_transaction(:cure) { sc_item_effect_remove_state(user, item, effect) }
- end
- #--------------------------------------------------------------------------
- # ● 应用技能/物品的效果
- #--------------------------------------------------------------------------
- alias sc_item_apply item_apply
- def item_apply(user, item)
- StateChain.bulk { sc_item_apply(user, item) }
- end
- end
- #==============================================================================
- # ■ Game_Actor
- #------------------------------------------------------------------------------
- # 处理地图上「步数到期」的解除。
- #==============================================================================
- class Game_Actor
- #--------------------------------------------------------------------------
- # ● 更新状态的步数计数
- #--------------------------------------------------------------------------
- alias sc_update_state_steps update_state_steps
- def update_state_steps(state)
- sc_transaction(:step) { sc_update_state_steps(state) }
- end
- #--------------------------------------------------------------------------
- # ● 角色在地图上行走一步时的处理
- #--------------------------------------------------------------------------
- alias sc_on_player_walk on_player_walk
- def on_player_walk
- StateChain.bulk { sc_on_player_walk }
- end
- #--------------------------------------------------------------------------
- # ● 显示被附加的状态
- #--------------------------------------------------------------------------
- alias sc_show_added_states show_added_states
- def show_added_states
- StateChain.flush
- sc_show_added_states
- end
- end
复制代码 |
|