package com.dairymoose.modernlife.blocks.gui.chess;

import net.minecraft.client.gui.components.Button;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/Piece.class */
public abstract class Piece {

    /* renamed from: x */
    public int f9x;

    /* renamed from: y */
    public int f10y;
    Button button;
    ChessScreen screen;
    boolean black;
    boolean top;
    int initialX = -1;
    int initialY = -1;
    boolean ignoreCheckRules = false;

    public abstract int getId();

    public abstract boolean canMove(int i, int i2);

    public Piece withXy(int x, int y) {
        this.f9x = x;
        this.f10y = y;
        return this;
    }

    protected boolean isOutOfBounds(int x, int y) {
        if (x < 0) {
            return true;
        }
        ChessScreen chessScreen = this.screen;
        if (x >= 8 || y < 0) {
            return true;
        }
        ChessScreen chessScreen2 = this.screen;
        if (y >= 8) {
            return true;
        }
        return false;
    }

    public boolean wouldCollide(int x, int y) {
        if (isOutOfBounds(x, y)) {
            return true;
        }
        if (this.f9x == x && this.f10y == y) {
            return true;
        }
        int currentX = this.f9x;
        int i = this.f10y;
        while (true) {
            int currentY = i;
            int xDiff = x - currentX;
            int yDiff = y - currentY;
            int moveX = xDiff == 0 ? 0 : xDiff / Math.abs(xDiff);
            int moveY = yDiff == 0 ? 0 : yDiff / Math.abs(yDiff);
            if (currentX == x && currentY == y) {
                return false;
            }
            if ((currentX != this.f9x || currentY != this.f10y) && this.screen != null) {
                Piece p = this.screen.getPieceAt(currentX, currentY);
                if (p != null) {
                    return true;
                }
            }
            currentX += moveX;
            i = currentY + moveY;
        }
    }

    public void absMoveTo(int x, int y) {
        this.button.f_93620_ = this.screen.boardXtoScreenX(x);
        this.button.f_93621_ = this.screen.boardYtoScreenY(y);
        this.f9x = x;
        this.f10y = y;
    }

    private boolean wouldPutKingInCheck(int x, int y, KingPiece king) {
        int thisOriginalX = this.f9x;
        int thisOriginalY = this.f10y;
        int pOriginalX = -1;
        int pOriginalY = -1;
        Piece p = this.screen.getPieceAt(x, y);
        if (p != null) {
            pOriginalX = p.f9x;
            pOriginalY = p.f10y;
            if (this.black != p.black) {
                p.f9x = -1;
                p.f10y = -1;
            }
        }
        this.f9x = x;
        this.f10y = y;
        boolean kingInCheck = king.inCheck();
        if (p != null) {
            p.f9x = pOriginalX;
            p.f10y = pOriginalY;
        }
        this.f9x = thisOriginalX;
        this.f10y = thisOriginalY;
        return kingInCheck;
    }

    private boolean internalDoMove(int x, int y, boolean simulate) {
        if (isOutOfBounds(x, y)) {
            return false;
        }
        if (!this.ignoreCheckRules) {
            if (this.black) {
                if (wouldPutKingInCheck(x, y, this.screen.blackKing)) {
                    return false;
                }
            } else if (wouldPutKingInCheck(x, y, this.screen.whiteKing)) {
                return false;
            }
        }
        if (this.screen != null) {
            Piece p = this.screen.getPieceAt(x, y);
            if (p != null) {
                if (this.black == (!p.black)) {
                    if (!simulate) {
                        kill(p);
                    }
                } else {
                    return false;
                }
            }
            if (!simulate) {
                absMoveTo(x, y);
                return true;
            }
            return true;
        }
        return true;
    }

    protected boolean doMove(int x, int y) {
        return internalDoMove(x, y, false);
    }

    public boolean canDoMove(int x, int y) {
        return internalDoMove(x, y, true);
    }

    public boolean move(int x, int y) {
        if (canMove(x, y)) {
            return doMove(x, y);
        }
        return false;
    }

    public void kill(Piece piece) {
        piece.remove();
    }

    public void remove() {
        this.button.f_93623_ = false;
        this.button.f_93624_ = false;
        this.f9x = -1;
        this.f10y = -1;
        if (this.screen != null) {
            if (this.black) {
                this.screen.blackPieces.remove(this);
            } else {
                this.screen.whitePieces.remove(this);
            }
        }
    }
}
