# ▼▲▼ XRXS65. CP制御ターンシステム ver.β ▼▲▼ built 201120
# by 桜雅 在土

#==============================================================================
# □ カスタマイズポイント
#==============================================================================
module XRXS65
	#
	# 「バトルスピード」(数値が高いほど早い)  战斗速度,数值越高越快
	#
	SPEED = 1.0
	#
	# 戦闘開始時 CP。 固定値と占有率
	#
	CP_PRESET_FIXNUM = 0
	CP_PRESET_RATIO  = 2.0
	#
	# ターンコントローラ (nil  : カウント/ターンを有効。
	#                     数値 : そのインデックスをもつエネミーが支配)
	#
	TC = 0
	#
	# カウント/ターン (TCが有効な場合は無視)
	#
	CPT = 40
	#
	# CP スキン
	#
	SKIN        = "123"  # スキンファイル名(Graphics/Windowskinsフォルダ)
	LINE_HEIGHT =  6        # スキンの"一行"の縦幅[単位:ピクセル]
	#
	# 表示位置セッティング
	#
	X_OFFSET = 144    # 横位置
	Y_OFFSET = 464    # 縦位置
	ALIGN    =   1    #「位置揃え」(CPメーターの位置。0:左寄せ 1:中央 2:右寄せ)
	MAX      =   4    # 確保するサイズ[単位:~人分]
	#
	# アクターコマンドがポップしたときの効果音
	#
	COMMAND_UP_SE = "Audio/SE/Decision2"
  CPADD_SKILLLIST = {194=>5660,190=>12767 } #新增#用于设定技能的cp跳条值 格式:技能id => cp跳条值
end
#==============================================================================
# --- CP メーターの描画情報の取得 --- (Game_Battlerにインクルードされます)
#==============================================================================
module XRXS_CP
	#--------------------------------------------------------------------------
	# ○ スキンライン (スキンの何行目を使うか)
	#--------------------------------------------------------------------------
	def cp_linetype
		# CP フルの場合はスキンの 2 行目を使う
		return 2 if self.cp_full?
		# 通常はスキンの 1 行目を使う
		return 1
	end
	#--------------------------------------------------------------------------
	# ○ メーター量[単位:%]
	#--------------------------------------------------------------------------
	def cp_lineamount
		# 戦闘不能の場合は 0 %として表示させる
		return 0 if self.dead?
		# CP値を%値に変換して返却する
		return 100 * self.cp / self.max_cp
	end
end
#
# カスタマイズポイントここまで。
#------------------------------------------------------------------------------



#==============================================================================
# --- XRXS. CP機構 ---
#==============================================================================
module XRXS_CP_SYSTEM
	#----------------------------------------------------------------------------
	# ○ 合計 AGI の取得
	#----------------------------------------------------------------------------
	def self.total_agi
		total = 0
		for battler in $game_party.actors + $game_troop.enemies
			total += battler.agi
		end
		return total
	end
end
#==============================================================================
# --- バトラーにCP機能を追加 モジュール ---
#==============================================================================
module XRXS_CP
	#--------------------------------------------------------------------------
	# ○ 最大 CP の取得
	#--------------------------------------------------------------------------
	def max_cp
		return 65535
	end
	#--------------------------------------------------------------------------
	# ○ CP の取得と設定
	#--------------------------------------------------------------------------
	def cp
		return @cp == nil ? @cp = 0 : @cp
	end
	def cp=(n)
		@cp = [[n.to_i, 0].max, self.max_cp].min
	end
	#--------------------------------------------------------------------------
	# ○ CP 初期設定
	#--------------------------------------------------------------------------
	def cp_preset
		percent = self.max_cp * XRXS65::CP_PRESET_RATIO * (rand(16) + 16) * self.agi / XRXS_CP_SYSTEM.total_agi / 24
		self.cp = XRXS65::CP_PRESET_FIXNUM + percent
	end
	#--------------------------------------------------------------------------
	# ○ CP カウントアップ
	#--------------------------------------------------------------------------
	def cp_update
		self.cp += XRXS65::SPEED * 4096 * self.agi / XRXS_CP_SYSTEM.total_agi
	end
	#--------------------------------------------------------------------------
	# ○ CP 満タン?CP满值
	#--------------------------------------------------------------------------
	def cp_full?
		return @cp == self.max_cp
	end
