Skip to content

Commit

Permalink
Import "Set region of interest" from KaTrain 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Dec 25, 2020
1 parent ebeb446 commit 28fe394
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ public void analyzeAvoid(String parameters) {
isPondering = true;
startPonderTime = System.currentTimeMillis();
}
Lizzie.board.regionOfInterest.reset();
sendCommand(
String.format(
"lz-analyze %d %s",
Expand Down Expand Up @@ -743,11 +744,26 @@ public void ponder() {
.config
.getJSONObject("leelaz")
.getInt("analyze-update-interval-centisec")
+ regionOfInterestCommand()
+ (this.isKataGo && Lizzie.config.showKataGoEstimate ? " ownership true" : ""));
// until it responds to this, incoming
// ponder results are obsolete
}

private String regionOfInterestCommand() {
return regionOfInterestCommand("B") + regionOfInterestCommand("W");
}

private String regionOfInterestCommand(String color) {
if (Lizzie.board.regionOfInterest.isEnabled()) {
int untilDepth = 1;
String vertices = Lizzie.board.regionOfInterest.vertices();
return String.format(" allow %s %s %d", color, vertices, untilDepth);
} else {
return "";
}
}

public void togglePonder() {
isPondering = !isPondering;
if (isPondering) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/featurecat/lizzie/gui/BoardPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public BoardPane(LizzieMain owner) {
new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.isAltDown() && e.getButton() == MouseEvent.BUTTON1) {
owner.input.startSettingRegionOfInterest(e);
Lizzie.frame.refresh();
return;
}
if (e.getButton() == MouseEvent.BUTTON1) { // left click
if (e.getClickCount() == 2) { // TODO: Maybe need to delay check
onDoubleClicked(e.getX(), e.getY());
Expand All @@ -123,6 +128,11 @@ public void mousePressed(MouseEvent e) {
}
}

@Override
public void mouseReleased(MouseEvent e) {
owner.input.mouseReleased(e);
}

@Override
public void mouseExited(MouseEvent e) {
onMouseExited(e.getX(), e.getY());
Expand All @@ -140,7 +150,9 @@ public void mouseMoved(MouseEvent e) {
}

@Override
public void mouseDragged(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
owner.input.mouseDragged(e);
}
});
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/featurecat/lizzie/gui/BoardRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import featurecat.lizzie.rules.Board;
import featurecat.lizzie.rules.BoardData;
import featurecat.lizzie.rules.BoardHistoryNode;
import featurecat.lizzie.rules.RegionOfInterest;
import featurecat.lizzie.rules.SGFParser;
import featurecat.lizzie.rules.Stone;
import featurecat.lizzie.rules.Zobrist;
Expand All @@ -33,6 +34,7 @@
import java.awt.RadialGradientPaint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.TexturePaint;
import java.awt.font.TextAttribute;
import java.awt.geom.Point2D;
Expand Down Expand Up @@ -136,6 +138,8 @@ public void draw(Graphics2D g) {

// Stopwatch timer = new Stopwatch();
drawGoban(g);
if (Lizzie.board != null && Lizzie.board.regionOfInterest.isEnabledOrInSetting())
drawRegionOfInterest(g);
if (!isMainBoard) drawSubBoardStatus(g);
if (Lizzie.config.showNameInBoard && isMainBoard) drawName(g);
// timer.lap("background");
Expand Down Expand Up @@ -540,6 +544,31 @@ private void drawStones() {
if (Lizzie.board.inScoreMode()) lastInScoreMode = true;
}

private void drawRegionOfInterest(Graphics2D g) {
Color origColor = g.getColor();
Stroke origStroke = g.getStroke();
double thick = 0.2, margin = 0.5 + thick / 2;
RegionOfInterest rect = Lizzie.board.regionOfInterest;
int x0 = xFor(rect.left() - margin);
int x1 = xFor(rect.right() + margin);
int y0 = yFor(rect.top() - margin);
int y1 = yFor(rect.bottom() + margin);
g.setColor(Color.BLUE);
g.setStroke(new BasicStroke((int) (squareWidth * thick)));
g.drawRoundRect(
x0, y0, x1 - x0, y1 - y0, (int) (squareWidth * 0.5), (int) (squareHeight * 0.5));
g.setColor(origColor);
g.setStroke(origStroke);
}

private int xFor(double i) {
return (int) (x + scaledMarginWidth + squareWidth * i);
}

private int yFor(double j) {
return (int) (y + scaledMarginHeight + squareHeight * j);
}

/*
* Draw a white/black dot on territory and captured stones. Dame is drawn as red dot.
*/
Expand Down Expand Up @@ -591,6 +620,7 @@ private void drawBranch() {
// calculate best moves and branch
bestMoves = Lizzie.leelaz.getBestMoves();
if (Lizzie.config.showBestMovesByHold
&& !Lizzie.board.regionOfInterest.isEnabled()
&& Leelaz.engineIndex == Lizzie.board.getData().engineIndex
&& Lizzie.board.getHistory().getGameInfo().getKomi() == Lizzie.board.getData().komi
&& MoveData.getPlayouts(bestMoves) < Lizzie.board.getData().getPlayouts()) {
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/featurecat/lizzie/gui/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.*;

public class Input implements MouseListener, KeyListener, MouseWheelListener, MouseMotionListener {
@Override
Expand All @@ -19,6 +20,11 @@ public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Lizzie.frame.toolBar.setTxtUnfocus();
if (Lizzie.frame.subBoardOnClick(e)) return;
if (e.isAltDown() && e.getButton() == MouseEvent.BUTTON1) {
startSettingRegionOfInterest(e);
Lizzie.frame.refresh();
return;
}
if (e.getButton() == MouseEvent.BUTTON1) { // left click
if (e.getClickCount() == 2) { // TODO: Maybe need to delay check
Lizzie.frame.onDoubleClicked(e.getX(), e.getY());
Expand All @@ -32,7 +38,9 @@ public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
finishSettingRegionOfInterest();
}

@Override
public void mouseEntered(MouseEvent e) {}
Expand All @@ -42,6 +50,10 @@ public void mouseExited(MouseEvent e) {}

@Override
public void mouseDragged(MouseEvent e) {
if (e.isAltDown()) {
if (updateRegionOfInterest(e)) Lizzie.frame.refresh();
return;
}
Lizzie.frame.onMouseDragged(e.getX(), e.getY());
}

Expand All @@ -53,6 +65,27 @@ public void mouseMoved(MouseEvent e) {
@Override
public void keyTyped(KeyEvent e) {}

public void startSettingRegionOfInterest(MouseEvent e) {
Optional<int[]> coord = mouseToCoord(e);
if (coord.isPresent()) Lizzie.board.regionOfInterest.startSetting(coord.get());
}

private void finishSettingRegionOfInterest() {
if (!Lizzie.board.regionOfInterest.isInSetting()) return;
Lizzie.board.regionOfInterest.finishSetting();
Lizzie.frame.refresh();
if (Lizzie.leelaz.isPondering()) Lizzie.leelaz.ponder();
}

private boolean updateRegionOfInterest(MouseEvent e) {
Optional<int[]> coord = mouseToCoord(e);
return coord.isPresent() && Lizzie.board.regionOfInterest.updateSetting(coord.get());
}

private Optional<int[]> mouseToCoord(MouseEvent e) {
return Lizzie.frame.convertScreenToCoordinates(e.getX(), e.getY());
}

public static void undo() {
undo(1);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/featurecat/lizzie/rules/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class Board implements LeelazListener {
public boolean isAvoding = false;
public boolean isKeepingAvoid = false;

public RegionOfInterest regionOfInterest = new RegionOfInterest();

// Save the node for restore move when in the branch
private Optional<BoardHistoryNode> saveNode;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/featurecat/lizzie/rules/BoardData.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public void tryToClearBestMoves() {
}

public void tryToSetBestMoves(List<MoveData> moves) {
if (!Lizzie.board.regionOfInterest.isEnabled()) return;
if (MoveData.getPlayouts(moves) > playouts) {
bestMoves = moves;
setPlayouts(MoveData.getPlayouts(moves));
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/featurecat/lizzie/rules/RegionOfInterest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package featurecat.lizzie.rules;

import featurecat.lizzie.Lizzie;
import java.util.StringJoiner;

public class RegionOfInterest {
private String vertices;
private int[] startCorner;
private int left, right, top, bottom;

public RegionOfInterest() {
reset();
}

public void reset() {
vertices = "";
startCorner = null;
}

public String vertices() {
return vertices;
}

public Boolean isEnabled() {
return !vertices.isEmpty();
}

public Boolean isEnabledOrInSetting() {
return isEnabled() || isInSetting();
}

public int left() {
return left;
}

public int right() {
return right;
}

public int top() {
return top;
}

public int bottom() {
return bottom;
}

public void startSetting(int[] corner) {
startCorner = corner;
if (startCorner == null) return;
left = right = startCorner[0];
top = bottom = startCorner[1];
}

public Boolean isInSetting() {
return startCorner != null;
}

public Boolean updateSetting(int[] endCorner) {
int[] a = startCorner, b = endCorner;
if (a == null || b == null) return false;
int i0 = Math.min(a[0], b[0]), j0 = Math.min(a[1], b[1]);
int i1 = Math.max(a[0], b[0]), j1 = Math.max(a[1], b[1]);
boolean changed = (left != i0) || (right != i1) || (top != j0) || (bottom != j1);
left = i0;
right = i1;
top = j0;
bottom = j1;
return changed;
}

public void finishSetting() {
vertices = verticesInRectangle();
startCorner = null;
}

private String verticesInRectangle() {
if (left == right && top == bottom) return "";
StringJoiner sj = new StringJoiner(",");
for (int i = left; i <= right; i++)
for (int j = top; j <= bottom; j++) sj.add(Lizzie.board.convertCoordinatesToName(i, j));
return sj.toString();
}
}

0 comments on commit 28fe394

Please sign in to comment.