/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   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
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "constants.h"

#include "human.h"

#include "humanDb.h"
#include "../../misc/setting.h"
#include "../../ui/ui.h"

/**
 **
 ** Construktor
 **
 ** @param	-
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.4.4
 **
 **/
Human::Human() :
Ai()
{
  // *** HACK
  this->type_set(HUMAN);
  this->name_set("");
#ifndef RELEASE
#ifdef BENDERS
  this->db_ = new HumanDb();
#endif
#endif

  return ;
} // Human::Human()

/**
 **
 ** Construktor
 **
 ** @param	istr	stream with the infos
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.2
 **
 **/
Human::Human(istream& istr) :
Ai(istr)
{
  this->type_set(HUMAN);
  this->name_set("");

  return ;
} // Human::Human(istream& istr)

/**
 **
 ** copy-constructor
 **
 ** @param	player	player to copy
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.3
 **
 **/
Human::Human(Player const& player) :
Ai(player)
{
  this->type_set(HUMAN);

  return ;
} // Human::Human(Player const& player)

/**
 **
 ** copy-constructor
 **
 ** @param	player		player to copy
 ** @param	aiconfig	configuration to copy
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
Human::Human(Player const& player, Aiconfig const& aiconfig) :
Ai(player, aiconfig)
{
  this->type_set(HUMAN);

  return ;
} // Human::Human(Player const& player, Aiconfig const& aiconfig)

/**
 **
 ** copy-constructor
 **
 ** @param	ai	ai to copy
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.7
 **
 **/
Human::Human(Ai const& ai) :
Ai(static_cast<Ai const&>(ai))
{
  this->type_set(HUMAN);

  return ;
} // Human::Human(Ai const& ai)

/**
 **
 ** copy-constructor
 **
 ** @param	human	human to copy
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.3
 **
 **/
Human::Human(Human const& human) :
Ai(static_cast<Ai const&>(human))
{
  this->type_set(HUMAN);

  return ;
} // Human::Human(Human const& human)

/**
 **
 ** clone the player
 **
 ** @param       -
 **
 ** @return      pointer of a clone
 **
 ** @author      Diether Knof
 **
 ** @version     0.5.3
 **
 **/
Human*
Human::clone() const
{  
  return new Human(*this);
} // virtual Human* Human::clone() const

/**
 **
 ** Destruktor
 **
 ** @param	-
 **
 ** @param	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.4.4
 **
 **/
Human::~Human()
{
  return ;
} // Human::~Human()

/**
 **
 ** returns the reservation of the player
 ** (taken from the ui)
 **
 ** @param	is_duty		the player has to play a duty solo
 **				(default: false)
 **
 ** @return	reservation of the player
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.1
 **
 **/
Reservation
Human::reservation_get(bool is_duty)
{ 
  DEBUG_ASSERTION(!this->game().isvirtual(),
		  "Human::reservation_get():\n"
		  "  Game is virtual");

  (void)is_duty;

  return ::ui->reservation_get(*this);
} // Reservation Human::reservation_get(bool is_duty = false)

/**
 **
 ** the game is started
 **
 ** @param       -
 **
 ** @return      -
 **
 ** @author      Diether Knof
 **
 ** @version     0.6.6
 **
 **/
void
Human::game_start() 
{ 
  if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY)) {
    if (this->game().rule()(Rule::SWINES_ANNOUNCEMENT_BEGIN))
      if (this->game().swines_announcement_valid(*this))
	this->game().swines_announce(*this);
    
  } // if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY))

  return ;
} // void Human::game_start()

/**
 **
 ** returns save announcements
 **
 ** @param	-
 **
 ** @return	safe announcement
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.1
 **
 **/
Announcement
Human::announcement_request() const
{
#ifdef OUTDATED
  // 2004-05-06 (0.6.4)
  // no automatic announcement

  // points of the own team
  unsigned points = 0;

  // count the points, the own team has made 
  for(vector<Player*>::const_iterator player = this->game().player_begin();
      player != this->game().player_end();
      player++)
    if (this->teaminfo(**player) == this->team())
      points += this->game().points_of_player(**player);

  if (points > 240 - 0)
    return ANNOUNCEMENT::NO0;
  if (points > 240 - 30)
    return ANNOUNCEMENT::NO30;
  if (points > 240 - 60)
    return ANNOUNCEMENT::NO60;
  if (points > 240 - 90)
    return ANNOUNCEMENT::NO90;
  if (points > 240 - 120)
    return ANNOUNCEMENT::NO120;
#endif

  return ANNOUNCEMENT::NOANNOUNCEMENT;
} // Announcement Human::announcement_request() const

