查看: 62|回复: 0

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

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

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

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

×
因为个人需求让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. #
  39. #   在脚本编辑器里「插入」一个新的脚本页,把本脚本粘贴进去,
  40. #   位置放在所有默认脚本之后、▼ 入口(Main)之前。
  41. #==============================================================================

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

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

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

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

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

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

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

  122.     #------------------------------------------------------------------------
  123.     # ● 批量处理
  124.     #     在这段处理全部结束之前,先不附加连锁状态,
  125.     #     以免在「遍历状态数组」的途中改动数组。
  126.     #------------------------------------------------------------------------
  127.     def bulk
  128.       @depth = (@depth || 0) + 1
  129.       begin
  130.         yield
  131.       ensure
  132.         @depth -= 1
  133.         flush if @depth <= 0
  134.       end
  135.     end

  136.     #------------------------------------------------------------------------
  137.     # ● 记录一次待附加的连锁
  138.     #------------------------------------------------------------------------
  139.     def enqueue(battler, ids)
  140.       return if ids.nil? || ids.empty?
  141.       (@queue ||= []).push([battler, ids])
  142.       flush if (@depth || 0) <= 0
  143.     end

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

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

  192.     #------------------------------------------------------------------------
  193.     # ● 解除原因的名称
  194.     #------------------------------------------------------------------------
  195.     def reason_name(reason)
  196.       REASON_NAMES[reason] || reason.to_s
  197.     end

  198.     #------------------------------------------------------------------------
  199.     # ● 调试输出
  200.     #------------------------------------------------------------------------
  201.     def log(text)
  202.       return unless DEBUG
  203.       p("[StateChain] #{text}")
  204.     end
  205.   end
  206. end

  207. #==============================================================================
  208. # ■ Game_Battler
  209. #------------------------------------------------------------------------------
  210. #  记录「解除原因」,并在状态解除后执行连锁。
  211. #==============================================================================

  212. class Game_Battler
  213.   #--------------------------------------------------------------------------
  214.   # ● 当前的解除原因
  215.   #--------------------------------------------------------------------------
  216.   def sc_reason
  217.     @sc_reason
  218.   end

  219.   def sc_reason=(reason)
  220.     @sc_reason = reason
  221.   end

  222.   #--------------------------------------------------------------------------
  223.   # ● 在指定原因下执行一段处理
  224.   #--------------------------------------------------------------------------
  225.   def sc_transaction(reason)
  226.     StateChain.bulk do
  227.       previous = @sc_reason
  228.       @sc_reason = reason
  229.       begin
  230.         yield
  231.       ensure
  232.         @sc_reason = previous
  233.       end
  234.     end
  235.   end

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

  249.   #--------------------------------------------------------------------------
  250.   # ● 战斗结束时的状态解除
  251.   #--------------------------------------------------------------------------
  252.   alias sc_remove_battle_states remove_battle_states
  253.   def remove_battle_states
  254.     sc_transaction(:battle_end) { sc_remove_battle_states }
  255.   end

  256.   #--------------------------------------------------------------------------
  257.   # ● 回合数到期/行动结束时的自动解除
  258.   #--------------------------------------------------------------------------
  259.   alias sc_remove_states_auto remove_states_auto
  260.   def remove_states_auto(timing)
  261.     sc_transaction(:turn) { sc_remove_states_auto(timing) }
  262.   end

  263.   #--------------------------------------------------------------------------
  264.   # ● 受到伤害时的状态解除
  265.   #--------------------------------------------------------------------------
  266.   alias sc_remove_states_by_damage remove_states_by_damage
  267.   def remove_states_by_damage
  268.     sc_transaction(:damage) { sc_remove_states_by_damage }
  269.   end

  270.   #--------------------------------------------------------------------------
  271.   # ● 因行动限制而解除状态
  272.   #--------------------------------------------------------------------------
  273.   alias sc_on_restrict on_restrict
  274.   def on_restrict
  275.     sc_transaction(:restrict) { sc_on_restrict }
  276.   end

  277.   #--------------------------------------------------------------------------
  278.   # ● 物品/技能的「状态解除」效果
  279.   #--------------------------------------------------------------------------
  280.   alias sc_item_effect_remove_state item_effect_remove_state
  281.   def item_effect_remove_state(user, item, effect)
  282.     sc_transaction(:cure) { sc_item_effect_remove_state(user, item, effect) }
  283.   end

  284.   #--------------------------------------------------------------------------
  285.   # ● 应用技能/物品的效果
  286.   #     整个效果处理完再统一附加连锁状态
  287.   #--------------------------------------------------------------------------
  288.   alias sc_item_apply item_apply
  289.   def item_apply(user, item)
  290.     StateChain.bulk { sc_item_apply(user, item) }
  291.   end
  292. end

  293. #==============================================================================
  294. # ■ Game_Actor
  295. #------------------------------------------------------------------------------
  296. #  处理地图上「步数到期」的解除。
  297. #==============================================================================

  298. class Game_Actor
  299.   #--------------------------------------------------------------------------
  300.   # ● 更新状态的步数计数
  301.   #--------------------------------------------------------------------------
  302.   alias sc_update_state_steps update_state_steps
  303.   def update_state_steps(state)
  304.     sc_transaction(:step) { sc_update_state_steps(state) }
  305.   end

  306.   #--------------------------------------------------------------------------
  307.   # ● 角色在地图上行走一步时的处理
  308.   #     等这一整套处理结束再附加连锁状态
  309.   #--------------------------------------------------------------------------
  310.   alias sc_on_player_walk on_player_walk
  311.   def on_player_walk
  312.     StateChain.bulk { sc_on_player_walk }
  313.   end

  314.   #--------------------------------------------------------------------------
  315.   # ● 显示被附加的状态
  316.   #     先把连锁状态真正加上,再显示消息
  317.   #--------------------------------------------------------------------------
  318.   alias sc_show_added_states show_added_states
  319.   def show_added_states
  320.     StateChain.flush
  321.     sc_show_added_states
  322.   end
  323. end
复制代码

效果演示

效果演示

评分

参与人数 1赞 +1 收起 理由
智将Jesse + 1 我很赞同

查看全部评分

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

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

在本版发帖返回顶部