<commit_before>/* *********************************************************************
 *
 *  This file is part of Full Metal Galaxy.
 *  http://www.fullmetalgalaxy.com
 *
 *  Full Metal Galaxy is free software: you can redistribute it and/or 
 *  modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *
 *  Full Metal Galaxy 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public 
 *  License along with Full Metal Galaxy.  
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright 2010, 2011 <NAME>
 *
 * *********************************************************************/
package com.fullmetalgalaxy.model.persist.gamelog;

import com.fullmetalgalaxy.model.EnuColor;
import com.fullmetalgalaxy.model.RpcFmpException;
import com.fullmetalgalaxy.model.TokenType;
import com.fullmetalgalaxy.model.persist.AnBoardPosition;
import com.fullmetalgalaxy.model.persist.EbBase;
import com.fullmetalgalaxy.model.persist.EbRegistration;
import com.fullmetalgalaxy.model.persist.EbToken;
import com.fullmetalgalaxy.model.persist.Game;
import com.fullmetalgalaxy.model.ressources.Messages;


/**
 * @author <NAME>
 *
 */
public class EbEvtControlFreighter extends AnEventPlay
{
  static final long serialVersionUID = 1;

  private EbBase m_packedOldRegistration = null;
  private int m_oldRegistrationSingleColor = EnuColor.None;

  /**
   * 
   */
  public EbEvtControlFreighter()
  {
    super();
    init();
  }

  @Override
  public void reinit()
  {
    super.reinit();
    this.init();
  }

  private void init()
  {
    setCost( 0 );
    setAuto( true );
    m_packedOldRegistration = null;
  }

  @Override
  public GameLogType getType()
  {
    return GameLogType.EvtControlFreighter;
  }


  @Override
  // @Transient
  public AnBoardPosition getSelectedPosition(Game p_game)
  {
    if( getToken(p_game) != null )
    {
      return getToken(p_game).getPosition();
    }
    return null;
  }

  /* (non-Javadoc)
   * @see com.fullmetalgalaxy.model.persist.AnAction#check()
   */
  @Override
  public void check(Game p_game) throws RpcFmpException
  {
    super.check(p_game);

    if( getTokenCarrier(p_game).getType() != TokenType.Freighter )
    {
      // no i18n
      throw new RpcFmpException( "le pion controlé doit être un astronef" );
    }
    if( !getToken(p_game).isNeighbor( getTokenCarrier(p_game) ) )
    {
      // no i18n
      throw new RpcFmpException( getToken(p_game) + " doit etre au contact de " + getTokenCarrier(p_game)
          + " pour le controler" );
    }
    // check that token is colored
    if( getToken(p_game).getColor() == EnuColor.None )
    {
      // no i18n
      throw new RpcFmpException( "vous ne pouvez pas déplacer des pions incolores" );
    }

    // check that player control the token color
    EbRegistration myRegistration = getMyRegistration(p_game);
    assert myRegistration != null;
    if( !myRegistration.getEnuColor().isColored( getToken(p_game).getColor() ) )
    {
      throw new RpcFmpException( errMsg().CantMoveDontControl(
          Messages.getColorString( getAccountId(), getToken( p_game ).getColor() ),
          Messages.getColorString( getAccountId(), myRegistration.getColor() ) ) );
    }
    // check that token isn't under opponents fire covers
    EnuColor fireCoverColor = p_game.getOpponentFireCover( myRegistration.getColor(),
        getToken(p_game).getPosition() );
    if( fireCoverColor.getValue() != EnuColor.None )
    {
      throw new RpcFmpException( errMsg().CantMoveDisableFire( Messages.getTokenString( getAccountId(), getToken(p_game).getType()),
          ( Messages.getColorString( getAccountId(),  fireCoverColor.getValue() ))));
    }
    // check presence of turrets
    for( AnBoardPosition position : getTokenCarrier(p_game).getExtraPositions() )
    {
      EbToken turret = p_game.getToken( position, TokenType.Turret );
      if( turret != null )
      {
        // no i18n ?
        throw new RpcFmpException( "You must destroy all turrets of an opponents freighter before taking control of it." );
      }
    }

    // check that controlling token is a destroyer
    if( !getToken( p_game ).isDestroyer() )
    {
      throw new RpcFmpException( errMsg().OnlyDestroyerCanControl() );
    }

    // player have extra action points.
    assert getCost() == -1 * p_game.getEbConfigGameVariant().getActionPtMaxPerExtraShip();

  }