/**
 **
 ** returns the card played by the human
 **
 ** @param	-
 **
 ** @return	the card choosen by a human player
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.2
 **
 **/
Card
Human::card_get()
{
#ifndef RELEASE
  // print the suggested card to stdout
  cout << this->Ai::card_suggestion() << " --"
    << " suggested card for human" << endl;
#endif

  DEBUG_ASSERTION(!this->game().isvirtual(),
		  "Human::card_get():\n"
		  "  Game is virtual");

  Card const card = ::ui->card_get(*this);

  if (::game_status != GAMESTATUS::GAME_PLAY)
    return Card();

  DEBUG_ASSERTION(card,
		  "Human::card_get():\n"
		  "  result of 'ui->card_get()' is an empty card");

  if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY)) {
    if (card.istrumpace()
	&& !this->game().rule()(Rule::SWINES_ANNOUNCEMENT_BEGIN)
	&& this->game().swines_announcement_valid(*this)) {
      if (this->game().rule()(Rule::SWINE_ONLY_SECOND)) {
	if (this->hand().numberoftrumpaces() == 1)
	  this->game().swines_announce(*this);
      } else {
	this->game().swines_announce(*this);
      }
    }
    if (card.istrumpnine()
	&& !this->game().rule()(Rule::HYPERSWINES_ANNOUNCEMENT_BEGIN)
      && this->game().hyperswines_announcement_valid(*this))
	this->game().hyperswines_announce(*this);
  } // if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY))

  return card;
} // Card Human::card_get()

/**
 **
 ** returns which cards the player shifts
 **
 ** @param	-
 **
 ** @return	the cards that are to be shifted
 **
 ** @author	Diether Knof
 **
 ** @version	0.4.5
 **
 **/
Hand
Human::poverty_shift()
{ 
  return ::ui->poverty_shift(*this);
} // void Human::poverty_shift(Player const& player)

/**
 **
 ** returns whether 'player' accepts the shifted cards
 **
 ** @param	cardno	the number of shifted cards
 **
 ** @return	whether to accept the cards
 **
 ** @author	Diether Knof
 **
 ** @version	0.4.5
 **
 **/
bool
Human::poverty_take_accept(unsigned const cardno)
{ 
  return ::ui->poverty_take_accept(*this, cardno);
} // void Human::poverty_take_accept(unsigned const cardno)

/**
 **
 ** changes the cards from the poverty-player
 **
 ** @param	cards	the cards that are given to the player
 **
 ** @return	the cards that are returned to the poverty-player
 **
 ** @author	Diether Knof
 **
 ** @version	0.4.5
 **
 **/
Hand
Human::poverty_cards_change(Hand const& cards)
{ 
#ifdef WORKAROUND
  // the ai has to update the cards
  Hand const cards_returned = ::ui->poverty_cards_change(*this, cards);
  this->hand_set(this->hand());

  return cards_returned;
#else
  return ::ui->poverty_cards_change(*this, cards);
#endif
} // Hand Human::poverty_cards_change(Hand const& cards)

/**
 **
 ** gets the cards from the partner and add them to the hand
 **
 ** @param	cards	the cards that are given to the player
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.5
 **
 **/
void
Human::poverty_cards_get_back(Hand const& cards)
{ 
  ::ui->poverty_cards_get_back(*this, cards);
#ifdef WORKAROUND
  // the ai has to update the cards
  this->hand_set(this->hand());
#endif

  return ;
} // void Human::poverty_cards_get_back(Hand const& cards)

/**
 **
 ** 'player' has announce hyperswines
 **
 ** @param      player	player that has announced the hyperswines
 **
 ** @return     -
 **
 ** @author     Diether Knof
 **
 ** @version    0.6.6
 **
 **/
void
Human::swines_announced(Player const& player)
{ 
  if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY)) {
  if (this->game().hyperswines_announcement_valid(*this)
      && this->game().rule()(Rule::HYPERSWINES_ANNOUNCEMENT_BEGIN))
    this->game().hyperswines_announce(*this);

  } // if (::setting(Setting::ANNOUNCE_SWINES_AUTOMATICALLY))

  return ;
} // void Human::swines_announced(Player const& player)

/**
 **
 ** genscher: -> result
 **
 ** @param	-
 **
 ** @return	pointer to the new teammate,
 **		NULL if the genscher is not to be announced
 **
 ** @author	Diether Knof
 **
 ** @version	0.5.4
 **
 **/
Player const*
Human::genscher_partner()
{ 
  return ::ui->genscher_partner();
} // Player const* Human::genscher_partner()
