require 'darkhall/const'

module DarkHall
	
	module Util
		module_function
		
		
		def convert_key_to_string(src_hash)
			re = {}
			src_hash.each_pair do |key, value|
				re[key.to_s] = value
			end
			
			re
		end
		
		# nilなら表示無し
		def bonus_to_s(value)
			if !(value) then
				'--'
			elsif value > 0 then
				"+#{value}"
			else
				value.to_s
			end
		end

	
		def encrypt(str)
			[str].pack('m')
		end
			
		def decrypt(str)
			str.unpack('m').first
		end
		
		def random_pick(array)
			if array.empty? then
				nil
			else
				array[rand(array.size)]
			end
		end
		
		
		EVAL_TEXT_PATTERN = /%\{(.+?)\}/
		def eval_text(text, params = {})
			re = text.gsub(EVAL_TEXT_PATTERN) do
				params[$1.to_sym] || "(#{$1})"
			end
		
			re
		end
		
		def next_index(index, max)
			index += 1
			index = 0 if index >= max
			return index
		end
		
		def prev_index(index, max)
			index -= 1
			index = (max - 1) if index <= -1
			return index
		end
		
		def fold_in_range(value, range)
			if value > range.last then
				range.last
			elsif value < range.first then
				range.first
			else
				value
			end	
		end
		
		
		
		
		def get_damage_type_caption(damage_types)
			caption = ""
			
			caption << _("斬") if damage_types.include?(DT_SLASH)
			caption << _("突") if damage_types.include?(DT_THRUST)
			caption << _("打") if damage_types.include?(DT_BLOW)
			caption << _("熱") if damage_types.include?(DT_HEAT)
			caption << _("冷") if damage_types.include?(DT_COLD)
			caption << _("雷") if damage_types.include?(DT_ERECTRIC)
			
			caption = "-" if caption.empty?
			
			return caption
		end
		
		def get_damage_resist_caption(resists)
			types = resists.find_all{|k, v| v > RES_NORMAL}.map{|k, v| k}
			return get_damage_type_caption(types)
		end
		
		def get_damage_weakness_caption(resists)
			types = resists.find_all{|k, v| v < RES_NORMAL}.map{|k, v| k}
			return get_damage_type_caption(types)
		end
		
		
	
		
		
		
		
		def targets_caption(targets)
			if targets and not targets.empty? then
				n_('%{t}', '%{t}たち', targets.size).evaluate(:t => targets.first.name)
			else
				""
			end
	
		end
		
		# 数値に対して、80%～120%の範囲で乱数によるばらつきを与える
		def randomize_20p(param)
			return (param * (80 + rand(40)) / 100.0).to_i
		end
		
		# ダイス（サイコロ）を振る
		def diceroll(dice, border = nil)
			result = 0
			number, max = dice.split("d")
			number = 1 if number.empty?
			max = 6 if max.empty?
			number = number.to_i
			max = max.to_i
		
			number.times do
				result = result + rand(max) + 1
			end
		
			if border then
				return (result <= border.to_i)
			else
				return result
			end
		end

	end
	
	include Util


	
	
	module Calc
		module_function
	
		# 特性値の算出
		def ability_base(growth, level)
			# 基本値
			re = 8 + growth * 2

			# レベルボーナス
			re += (0.6 + growth * 0.1) * level

			re.to_i
		end
		
		# 防護力→ダメージ減少値
		def def2red(df)
			((df + 2) ** 1.8 * 0.1).round
		end
		
		# 宿屋の宿泊費用
		def inn_fee(room_type, member)
			case room_type
			when :stable
				0
			when :standard
				(member.hp_max * 0.08).to_i
			when :large
				(member.hp_max * 0.16).to_i
			when :royal
				(member.hp_max * 0.40).to_i
			else
				raise "unknown room type - #{room_type}"
			end
		end
	end


end


class String
	def evaluate(params)
		DarkHall::Util.eval_text(self, params)
	end
end