Skip to content

Instantly share code, notes, and snippets.

@dekart
Created April 28, 2009 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dekart/102987 to your computer and use it in GitHub Desktop.
Save dekart/102987 to your computer and use it in GitHub Desktop.
VTM = {
:dice => 10,
:critical_failure => 1,
:critical_success => 10,
:success => 5
}
def calculate_dices(attacker, victim)
attack_points = attacker.attack_points
defence_points = victim.defence_points
attack_bonus = 1
defence_bonus = 1
# Считаем считаем наносимые повреждения
attack = []
# Бросаем кости
attack_dices = attack_points.d(VTM[:dice])
# Выбираем критичные успехи и бросаем на каждый дополнительный кубик
attack_dices.select { |value| value == VTM[:critical_success] }.each do
attack_dices << 1.d(VTM[:dice]).to_a
end
# Собираем все успешные броски
attack_dices.each do |value|
attack.push(value) if value >= VTM[:success]
end
# Выбираем критичные неудачи и на каждую вычитаем один успех
attack_dices.select { |value| value == VTM[:critical_failure] }.each do
attack.pop
end
# Считаем считаем компенсируемые повреждения
defence = []
# Бросаем кости
defence_dices = defence_points.d(VTM[:dice])
# Выбираем критичные успехи и бросаем на каждый дополнительный кубик
defence_dices.select { |value| value == VTM[:critical_success] }.each do
defence_dices << 1.d(VTM[:dice]).to_a
end
# Собираем все успешные броски
defence_dices.each do |value|
defence.push(value) if value >= VTM[:success]
end
# Выбираем критичные неудачи и на каждую вычитаем один успех
defence_dices.select { |value| value == VTM[:critical_failure] }.each do
defence.pop
end
# Summarize successful dices
attack = attack.summarize.to_f * attack_bonus
defence = defence.summarize.to_f * defence_bonus
# Make sure that both attack and defence are at least 1
attack = 1 if attack == 0
defence = 1 if defence == 0
logger.debug <<-CODE
Attack: #{attack_points} * #{attack_bonus} = #{attack}
Defence Points: #{defence_points} * #{defence_bonus} = #{defence}
CODE
attacker_won = (attack >= defence)
if attacker_won
attack_damage = rand(victim.health) * 0.3
defence_damage = rand(attack_damage * defence / attack)
else
defence_damage = rand(attacker.health) * 0.3
attack_damage = rand(defence_damage * attack / defence)
end
return [attacker_won, attack_damage.ceil, defence_damage.ceil]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment