<commit_before>/*
 * $Id$
 *
 * Copyright (c) 2000-2003 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASL.counters;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.KeyStroke;

import VASSAL.command.ChangeTracker;
import VASSAL.command.Command;
import VASSAL.counters.Decorator;
import VASSAL.counters.EditablePiece;
import VASSAL.counters.Embellishment;
import VASSAL.counters.GamePiece;
import VASSAL.counters.KeyCommand;
import VASSAL.counters.PieceEditor;
import VASSAL.counters.SimplePieceEditor;
import VASSAL.tools.SequenceEncoder;
import VASSAL.tools.imageop.Op;
import VASSAL.tools.imageop.ScaledImagePainter;

/**
 * This class draws a turret counter at different offsets depending on the CA
 * and doesn't draw the counter at all if the TCA=CA
 * (unless CE status differs from the default)
 */
public class Turreted extends Embellishment implements EditablePiece {
  public static final String ID = "turr;";

  protected String front,back;
  protected boolean flipped;
  protected boolean rotateWithCounter; // As of VASL 4.2+, vehicles counters rotate in their entirety,
  // rather than just the silhouette
  protected ScaledImagePainter[] frontImages;
  protected ScaledImagePainter[] backImages;

  public Turreted() {
//    this(ID + "tcaCE;tca;S;A;r", null);   // Enable this in VASL 5.0
    this(ID + "tcaCE;tca;SX;AZ", null);
  }

  public Turreted(String type, GamePiece p) {
    setInner(p);
    mySetType(type);
  }

  public void mySetType(String type) {
    String embType;
    if (type.startsWith(Embellishment.ID)
      || type.startsWith(Embellishment.OLD_ID)) {
      embType = type;
    }
    else {
      SequenceEncoder.Decoder st = new SequenceEncoder.Decoder(type, ';');
      st.nextToken();
      front = st.nextToken();
      back = st.nextToken();
      embType = Embellishment.OLD_ID + ";_;" + st.nextToken() + ";TCA cw;"
          + st.nextToken() + ";TCA ccw;0;0";
      if (st.hasMoreTokens()) {
        rotateWithCounter = true;
        embType += ";;;;;;";
      }
      else {
        embType += ";,+ TCA = 1;,+ TCA = 2;,+ TCA = 3;,+ TCA = 4;,+ TCA = 5;,+ TCA = 6";
      }
    }
    super.mySetType(embType);
    frontImages = new ScaledImagePainter[6];
    backImages = new ScaledImagePainter[6];
    for (int i=0;i<6;++i) {
      frontImages[i] = new ScaledImagePainter();
      frontImages[i].setImageName(front+(i+1));
      backImages[i] = new ScaledImagePainter();
      backImages[i].setImageName(back+(i+1));
    }
  }

  public Shape getShape() {
    return piece.getShape();
  }
  
  public boolean isActive() {
    return super.isActive() && (flipped || value != getVehicleCA());
  }
  
  @Override
  public void draw(Graphics g, int x, int y, Component obs, double zoom) {
    imagePainter = flipped ? backImages : frontImages;
    super.draw(g, x, y, obs, zoom);
  }

  protected Image getCurrentImage() throws java.io.IOException {
    if (flipped || value != getVehicleCA()) {
      try {
        return Op.load((flipped ? back : front) + value + ".gif").getImage(null);
      }
      catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }
    else {
      return null;
    }
  }

  public Rectangle getCurrentImageBounds() {
    Rectangle r;
    if (flipped || value != getVehicleCA()) {
      r = new Rectangle(super.getCurrentImageBounds());
      switch (value) {
        case 1:
          r.translate(20, -10);
          break;
        case 2:
          r.translate(20, 5);
          break;
        case 3:
          r.translate(20, 20);
          break;
        case 4:
          r.translate(-10, 20);
          break;
        case 5:
          r.translate(-10, 5);
          break;
        case 6:
          r.translate(-10, -10);
          break;
      }
    }
    else {
      r = new Rectangle();
    }
    return r;
  }