  /* (non-Javadoc)
   * @see com.fullmetalgalaxy.model.persist.AnAction#exec()
   */
  @Override
  public void exec(Game p_game) throws RpcFmpException
  {
    super.exec(p_game);

    // backup old registration (used by unexec)
    for( EbRegistration registration : p_game.getSetRegistration() )
    {
      EnuColor color = registration.getEnuColor();
      if( color.isColored( getTokenFreighter(p_game).getColor() ) )
      {
        setOldRegistration( registration );
        m_oldRegistrationSingleColor = registration.getSingleColor();
      }
    }
<commit_msg>Issue 122:	we can control freighter with no action point<commit_after>/* *********************************************************************
 *
 *  This file is part of Full Metal Galaxy.
 *  http://www.fullmetalgalaxy.com
 *
 *  Full Metal Galaxy is free software: you can redistribute it and/or 
 *  modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *
 *  Full Metal Galaxy 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public 
 *  License along with Full Metal Galaxy.  
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright 2010, 2011 <NAME>
 *
 * *********************************************************************/
package com.fullmetalgalaxy.model.persist.gamelog;

import com.fullmetalgalaxy.model.EnuColor;
import com.fullmetalgalaxy.model.RpcFmpException;
import com.fullmetalgalaxy.model.TokenType;
import com.fullmetalgalaxy.model.persist.AnBoardPosition;
import com.fullmetalgalaxy.model.persist.EbBase;
import com.fullmetalgalaxy.model.persist.EbRegistration;
import com.fullmetalgalaxy.model.persist.EbToken;
import com.fullmetalgalaxy.model.persist.Game;
import com.fullmetalgalaxy.model.ressources.Messages;


/**
 * @author <NAME>
 *
 */
public class EbEvtControlFreighter extends AnEventPlay
{
  static final long serialVersionUID = 1;

  private EbBase m_packedOldRegistration = null;
  private int m_oldRegistrationSingleColor = EnuColor.None;

  /**
   * 
   */
  public EbEvtControlFreighter()
  {
    super();
    init();
  }

  @Override
  public void reinit()
  {
    super.reinit();
    this.init();
  }

  private void init()
  {
    setCost( 0 );
    setAuto( true );
    m_packedOldRegistration = null;
  }

  @Override
  public GameLogType getType()
  {
    return GameLogType.EvtControlFreighter;
  }


  @Override
  // @Transient
  public AnBoardPosition getSelectedPosition(Game p_game)
  {
    if( getToken(p_game) != null )
    {
      return getToken(p_game).getPosition();
    }
    return null;
  }

  /* (non-Javadoc)
   * @see com.fullmetalgalaxy.model.persist.AnAction#check()
   */
  @Override
  public void check(Game p_game) throws RpcFmpException
  {
    super.check(p_game);

    if( getTokenCarrier(p_game).getType() != TokenType.Freighter )
    {
      // no i18n
      throw new RpcFmpException( "le pion controlé doit être un astronef" );
    }
    if( !getToken(p_game).isNeighbor( getTokenCarrier(p_game) ) )
    {
      // no i18n
      throw new RpcFmpException( getToken(p_game) + " doit etre au contact de " + getTokenCarrier(p_game)
          + " pour le controler" );
    }
    // check that token is colored
    if( getToken(p_game).getColor() == EnuColor.None )
    {
      // no i18n
      throw new RpcFmpException( "vous ne pouvez pas déplacer des pions incolores" );
    }

    // check that player control the token color
    EbRegistration myRegistration = getMyRegistration(p_game);
    assert myRegistration != null;
    if( !myRegistration.getEnuColor().isColored( getToken(p_game).getColor() ) )
    {
      throw new RpcFmpException( errMsg().CantMoveDontControl(
          Messages.getColorString( getAccountId(), getToken( p_game ).getColor() ),
          Messages.getColorString( getAccountId(), myRegistration.getColor() ) ) );
    }
    // check that token isn't under opponents fire covers
    EnuColor fireCoverColor = p_game.getOpponentFireCover( myRegistration.getColor(),
        getToken(p_game).getPosition() );
    if( fireCoverColor.getValue() != EnuColor.None )
    {
      throw new RpcFmpException( errMsg().CantMoveDisableFire( Messages.getTokenString( getAccountId(), getToken(p_game).getType()),
          ( Messages.getColorString( getAccountId(),  fireCoverColor.getValue() ))));
    }
    // check presence of turrets
    for( AnBoardPosition position : getTokenCarrier(p_game).getExtraPositions() )
    {
      EbToken turret = p_game.getToken( position, TokenType.Turret );
      if( turret != null )
      {
        // no i18n ?
        throw new RpcFmpException( "You must destroy all turrets of an opponents freighter before taking control of it." );
      }
    }

    // check that controlling token is a destroyer
    if( !getToken( p_game ).isDestroyer() )
    {
      throw new RpcFmpException( errMsg().OnlyDestroyerCanControl() );
    }

    // check that player have at least one action point to control freighter
    if( myRegistration.getPtAction() <= 0 )
    {
      throw new RpcFmpException( errMsg().NotEnouthActionPt() );
    }

    // player have extra action points.
    assert getCost() == -1 * p_game.getEbConfigGameVariant().getActionPtMaxPerExtraShip();

  }

  /* (non-Javadoc)
   * @see com.fullmetalgalaxy.model.persist.AnAction#exec()
   */
  @Override
  public void exec(Game p_game) throws RpcFmpException
  {
    super.exec(p_game);

    // backup old registration (used by unexec)
    for( EbRegistration registration : p_game.getSetRegistration() )
    {
      EnuColor color = registration.getEnuColor();
      if( color.isColored( getTokenFreighter(p_game).getColor() ) )
      {
        setOldRegistration( registration );
        m_oldRegistrationSingleColor = registration.getSingleColor();
      }
    }

{'fraction_non_alphanumeric': 0.12486208164766459, 'fraction_numerical': 0.004045605001838912, 'mean_word_length': 3.3508, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1920295', 'n_tokens_mistral': 3437, 'n_tokens_neox': 3280, 'n_words': 840}