Permalink
Browse files

More abstraction, and start tracking where enemies are

  • Loading branch information...
1 parent c2e0569 commit ff8ec531254cf4b996cd13b898f23c1b5847a971 @gabebw committed Sep 23, 2010
Showing with 72 additions and 15 deletions.
  1. +72 −15 player.rb
View
@@ -5,6 +5,7 @@ class Player
MINIMUM_PERCENT_HEALTH = 70
ENEMY_HEALTH = {:thick_sludge => 24,
:archer => 7 }
+ # Yay, Ruby 1.9! Yes, I'm using a lambda only so it takes up less space.
AVG_ENEMY_HEALTH = ->(v){v.inject{|acc,x|acc+x}/v.size.to_f}.call(ENEMY_HEALTH.values)
DIRECTIONS = [:forward, :backward]
@@ -21,15 +22,22 @@ def play_turn(warrior)
# Performs a bang-action, e.g. walk!
def perform_action!(warrior)
# available methods for a Space (returned by warrior.feel)
- # stairs?
- # empty?
- # enemy?
- # captive?
- # wall?
- # ticking? (OMINOUS!)
- # Note that the stairs space is empty, i.e.
- # if warrior.feel.stairs? is true, then warrior.feel.empty? is true too
-
+ # TESTERS
+ # stairs?
+ # empty?
+ # enemy?
+ # captive?
+ # wall?
+ # ticking? (OMINOUS!)
+ # TYPE GETTERS
+ # unit (e.g. "Thick Sludge", may be nil)
+ # location - An [X-coord, Y-coord] pair
+ # - A coordinate may be < 0, e.g. if you're at [0, 0],
+ # then warrior.feel(:backward).location = [-1, 0]
+ # character (e.g. "S" for a thick sludge, may be nil)
+ # Note that the stairs space is empty (unless a unit is on it), i.e.
+ # space.empty? returns true for the stairs space, so explicitly check
+ # for stairs.
# We can get away with only feeling in one direction, for now.
space = warrior.feel(direction)
@@ -41,19 +49,29 @@ def perform_action!(warrior)
elsif should_rest?
if in_safe_space?
# Regain some health
+ mark_safe_space!
warrior.rest!
else
+ puts "Should rest, but not in safe space. Moving towards it."
move_toward_safe_space!
end
elsif space.captive?
warrior.rescue!(direction)
elsif space.enemy?
+ set_most_recent_enemy_direction!
warrior.attack!(direction)
elsif space.empty?
- puts "Empty space"
- warrior.walk!(direction)
+ # May have just moved away from an enemy, so check if we should
+ # re-engage.
+ if direction_of_most_recent_enemy.nil?
+ # No enemies, blithely continue
+ warrior.walk!(direction)
+ else
+ move_toward_most_recent_enemy!
+ end
elsif taking_damage_from_afar? and should_rest?
puts "Should move away from threat"
+ set_most_recent_enemy_direction!
# Move away from threat
move_toward_safe_space!
elsif space.wall?
@@ -69,17 +87,48 @@ def perform_action!(warrior)
# STATE CHANGERS
def reverse_direction!
- @direction = (direction == :forward ? :backward : :forward)
+ @direction = opposite_direction_of(direction)
end
# Moves warrior toward a safe space
def move_toward_safe_space!
puts "Moving toward safe space"
- #if taking_damage_from_afar? and should_rest?
+ if taking_damage_from_afar? and should_rest?
# Move away from threat
+ @direction = opposite_direction_of(direction_of_most_recent_enemy)
+ else
reverse_direction!
- @warrior.walk!(direction)
- #end
+ end
+ @warrior.walk!(direction)
+ end
+
+ # Returns direction of most recent enemy that the warrior engaged (or just
+ # took damage from), or nil if the warrior has yet to encounter an enemy
+ def direction_of_most_recent_enemy
+ #@direction_of_most_recent_enemy ||= direction
+ # Default to nil so we can tell when we have yet to engage an enemy
+ @direction_of_most_recent_enemy ||= nil
+ end
+
+ # Set the direction of the most recent enemy to the current direction.
+ # Only called when warrior is engaging an enemy (or just taking damage
+ # from it).
+ def set_most_recent_enemy_direction!
+ @direction_of_most_recent_enemy = direction
+ end
+
+ def move_toward_most_recent_enemy!
+ @direction = direction_of_most_recent_enemy
+ @warrior.walk!(direction)
+ end
+
+ # Mark the current space as safe
+ def mark_safe_space!
+ @safe_spaces ||= []
+ backward = @warrior.feel(:backward)
+ x_coord = backward[0] + 1
+ y_coord = backward[1] + 1
+ @safe_spaces << [x_coord, y_coord]
end
## TESTERS
@@ -131,4 +180,12 @@ def current_turn
def direction
@direction ||= :backward
end
+
+ # UTILITY
+ def opposite_direction_of(dir)
+ case dir
+ when :forward then :backward
+ when :backward then :forward
+ end
+ end
end

0 comments on commit ff8ec53

Please sign in to comment.