  protected int getVehicleCA() {<commit_msg>Fixed turret position when loading a log file.<commit_after>/*
 * $Id$
 *
 * Copyright (c) 2000-2003 by <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASL.counters;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;

import javax.swing.KeyStroke;

import VASSAL.command.ChangeTracker;
import VASSAL.command.Command;
import VASSAL.counters.Decorator;
import VASSAL.counters.EditablePiece;
import VASSAL.counters.Embellishment;
import VASSAL.counters.GamePiece;
import VASSAL.counters.KeyCommand;
import VASSAL.counters.PieceEditor;
import VASSAL.counters.SimplePieceEditor;
import VASSAL.tools.SequenceEncoder;
import VASSAL.tools.imageop.Op;
import VASSAL.tools.imageop.ScaledImagePainter;

/**
 * This class draws a turret counter at different offsets depending on the CA
 * and doesn't draw the counter at all if the TCA=CA
 * (unless CE status differs from the default)
 */
public class Turreted extends Embellishment implements EditablePiece {
  public static final String ID = "turr;";

  protected String front,back;
  protected boolean flipped;
  protected boolean rotateWithCounter; // As of VASL 4.2+, vehicles counters rotate in their entirety,
  // rather than just the silhouette
  protected ScaledImagePainter[] frontImages;
  protected ScaledImagePainter[] backImages;

  public Turreted() {
//    this(ID + "tcaCE;tca;S;A;r", null);   // Enable this in VASL 5.0
    this(ID + "tcaCE;tca;SX;AZ", null);
  }

  public Turreted(String type, GamePiece p) {
    setInner(p);
    mySetType(type);
  }

  public void mySetType(String type) {
    String embType;
    if (type.startsWith(Embellishment.ID)
      || type.startsWith(Embellishment.OLD_ID)) {
      embType = type;
    }
    else {
      SequenceEncoder.Decoder st = new SequenceEncoder.Decoder(type, ';');
      st.nextToken();
      front = st.nextToken();
      back = st.nextToken();
      embType = Embellishment.OLD_ID + ";_;" + st.nextToken() + ";TCA cw;"
          + st.nextToken() + ";TCA ccw;0;0";
      if (st.hasMoreTokens()) {
        rotateWithCounter = true;
        embType += ";;;;;;";
      }
      else {
        embType += ";,+ TCA = 1;,+ TCA = 2;,+ TCA = 3;,+ TCA = 4;,+ TCA = 5;,+ TCA = 6";
      }
    }
    super.mySetType(embType);
    frontImages = new ScaledImagePainter[6];
    backImages = new ScaledImagePainter[6];
    for (int i=0;i<6;++i) {
      frontImages[i] = new ScaledImagePainter();
      frontImages[i].setImageName(front+(i+1));
      backImages[i] = new ScaledImagePainter();
      backImages[i].setImageName(back+(i+1));
    }
  }

  public Shape getShape() {
    return piece.getShape();
  }
  
  public boolean isActive() {
    return super.isActive() && (flipped || value != getVehicleCA());
  }
  
  @Override
  public void draw(Graphics g, int x, int y, Component obs, double zoom) {
    imagePainter = flipped ? backImages : frontImages;
    super.draw(g, x, y, obs, zoom);
  }

  protected Image getCurrentImage() throws java.io.IOException {
    if (flipped || value != getVehicleCA()) {
      try {
        return Op.load((flipped ? back : front) + value + ".gif").getImage(null);
      }
      catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }
    else {
      return null;
    }
  }

  public Rectangle getCurrentImageBounds() {
    Rectangle r;
    if (flipped || value != getVehicleCA()) {
      imagePainter = flipped ? backImages : frontImages;
      r = new Rectangle(super.getCurrentImageBounds());
      /*
      switch (value) {
        case 1:
          r.translate(20, -10);
          break;
        case 2:
          r.translate(20, 5);
          break;
        case 3:
          r.translate(20, 20);
          break;
        case 4:
          r.translate(-10, 20);
          break;
        case 5:
          r.translate(-10, 5);
          break;
        case 6:
          r.translate(-10, -10);
          break;
      }
      */
    }
    else {
      r = new Rectangle();
    }
    return r;
  }

  protected int getVehicleCA() {
{'fraction_non_alphanumeric': 0.11723316735224075, 'fraction_numerical': 0.011690842173630657, 'mean_word_length': 2.7989309210526314, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1859549', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2904, 'n_words': 831}