Skip to content
Browse files

Try to fix lunge

Add attack_type and counter_type random selector.
  • Loading branch information...
1 parent 2209b32 commit d5f49aa1f9489ce0ab84436478b01c31351c81e1 @galthaus committed Jul 8, 2012
Showing with 105 additions and 29 deletions.
  1. +12 −2 global.rb
  2. +51 −13 person.rb
  3. +27 −5 run_sim.rb
  4. +15 −9 test_person.rb
View
14 global.rb
@@ -46,8 +46,18 @@ class Global
:thrust => 30
}
}
- ATTACK_TYPE_DEFAULT = :slash
- COUNTER_ATTACK_TYPE_DEFAULT = :slash
+ ATTACK_TYPE_DEFAULT = {
+ :use_force => true,
+ :force => :slash,
+ :choices => {
+ :lunge => 40,
+ :slash => 30,
+ :thrust => 30
+ }
+ }
+
+ COUNTER_ATTACK_TYPE_DEFAULT = ATTACK_TYPE_DEFAULT
+
STUN_ACTION_DEFAULT = :parry
ACTIONS_DEFAULT = [:attack, :parry]
View
64 person.rb
@@ -153,15 +153,57 @@ def guess_attack(opponent)
end
def get_actions(opponents = nil)
- if @stunned
- @actions = [ :stun, @stun_action ]
- @stunned = false
- else
- @actions = @default_actions
- end
- @opponent = opponents.first rescue nil
- # GREG: This should be configurable.
- @attack_type = @default_attack_type
+ if @stunned
+ @actions = [ :stun, @stun_action ]
+ @stunned = false
+ else
+ @actions = @default_actions
+ end
+ @opponent = opponents.first rescue nil
+
+ # Random assign attack_type
+ if @default_attack_type[:use_force]
+ @attack_type = @default_attack_type[:force]
+ else
+ choices = @default_attack_type[:choices] rescue nil
+ @attack_type = :fred unless choices
+ if choices
+ count = choices.values.inject(0, :+)
+ # Remove lunge if we didn't choose attack
+ ignore_lunge = false
+ if choices[:lunge] and @actions.size != 1
+ count -= choices[:lunge]
+ ignore_lunge = true
+ end
+ roll = Utils.roll("1d#{count}")
+ sum = 0
+ choices.sort { |x,y| x.to_s <=> y.to_s }.each do |k,v|
+ next if k == :lunge and ignore_lunge
+ @attack_type = k
+ break if roll <= v
+ roll -= v
+ end
+ end
+ end
+ @attack_type = :lunge if @actions.size == 1
+ end
+
+ def counter_attack_type(opponent)
+ # Random assign attack_type
+ if @counter_attack_type[:use_force]
+ return @counter_attack_type[:force]
+ else
+ choices = @counter_attack_type[:choices] rescue nil
+ return :fred unless choices
+ count = choices.values.inject(0, :+)
+ roll = Utils.roll("1d#{count}")
+ sum = 0
+ choices.sort { |x,y| x.to_s <=> y.to_s }.each do |k,v|
+ return k if roll <= v
+ roll -= v
+ end
+ return :fred
+ end
end
def parrying?
@@ -204,10 +246,6 @@ def active?
!@died and !@knocked_out and !@resigned
end
- def counter_attack_type(opponent)
- @counter_attack_type
- end
-
def get_location(opponent)
# GREG: Choose this one day
@attack_location
View
32 run_sim.rb
@@ -42,14 +42,36 @@
# [ attack ] # Lunge case
#
+# Setting :use_force to true causes the system to use :force always
+# Setting :use_force to false causes the system to use the distribution.
+attack_type = {
+ :use_force => false,
+ :force => :slash,
+ :choices => {
+ :lunge => 40,
+ :slash => 30,
+ :thrust => 30
+ }
+}
+counter_type = {
+ :use_force => false,
+ :force => :slash,
+ :choices => {
+ :slash => 50,
+ :thrust => 50
+ }
+}
+
+attack_guess = { :right => true } # This line means the defender always guesses right!
+# attack_guess = { :wrong => true } # This line means the defender always guesses wrong!
+# attack_guess = {:choices => { :slash => 30, :lunge => 30, :thrust => 40 }} # This line means the defender guess in those percentages!
+
# Big Run
compare = {
:attack_location => [ :chest ], # Or build subsets :chest, :flank, :right_arm, :right_leg, :left_arm, :left_leg
:default_actions => [ [:attack, :counter] ], # Default action (:counter, :parry, :attack, :evade)
:stun_action => [ :parry ], # Action to take when stunned
- :default_attack_type => [ :thrust ], # Attack type: :slash, :lunge, :thrust
- :counter_attack_type => [ :thrust ], # Attack type to use on counter: :slash, :thrust
:endurance => (10..10), # Endurance
:dexterity => (13..13), # Dexterity
:hit_points => (12..12), # (X..Y) means X to Y inclusive (X...Y) means X to Y-1.
@@ -58,9 +80,9 @@
:high_quality_weapon => [ true ], # true or false if weapons are HQ
:offhand_weapon => [:main_gauche], # :main_gauche, :good_stuff, :OK_stuff in a list
:style => [ :french ], # styles are: :italian, :french, :spanish
- :attack_guess => [{ :right => true }] # This line means the defender always guesses right!
-# :attack_guess => [{ :wrong => true }] # This line means the defender always guesses wrong!
-# :attack_guess => [{:choices => { :slash => 30, :lunge => 30, :thrust => 40 }}] # This line means the defender guess in those percentages!
+ :default_attack_type => [ attack_type ], # Defined above as a variable
+ :counter_attack_type => [ counter_type ], # Defined above as a variable
+ :attack_guess => [ attack_guess ] # This is defined above as a variable
}
compare1 = compare
View
24 test_person.rb
@@ -350,9 +350,12 @@ def test_attack_location
assert_equal :tail, p1.get_location(p1)
end
+ # GREG: Add variable attack_type tests
def test_attack_type
- p1 = Person.new("James", {:attack_type => :tail})
- assert_equal :tail, p1.default_attack_type
+ a = {:use_force => true, :force => :tail}
+ p1 = Person.new("James", {:attack_type => a})
+
+ assert_equal a, p1.default_attack_type
assert_equal nil, p1.attack_type
p1.get_actions
assert_equal :tail, p1.attack_type
@@ -387,7 +390,7 @@ def test_attack_type
end
def actions_test(actions, att, deff, par, eva, cou)
- p1 = Person.new("James", {:attack_type => :slash, :actions => actions})
+ p1 = Person.new("James", {:attack_type => {:use_force => true, :force => :slash}, :actions => actions})
p1.get_actions([p1])
assert_equal att, p1.attacking?
assert_equal deff, p1.defending?
@@ -406,7 +409,8 @@ def test_actions
end
def test_stunned_actions
- p1 = Person.new("James", {:attack_type => :slash, :stun_action => :attack})
+ a = {:use_force => true, :force => :slash}
+ p1 = Person.new("James", {:attack_type => a, :stun_action => :attack})
p1.take_damage(:head, 5, 1)
assert_equal true, p1.stunned
p1.get_actions([p1])
@@ -431,22 +435,23 @@ def test_guess_attack
assert_equal :fred, p1.guess_attack(p1)
$mock_die = nil
- p1 = Person.new("James", {:attack_guess => {:right => true}, :attack_type => :john})
+ a = {:use_force => true, :force => :john}
+ p1 = Person.new("James", {:attack_guess => {:right => true}, :attack_type => a})
p1.get_actions(p1)
$mock_die = [ 5 ]
assert_equal :john, p1.guess_attack(p1)
$mock_die = nil
- p1 = Person.new("James", {:attack_guess => {:wrong => true}, :attack_type => :john})
+ p1 = Person.new("James", {:attack_guess => {:wrong => true}, :attack_type => a})
p1.get_actions(p1)
$mock_die = [ 5 ]
assert_equal :fred, p1.guess_attack(p1)
$mock_die = nil
- p1 = Person.new("James", {:attack_guess => {:right => false, :wrong => true}, :attack_type => :john})
+ p1 = Person.new("James", {:attack_guess => {:right => false, :wrong => true}, :attack_type => a})
p1.get_actions(p1)
$mock_die = [ 5 ]
assert_equal :fred, p1.guess_attack(p1)
$mock_die = nil
- p1 = Person.new("James", {:attack_guess => {:right => true, :wrong => true}, :attack_type => :john})
+ p1 = Person.new("James", {:attack_guess => {:right => true, :wrong => true}, :attack_type => a})
p1.get_actions(p1)
$mock_die = [ 5 ]
assert_equal :john, p1.guess_attack(p1)
@@ -458,7 +463,8 @@ def test_guess_attack
end
def test_counter_attack_type
- p1 = Person.new("James", {:counter_attack_type => :tail})
+ a = {:use_force => true, :force => :tail}
+ p1 = Person.new("James", {:counter_attack_type => a})
assert_equal :tail, p1.counter_attack_type(p1)
end

0 comments on commit d5f49aa

Please sign in to comment.
Something went wrong with that request. Please try again.