end
class Game_Battler
	include XRXS_CP
end
#==============================================================================
# --- ガード機能 ---
#==============================================================================
class Game_Battler
	#--------------------------------------------------------------------------
	# ○ ガードフラグ
	#--------------------------------------------------------------------------
	def guarding=(n)
		@guarding = n
	end
	#--------------------------------------------------------------------------
	# ● 防御中判定 [再定義]
	#--------------------------------------------------------------------------
	def guarding?
		return @guarding
	end
end
#==============================================================================
# --- アクター「コマンド入力可能判定」:CPがないとコマンドしない ---
#==============================================================================
module XRXS_CP_INPUTABLE
	def inputable?
		return (self.cp_full? and super)
	end
  
  def inputable_zcl_fix?
    return (self.cp_full? and exist?)
  end
end
class Game_Actor < Game_Battler
	include XRXS_CP_INPUTABLE
end
#==============================================================================
# --- エネミー「行動可能判定」:CPがないとコマンドしない ---
#==============================================================================
module XRXS_CP_MOVABLE
	def movable?
		return (self.cp_full? and super)
	end
end
class Game_Enemy < Game_Battler
	include XRXS_CP_MOVABLE
end
#==============================================================================
# --- 戦闘時 CPカウント ---
#==============================================================================
module XRXS_CP_Battle
	#--------------------------------------------------------------------------
	# ○ パーティ全員の CP を初期設定
	#--------------------------------------------------------------------------
	def cp_preset_party
		#p $game_party.actors.size
		i = 0
		for actor in $game_party.actors
			actor.cp_preset
			if actor.cp_full?
				if i == 0
					i += 1
				else          
					actor.cp -= 1          
				end
			end
		end
	end
	#--------------------------------------------------------------------------
	# ○ トループ全員の CP を初期設定
	#--------------------------------------------------------------------------
	def cp_preset_troop
		for enemy in $game_troop.enemies
			enemy.cp_preset
		end
	end
	#--------------------------------------------------------------------------
	# ○ バトラー全員の CP をカウントアップ
	#--------------------------------------------------------------------------
	def cp_update
		#p $game_party.actors.size
		#for battler in $game_party.actors + $game_troop.enemies
		#  battler.cp_update
		#end
		i = 0
		for battler in $game_party.actors + $game_troop.enemies
			battler.cp_update
			if battler.cp_full?
				if i == 0
					i += 1
				else
					battler.cp -= 1
				end
			end
		end
		@fuck_helper.cp_update if @fuck_helper != nil
	end
end
class Scene_Battle
	include XRXS_CP_Battle
