Skip to content
Jarod42 edited this page Sep 27, 2010 · 3 revisions
class Player

	def initialize()
		@turn = 1
		@maxhealth = 20
	end

	def play_turn(warrior)
		action(warrior)
		updateInfoEndTurn(warrior)
	end

	def updateInfoEndTurn(warrior)
		@turn += 1
	end

	def action(warrior)
		if (halfDetonate(warrior) or moveToTicking(warrior) or bind(warrior) \
			or attack(warrior) or rescueAround(warrior) \
			or finish(warrior) or restore(warrior) or achievebind(warrior) \
			or move(warrior))
			return
		end
	end

	def halfDetonate(warrior)
		if (warrior.look[1].enemy? and warrior.look[1].unit.health < 5)
			if (warrior.health < 5)
				warrior.rest!
			else
				warrior.detonate!
			end
			return true
		end
		return false
	end

	def moveToTicking(warrior)
		warrior.listen.each do |space|
			if (space.ticking?)
				spaceDir = warrior.direction_of(space)
				nextSpace = warrior.feel(spaceDir)
				if (nextSpace.empty? and !nextSpace.stairs?)
					warrior.walk!(spaceDir)
					return true
				end
			end
		end
		return false
	end

	def move(warrior)
		warrior.listen.each do |space|
			if (!warrior.feel(warrior.direction_of(space)).stairs?)
				warrior.walk!(warrior.direction_of(space))
			else
				[:forward, :left, :right, :backward].each do |dir|
					if (!warrior.feel(dir).stairs?)
						warrior.walk!(dir)
						break;
					end
				end
			end
			puts ("going to #{space.unit.character}")
			return true
		end
		warrior.walk!(warrior.direction_of_stairs)
		return false
	end
	
	def achievebind(warrior)
		[:forward, :left, :right, :backward].each do |dir|
			if (warrior.feel(dir).captive? and warrior.feel(dir).unit.character != "C")
				warrior.attack!(dir)
				return true
			end
		end
		return false
	end
	
	def bind(warrior)
		count = 0
		[:forward, :left, :right, :backward].each do |dir|
			count += 1 if warrior.feel(dir).enemy?
		end
		return false if (count < 2)
		[:left, :right, :forward, :backward].each do |dir|
			if (warrior.feel(dir).enemy?)
				warrior.bind!(dir)
				return true
			end
		end
		return false
	end

	def getMinHealth(warrior)
		minHealth = 1
		warrior.listen.each do |space|
			case space.unit.character
			when "s" then minHealth += 9
			when "S" then minHealth += 12
			end
		end
		return minHealth
	end

	def restore(warrior)
		if warrior.health < getMinHealth(warrior) and warrior.health < @maxhealth
		  warrior.rest!
		  return true
		end
		return false
	end

	def attack(warrior)
		[:forward, :left, :right, :backward].each do |dir|
			if (warrior.feel(dir).enemy?)
				if (warrior.look(dir)[1].enemy?)
					warrior.detonate!(dir)
				else
					warrior.attack!(dir)
				end
				#@prevAttackIsShoot = false
				return true
			end
		end
		return false
	end

	def finish(warrior, dir = :forward)
		warrior.listen.each do return false end
		warrior.walk!(warrior.direction_of_stairs)
		return true
	end

	def rescueAround(warrior)
		for dir in [:backward, :left, :right, :forward]
			if (warrior.feel(dir).captive? and warrior.feel(dir).unit.character == "C")
				warrior.rescue!(dir)
				return true
			end
		end
		return false
	end
end