/***************************************************************************
 *   Copyright (C) 2004 by ian reinhart geiser                             *
 *   ian@geiseri.com                                                       *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "monster.h"
#include "util.h"
#include "player.h"
#include "wepon.h"
#include "armor.h"
Monster::Monster(const QString &name, int maxGold) : Unit(name)
{
	m_gold = roll( 2, maxGold);
	m_attacks = 1;
	m_morale = roll( 1,20 );
	m_sprite = QPixmap(16,16);
	m_sprite.fill(Qt::red);
	m_isUndead = false;
	m_attackType = CUT;
	m_dexterity = roll( 4,4 );
	m_strength = roll( 4,4 );
	m_hitpoints = roll( 2,6 );
	m_thaco = 19;
	m_xpValue = 1;
	m_skill = roll(10,10);
	m_gold = roll( 1,4 );

}


Monster::~Monster()
{}

int Monster::toHit(Attack at , Armor *ac ) const
{
	int val = m_thaco;
	switch( at )
	{
			case CUT:
			val -= ac->cut();
			break;
			case BASH:
			val -= ac->bash();
			break;
			case THRUST:
			val -= ac->thrust();
			break;
			default:
			break;
	}
	return val;
}

bool Monster::save( const QString & vs ) const
{
	int max = m_saves[vs];
	max += m_armor->saveVersus(vs);
	max += m_wepon->saveVersus(vs);
	if( max == 0 )
		return false;
	return (roll( 2,10) < max );
}

bool Monster::melee( Unit * unit, Attack atk ) const
{
	bool success = false;
	for( int num = 0; num < m_attacks; ++num )
	{
		int value = roll( 1, 20 );
		if( value == 20 )
		{
			// Do somthing really mean to the player.
			unit->hurt(1000);
			success = true;
		}
		else if ( value >= unit->toHit( m_attackType, m_armor ) )
		{
			switch( m_attackType )
			{
				case THRUST:
				unit->hurt(  m_wepon->thrust( this ) );
				break;
				case CUT:
				unit->hurt(  m_wepon->cut( this ) );
				break;
				case BASH:
				unit->hurt(  m_wepon->bash( this ) );
				break;
			}
			success = true;
		}
	}
	return success;
}

int Monster::morale( ) const
{
	return m_morale * (m_hitpoints - m_damage)/m_hitpoints;
}

Monster * Monster::monsterFactory( const QString & monsterName )
{
	void * ptr = QMetaType::construct( QMetaType::type( monsterName.toLatin1().constData()) );
	if( ptr )
	{
		return static_cast<Monster*>( ptr );
	}
	else
		return 0;
}

Attack Monster::attackType() const
{
	return m_attackType;
}