end
#==============================================================================
# ■ Scene_Battle
#==============================================================================
class Scene_Battle
	#--------------------------------------------------------------------------
	# ● メイン処理
	#--------------------------------------------------------------------------
	alias xrxs65_main main
	def main
		# エクストラスプライトの初期化
		@extra_sprites = [] if @extra_sprites == nil
		# CP の初期化
		cp_preset_party
		# CP メーターの作成
		@cp_meters = CP_Meters.new
		# CP メーターをエクストラスプライトへ登録
		@extra_sprites.push(@cp_meters)
		# 呼び戻す
		#    p $game_party.actors.size
		xrxs65_main
		# メーターの解放
		@cp_meters.dispose
	end
	#--------------------------------------------------------------------------
	# ● プレバトルフェーズ開始
	#--------------------------------------------------------------------------
	alias xrxs65_start_phase1 start_phase1
	def start_phase1
		# 呼び戻す
		xrxs65_start_phase1
		# CP の初期化
		cp_preset_troop
		# CP メーターの更新
		@cp_meters.refresh
		# インデックスを計算
		@cp_escape_actor_command_index = @actor_command_window.height/32 - 1
		# アクターコマンドウィンドウに追加
		#@actor_command_window.add_command("逃跑")
		#    if !$game_temp.battle_can_escape
		#      @actor_command_window.disable_item(@cp_escape_actor_command_index)
		#    end
	end
	#--------------------------------------------------------------------------
	# ● パーティコマンドフェーズ開始
	#--------------------------------------------------------------------------
	alias xrxs65_start_phase2 start_phase2
	def start_phase2
		# 呼び戻す
		xrxs65_start_phase2
		# パーティコマンドウィンドウを無効化
		@party_command_window.active  = false
		@party_command_window.visible = false
		# 強制的にフェイズ 2 を保持
		
		@phase = 2
		# ただし、既に行動可能者が存在する場合は 3 へ
		start_phase3 if anybody_movable?
	end
	#--------------------------------------------------------------------------
	# ○ CP制での ターンのカウント
	#--------------------------------------------------------------------------
	def cp_turn_count
		$game_temp.battle_turn += 1
		# バトルイベントの全ページを検索
		for index in 0...$data_troops[@troop_id].pages.size
			# このページのスパンが [ターン] の場合
			if $data_troops[@troop_id].pages[index].span == 1
				# 実行済みフラグをクリア
				$game_temp.battle_event_flags[index] = false
			end
		end
	end
	#--------------------------------------------------------------------------
	# ● フレーム更新 (パーティコマンドフェーズ)
	#--------------------------------------------------------------------------
	alias xrxs65_update_phase2 update_phase2
	def update_phase2
		# パーティコマンドウィンドウのインデックスを無効化
		@party_command_window.index = -1
		# 呼び戻す
		xrxs65_update_phase2
		# 例外補正
		@turn_count_time = 1 if @turn_count_time == nil
		# ターンのカウント
		if @turn_count_time > 0
			@turn_count_time -= 1
			if @turn_count_time == 0
				cp_turn_count
				@turn_count_time = XRXS65::CPT if XRXS65::TC == nil
			end
		end
		# CP のフレーム更新
		cp_update
		@cp_meters.refresh
		# フル CP のバトラーが存在する場合、ターン開始
		start_phase3 if anybody_movable?
	end
	#--------------------------------------------------------------------------
	# ○ フル CP バトラーが存在するか?
	#--------------------------------------------------------------------------
	def anybody_movable?
		for battler in $game_party.actors + $game_troop.enemies
			return true if battler.cp_full?
		end
		#p "Full" if @fuck_helper.cp_full?
		if @fuck_helper != nil
			return true if @fuck_helper.cp_full?
		end
		return false
	end
	#--------------------------------------------------------------------------
	# ● アクターコマンドウィンドウのセットアップ
	#--------------------------------------------------------------------------
	alias xrxs65_phase3_setup_command_window phase3_setup_command_window
	def phase3_setup_command_window
		# 効果音の再生
		Audio.se_play(XRXS65::COMMAND_UP_SE) rescue nil
		# 呼び戻す
		xrxs65_phase3_setup_command_window
	end
	#--------------------------------------------------------------------------
	# ● フレーム更新 (アクターコマンドフェーズ : 基本コマンド)
	#--------------------------------------------------------------------------
	alias xrxs_bsp1_update_phase3_basic_command update_phase3_basic_command
	def update_phase3_basic_command
    if Input.trigger?(Input::B) then return end
		# C ボタンが押された場合
		if Input.trigger?(Input::C)
			# アクターコマンドウィンドウのカーソル位置で分岐
			case @actor_command_window.index
			when @cp_escape_actor_command_index # 逃げる
				if $game_temp.battle_can_escape
					# 決定 SE を演奏
					$game_system.se_play($data_system.decision_se)
					# アクションを設定
					@active_battler.current_action.kind = 0
					@active_battler.current_action.basic = 4
					# 次のアクターのコマンド入力へ
					phase3_next_actor
				else
					# ブザー SE を演奏
					$game_system.se_play($data_system.buzzer_se)
				end
				return
			end
		end
		# 呼び戻す
		xrxs_bsp1_update_phase3_basic_command
	end
	#--------------------------------------------------------------------------
	# ● メインフェーズ開始
	#--------------------------------------------------------------------------
	alias xrxs65_start_phase4 start_phase4
	def start_phase4
		# ターン数を引くことによって擬似的にカウントに変化を起こさせない
		$game_temp.battle_turn -= 1
		# フラグを退避
		save_flags = $game_temp.battle_event_flags.dup
		# 呼び戻す
		xrxs65_start_phase4
		# フラグを復旧
		$game_temp.battle_event_flags = save_flags
	end
	#--------------------------------------------------------------------------
	# ● 行動順序作成
	#--------------------------------------------------------------------------
	alias xrxs65_make_action_orders make_action_orders
	def make_action_orders
		# 呼び戻す
		xrxs65_make_action_orders
		# CPが不足している場合は @action_battlers から除外する
		for battler in @action_battlers.dup
			@action_battlers.delete(battler) unless battler.cp_full?# || battler.id == @fuck_helper.id
		end
	end
	#--------------------------------------------------------------------------
	# ● フレーム更新 (メインフェーズ ステップ 2 : アクション開始)
	#--------------------------------------------------------------------------
	alias xrxs65_update_phase4_step2 update_phase4_step2
	def update_phase4_step2
		# ガードの解除
		@active_battler.guarding = false
		# CPの消費
		@active_battler.cp = 0
		#p @active_battler.name,@active_battler.cp
		# CP メーターの更新
		@cp_meters.refresh
		# 呼び戻す
		xrxs65_update_phase4_step2
		# 例外補正
		return if @active_battler == nil
		# ターンコントローラ
		if @active_battler.is_a?(Game_Enemy) and @active_battler.index == XRXS65::TC
			cp_turn_count
		end
	end
	#--------------------------------------------------------------------------
	# ● 基本アクション 結果作成
	#--------------------------------------------------------------------------
	alias xrxs65_make_basic_action_result make_basic_action_result
	def make_basic_action_result
		# 呼び戻す
		xrxs65_make_basic_action_result
		# 防御の場合
		if @active_battler.current_action.basic == 1
			@active_battler.guarding = true
			return
		end
		# パーティの逃亡の場合
		if @active_battler.current_action.basic == 4
			# 逃走可能ではない場合
			if $game_temp.battle_can_escape == false
				# ブザー SE を演奏
				$game_system.se_play($data_system.buzzer_se)
				return
			end
			# パーティ全員の CP をクリア
			for actor in $game_party.actors
				actor.cp = 0
			end
			# CP メーターの更新
			@cp_meters.refresh
			# 逃走処理
			update_phase2_escape
			return
		end
	end
	#--------------------------------------------------------------------------
	# ● 特技アクション 結果作成
	#--------------------------------------------------------------------------
	alias xrxs65_make_skill_action_result make_skill_action_result
	def make_skill_action_result
		# 呼び戻す的
		xrxs65_make_skill_action_result
		if XRXS65::CPADD_SKILLLIST.keys.include?(@skill.id)
			cp_add = XRXS65::CPADD_SKILLLIST[@skill.id]
      list = []
      if @active_battler.is_a?(Game_Actor)
        for battler in $game_party.actors
          num = (XRXS65::CPADD_SKILLLIST[@skill.id] / (XRXS65::SPEED * 4096 * battler.agi / XRXS_CP_SYSTEM.total_agi)).ceil
          #p num
          for i in 0...num
            for battler2 in $game_party.actors
            if battler2.cp >= battler2.max_cp
              battler2.cp = battler2.max_cp - i-1
              break
            end
            battler2.cp_update
            @cp_meters.refresh
            Graphics.update
            end
          end
        end
      end
      if @active_battler.is_a?(Game_Enemy)
        for battler in $game_party.enemies
          num = (XRXS65::CPADD_SKILLLIST[@skill.id] / (XRXS65::SPEED * 4096 * battler.agi / XRXS_CP_SYSTEM.total_agi)).ceil
          for i in 0...num
            if battler.cp >= battler.max_cp
              battler.cp = battler.max_cp
              break
            end
            battler.cp_update
            Graphics.update
          end
        end
      end
    end
	end
	#--------------------------------------------------------------------------
	# ● フレーム更新 (メインフェーズ ステップ 5 : ダメージ表示)
	#--------------------------------------------------------------------------
	alias xrxs65_update_phase4_step5 update_phase4_step5
	def update_phase4_step5
		# 呼び戻す
		xrxs65_update_phase4_step5
		# CP メーターの更新
		@cp_meters.refresh
	end
