Skip to content
Felipe S. S. Schneider edited this page Jul 10, 2019 · 1 revision
SAFE_PERCENT = 0

class Player
	def update_info warrior
		@max_health ||= warrior.health
		@last_health ||= @max_health
		@health ||= @max_health

		@last_health = @health
		@health = warrior.health

		@t ||= -1
		@t += 1
	end

	def damage
		if @last_health > @health
			return @last_health - @health
		end

		return 0
	end

	def safe_health?
		return false if @health - damage < @max_health*SAFE_PERCENT/2
		return true
	end

	def got_direct_enemy? warrior, direction = :forward
		if warrior.feel(direction).enemy?
			return true
		end

		warrior.look(direction).each do |space|
			if space.captive?
				return false
			end
			if space.enemy?
				return true
			end
			if space.stairs?
				return false
			end
		end

		return false
	end
	
	def got_direct_captive? warrior, direction = :forward
		if warrior.feel(direction).captive?
			return true
		end
		
		warrior.look(direction).each do |space|
			if space.captive?
				return true
			end
			if space.enemy?
				return false
			end
			if space.stairs?
				return false
			end
		end

		return false
	end

	def got_direct_wall? warrior, direction = :forward
		if warrior.feel(direction).wall?
			return true
		end
		
		warrior.look(direction).each do |space|
			if space.wall?
				return true
			end
			if space.captive?
				return false
			end
			if space.enemy?
				return false
			end
			if space.stairs?
				return false
			end
		end

		return false
	end

	def play_turn warrior
		update_info warrior

		if got_direct_enemy? warrior, :backward
			if warrior.feel(:backward).enemy?
				return warrior.attack! :backward 
			end
			return warrior.shoot! :backward
		elsif got_direct_enemy? warrior
			if warrior.feel.enemy?
				return warrior.attack!
			end
			return warrior.shoot!
		elsif damage > 0
			if not safe_health?
				if got_direct_enemy? warrior
					return warrior.walk! :backward
				elsif got_direct_enemy? warrior, :backward
					return warrior.walk!
				end
			end
		else
			if warrior.feel(:backward).captive?
				return warrior.rescue! :backward
			elsif got_direct_captive? warrior, :backward
				if warrior.feel(:backward).captive?
					return warrior.rescue! :backward
				else
					return warrior.walk! :backward
				end
			elsif got_direct_captive? warrior
				if warrior.feel.captive?
					return warrior.rescue!
				else
					return warrior.walk!
				end
			elsif warrior.health < @max_health*SAFE_PERCENT
				return warrior.rest!
			end
		end

		return warrior.pivot! if got_direct_wall? warrior
		return warrior.walk!
	end
end