查看: 77|回复: 0

[原创发布] [脚本] 当一个状态结束后赋予另一个状态

[复制链接]
梦石
0
星屑
199
在线时间
20 小时
注册时间
2025-8-9
回帖
0
发表于 2026-11-5 15:48:24 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
因为个人需求让AI写了一个这种脚本,考虑到可能会有其他需要的人遂发布
使用方法见注释,可以判断因为什么条件结束而决定是否赋予下一个状态

  1. # encoding: utf-8
  2. #==============================================================================
  3. # ■ 状态连锁(State Chain)
  4. #------------------------------------------------------------------------------
  5. #  让一个状态在解除时,自动为同一个角色/敌人附加另一个状态。
  6. #
  7. #  ◆ 备注写法(写在备注栏里)
  8. #
  9. #    <Next:2>              该状态解除后,附加 2 号状态
  10. #    <Next:2> <Type:1>     只有满足 Type 指定的解除原因时才附加
  11. #
  12. #   <Type> 省略时等同于 <Type:0>。
  13. #   <Next> 可以写多个 ID,用逗号隔开,例如 <Next:2,3,4>。
  14. #
  15. #  ◆ Type调用:
  16. #
  17. #   0 任何方式解除都连锁(默认)
  18. #   1 战斗结束解除 / 回合数到期解除 / 步数到期解除
  19. #   2 受到伤害解除 / 因行动限制解除
  20. #   3 使用物品或技能治愈(「状态解除」效果)
  21. #
  22. #  ◆ 注意
  23. #
  24. #   · <Type:1>~<Type:3> 只是「连锁的触发条件」,
  25. #    状态本身何时解除,仍由数据库里的勾选项决定。
  26. #    例如要触发「受到伤害解除」,需要勾选「受到伤害时解除」;
  27. #    要触发「因行动限制解除」,需要勾选「因行动限制而解除」。
  28. #
  29. #   · 战斗不能、撤退、完全恢复会直接清空状态表,
  30. #    不经过逐个解除,因此不会触发连锁。
  31. #
  32. #   · 目标状态能否附加成功,仍受「状态有效度」等抗性影响;
  33. #    角色处于战斗不能状态时也不会附加。
  34. #
  35. #   · 连锁不会在同一个状态上无限循环(有次数上限保护)。
  36. #
  37. #==============================================================================

  38. module StateChain
  39.   #--------------------------------------------------------------------------
  40.   # ● 备注标签
  41.   #--------------------------------------------------------------------------
  42.   NEXT_PATTERN = /<\s*next\s*:\s*([0-9]+(?:\s*[,,、]\s*[0-9]+)*)\s*>/i
  43.   TYPE_PATTERN = /<\s*type\s*:\s*([0-9]+)\s*>/i

  44.   #--------------------------------------------------------------------------
  45.   # ● 解除原因的名称(仅用于调试输出)
  46.   #--------------------------------------------------------------------------
  47.   REASON_NAMES = {
  48.     :battle_end => "战斗结束",
  49.     :turn       => "回合到期",
  50.     :step       => "步数到期",
  51.     :damage     => "受到伤害",
  52.     :restrict   => "行动限制",
  53.     :cure       => "物品/技能治愈",
  54.     :other      => "其他方式",
  55.   }

  56.   #--------------------------------------------------------------------------
  57.   # ● 每种 Type 允许的解除原因(nil 表示不限制)
  58.   #--------------------------------------------------------------------------
  59.   TRIGGERS = {
  60.     0 => nil,
  61.     1 => [:battle_end, :turn, :step],
  62.     2 => [:damage, :restrict],
  63.     3 => [:cure],
  64.   }

  65.   #--------------------------------------------------------------------------
  66.   # ● 设定
  67.   #--------------------------------------------------------------------------
  68.   # 目标状态已经存在时,是否重新计算持续时间
  69.   REFRESH_EXISTING = false
  70.   # 角色处于战斗不能状态时,是否仍然附加
  71.   APPLY_WHEN_DEAD = false
  72.   # 一次连锁最多处理多少个状态(防止 A → B → A 的死循环)
  73.   MAX_TRANSFER = 32
  74.   # 是否在控制台输出调试信息
  75.   DEBUG = false

  76.   class << self
  77.     #------------------------------------------------------------------------
  78.     # ● 取得状态的连锁设定
  79.     #     返回值:[目标状态 ID 数组, Type],没有设定时返回 nil
  80.     #------------------------------------------------------------------------
  81.     def info(state_id)
  82.       state = $data_states[state_id]
  83.       return nil unless state
  84.       @cache ||= {}
  85.       entry = @cache[state_id]
  86.       return entry[1] if entry && entry[0].equal?(state)
  87.       parsed = parse_note(state)
  88.       @cache[state_id] = [state, parsed]
  89.       parsed
  90.     end

  91.     #------------------------------------------------------------------------
  92.     # ● 解析备注
  93.     #------------------------------------------------------------------------
  94.     def parse_note(state)
  95.       note = state.note.to_s
  96.       matched = NEXT_PATTERN.match(note)
  97.       return nil unless matched
  98.       ids = []
  99.       matched[1].scan(/[0-9]+/) {|num| ids.push(num.to_i) }
  100.       ids = ids.uniq
  101.       ids.delete_if {|id| id <= 0 || $data_states[id].nil? }
  102.       return nil if ids.empty?
  103.       type_matched = TYPE_PATTERN.match(note)
  104.       type = type_matched ? type_matched[1].to_i : 0
  105.       [ids, type]
  106.     end

  107.     #------------------------------------------------------------------------
  108.     # ● 判断这次解除是否应该连锁,返回目标状态 ID 数组
  109.     #------------------------------------------------------------------------
  110.     def chain_for(state_id, reason)
  111.       data = info(state_id)
  112.       return nil unless data
  113.       ids, type = data
  114.       allowed = TRIGGERS.key?(type) ? TRIGGERS[type] : TRIGGERS[0]
  115.       return nil if allowed && !allowed.include?(reason)
  116.       ids
  117.     end

  118.     #------------------------------------------------------------------------
  119.     # ● 批量处理
  120.     #------------------------------------------------------------------------
  121.     def bulk
  122.       @depth = (@depth || 0) + 1
  123.       begin
  124.         yield
  125.       ensure
  126.         @depth -= 1
  127.         flush if @depth <= 0
  128.       end
  129.     end

  130.     #------------------------------------------------------------------------
  131.     # ● 记录一次待附加的连锁
  132.     #------------------------------------------------------------------------
  133.     def enqueue(battler, ids)
  134.       return if ids.nil? || ids.empty?
  135.       (@queue ||= []).push([battler, ids])
  136.       flush if (@depth || 0) <= 0
  137.     end

  138.     #------------------------------------------------------------------------
  139.     # ● 执行所有待附加的连锁
  140.     #------------------------------------------------------------------------
  141.     def flush
  142.       return if @flushing
  143.       queue = (@queue ||= [])
  144.       return if queue.empty?
  145.       @flushing = true
  146.       begin
  147.         count = 0
  148.         until queue.empty?
  149.           battler, ids = queue.shift
  150.           count += 1
  151.           if count > MAX_TRANSFER
  152.             log("连锁次数超过 #{MAX_TRANSFER},已强制中止")
  153.             queue.clear
  154.             break
  155.           end
  156.           apply(battler, ids)
  157.         end
  158.       ensure
  159.         @flushing = false
  160.       end
  161.     end

  162.     #------------------------------------------------------------------------
  163.     # ● 对某个战斗者附加连锁状态
  164.     #------------------------------------------------------------------------
  165.     def apply(battler, ids)
  166.       return if battler.nil?
  167.       ids.each do |state_id|
  168.         state = $data_states[state_id]
  169.         next if state.nil?
  170.         if battler.dead? && !APPLY_WHEN_DEAD
  171.           log("角色已战斗不能,跳过状态 #{state_id}")
  172.           next
  173.         end
  174.         if battler.state?(state_id) && !REFRESH_EXISTING
  175.           log("角色已拥有状态 #{state_id},跳过")
  176.           next
  177.         end
  178.         battler.add_state(state_id)
  179.         if battler.state?(state_id)
  180.           log("附加状态 #{state_id}「#{state.name}」")
  181.         else
  182.           log("状态 #{state_id}「#{state.name}」附加失败(抗性/限制)")
  183.         end
  184.       end
  185.     end

  186.     #------------------------------------------------------------------------
  187.     # ● 解除原因的名称
  188.     #------------------------------------------------------------------------
  189.     def reason_name(reason)
  190.       REASON_NAMES[reason] || reason.to_s
  191.     end

  192.     #------------------------------------------------------------------------
  193.     # ● 调试输出
  194.     #------------------------------------------------------------------------
  195.     def log(text)
  196.       return unless DEBUG
  197.       p("[StateChain] #{text}")
  198.     end
  199.   end
  200. end

  201. #==============================================================================
  202. # ■ Game_Battler
  203. #------------------------------------------------------------------------------
  204. #  记录「解除原因」,并在状态解除后执行连锁。
  205. #==============================================================================

  206. class Game_Battler
  207.   #--------------------------------------------------------------------------
  208.   # ● 当前的解除原因
  209.   #--------------------------------------------------------------------------
  210.   def sc_reason
  211.     @sc_reason
  212.   end

  213.   def sc_reason=(reason)
  214.     @sc_reason = reason
  215.   end

  216.   #--------------------------------------------------------------------------
  217.   # ● 在指定原因下执行一段处理
  218.   #--------------------------------------------------------------------------
  219.   def sc_transaction(reason)
  220.     StateChain.bulk do
  221.       previous = @sc_reason
  222.       @sc_reason = reason
  223.       begin
  224.         yield
  225.       ensure
  226.         @sc_reason = previous
  227.       end
  228.     end
  229.   end

  230.   #--------------------------------------------------------------------------
  231.   # ● 解除状态
  232.   #--------------------------------------------------------------------------
  233.   alias sc_remove_state remove_state
  234.   def remove_state(state_id)
  235.     return unless state?(state_id)
  236.     reason = sc_reason || :other
  237.     chain = StateChain.chain_for(state_id, reason)
  238.     StateChain.log("解除状态 #{state_id}(#{StateChain.reason_name(reason)})" +
  239.                    "→ 连锁 #{chain.inspect}") if chain
  240.     sc_remove_state(state_id)
  241.     StateChain.enqueue(self, chain) if chain
  242.   end

  243.   #--------------------------------------------------------------------------
  244.   # ● 战斗结束时的状态解除
  245.   #--------------------------------------------------------------------------
  246.   alias sc_remove_battle_states remove_battle_states
  247.   def remove_battle_states
  248.     sc_transaction(:battle_end) { sc_remove_battle_states }
  249.   end

  250.   #--------------------------------------------------------------------------
  251.   # ● 回合数到期/行动结束时的自动解除
  252.   #--------------------------------------------------------------------------
  253.   alias sc_remove_states_auto remove_states_auto
  254.   def remove_states_auto(timing)
  255.     sc_transaction(:turn) { sc_remove_states_auto(timing) }
  256.   end

  257.   #--------------------------------------------------------------------------
  258.   # ● 受到伤害时的状态解除
  259.   #--------------------------------------------------------------------------
  260.   alias sc_remove_states_by_damage remove_states_by_damage
  261.   def remove_states_by_damage
  262.     sc_transaction(:damage) { sc_remove_states_by_damage }
  263.   end

  264.   #--------------------------------------------------------------------------
  265.   # ● 因行动限制而解除状态
  266.   #--------------------------------------------------------------------------
  267.   alias sc_on_restrict on_restrict
  268.   def on_restrict
  269.     sc_transaction(:restrict) { sc_on_restrict }
  270.   end

  271.   #--------------------------------------------------------------------------
  272.   # ● 物品/技能的「状态解除」效果
  273.   #--------------------------------------------------------------------------
  274.   alias sc_item_effect_remove_state item_effect_remove_state
  275.   def item_effect_remove_state(user, item, effect)
  276.     sc_transaction(:cure) { sc_item_effect_remove_state(user, item, effect) }
  277.   end

  278.   #--------------------------------------------------------------------------
  279.   # ● 应用技能/物品的效果
  280.   #--------------------------------------------------------------------------
  281.   alias sc_item_apply item_apply
  282.   def item_apply(user, item)
  283.     StateChain.bulk { sc_item_apply(user, item) }
  284.   end
  285. end

  286. #==============================================================================
  287. # ■ Game_Actor
  288. #------------------------------------------------------------------------------
  289. #  处理地图上「步数到期」的解除。
  290. #==============================================================================

  291. class Game_Actor
  292.   #--------------------------------------------------------------------------
  293.   # ● 更新状态的步数计数
  294.   #--------------------------------------------------------------------------
  295.   alias sc_update_state_steps update_state_steps
  296.   def update_state_steps(state)
  297.     sc_transaction(:step) { sc_update_state_steps(state) }
  298.   end

  299.   #--------------------------------------------------------------------------
  300.   # ● 角色在地图上行走一步时的处理
  301.   #--------------------------------------------------------------------------
  302.   alias sc_on_player_walk on_player_walk
  303.   def on_player_walk
  304.     StateChain.bulk { sc_on_player_walk }
  305.   end

  306.   #--------------------------------------------------------------------------
  307.   # ● 显示被附加的状态
  308.   #--------------------------------------------------------------------------
  309.   alias sc_show_added_states show_added_states
  310.   def show_added_states
  311.     StateChain.flush
  312.     sc_show_added_states
  313.   end
  314. end
复制代码
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部