end
#==============================================================================
# --- CP メーターをまとめて管理するクラス、表示関係はすべてココ ---
#==============================================================================
class CP_Meters
	#--------------------------------------------------------------------------
	# ○ オブジェクト初期化
	#--------------------------------------------------------------------------
	def initialize
		# メーター群の生成
		@meters = []
		for i in 0...$game_party.actors.size
			make_meter(i)
		end
		refresh
	end
	#--------------------------------------------------------------------------
	# ○ リフレッシュ
	#--------------------------------------------------------------------------
	def refresh
		# 戦闘メンバー数の変更を判別
		for i in @meters.size...$game_party.actors.size
			make_meter(i)
		end
		for i in [email protected]
			@meters[i].dispose
			@meters[i] = nil
		end
		@meters.compact!
		# 表示更新
		for i in 0...$game_party.actors.size
			actor = $game_party.actors[i]
			@meters[i].line   = actor.cp_linetype
			@meters[i].amount = actor.cp_lineamount
		end
	end
	#--------------------------------------------------------------------------
	# ○ メーターひとつの生成
	#--------------------------------------------------------------------------
	def make_meter(i)
		# スキンの取得
		skin = RPG::Cache.windowskin(XRXS65::SKIN)
		#
		space = 640 / XRXS65::MAX
		case XRXS65::ALIGN
		when 0
			actor_x = i * space + 4
		when 1
			actor_x = (space * ((XRXS65::MAX - $game_party.actors.size)/2.0 + i)).floor
		when 2
			actor_x = (i + XRXS65::MAX - $game_party.actors.size) * space + 4
		end
		meter = MeterSprite.new(skin, XRXS65::LINE_HEIGHT)
		meter.x = actor_x + XRXS65::X_OFFSET - skin.width
		meter.y = XRXS65::Y_OFFSET
		@meters[i] = meter
	end
	#--------------------------------------------------------------------------
	# ○ 可視状態
	#--------------------------------------------------------------------------
	def visible=(b)
		@meters.each{|sprite| sprite.visible = b }
	end
	#--------------------------------------------------------------------------
	# ○ 解放
	#--------------------------------------------------------------------------
	def dispose
		@meters.each{|sprite| sprite.dispose }
	end
end
#==============================================================================
# --- XRXS. レクタンギュラーメーター表示・極 ---
#==============================================================================
class MeterSprite < Sprite
	#--------------------------------------------------------------------------
	# ○ オブジェクト初期化
	#--------------------------------------------------------------------------
	def initialize(skin, line_height)
		@skin   = skin
		@width  = @skin.width
		@height = line_height
		@line   = 1
		@amount = 0
		@base_sprite = Sprite.new
		@base_sprite.bitmap = @skin
		@base_sprite.src_rect.set(0, 0, @width, @height)
		@base_sprite.z = 601
		super()
		self.z = @base_sprite.z + 1
		self.bitmap = @skin
		self.line = 1
	end
	#--------------------------------------------------------------------------
	# ○ 値の設定
	#--------------------------------------------------------------------------
	def line=(n)
		@line = n
		refresh
	end
	def amount=(n)
		@amount = n
		refresh
	end
	def refresh
		self.src_rect.set(0, @line * @height, @width * @amount / 100, @height)
	end
	#--------------------------------------------------------------------------
	# ○ 座標の設定
	#--------------------------------------------------------------------------
	def x=(n)
		super
		@base_sprite.x = n
	end
	def y=(n)
		super
		@base_sprite.y = n
	end
	#--------------------------------------------------------------------------
	# ○ 解放
	#--------------------------------------------------------------------------
	def dispose
		@base_sprite.dispose
		super
	end
end