// Decompiled by DJ v3.0.0.63 Copyright 2002 Atanas Neshkov  Date: 11/09/2002 17:30:50
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SPlayfield.java

import java.applet.Applet;
import java.awt.*;

public class SPlayfield extends Canvas
{

    public SPlayfield(Applet applet, SnakeConfig snakeconfig)
    {
        goFont = new Font("Helvetica", 1, 12);
        snakeConfig = snakeconfig;
        gridDimension = new Dimension();
        gridInset = new Dimension();
        playfield = Snake7110.playfieldImage;
        gridDimension.width = Snake7110.gridWidth;
        gridDimension.height = Snake7110.gridHeight;
        ownerApplet = applet;
        setBackground(snakeConfig.getBackgroundColor());
    }

    public void initialize()
    {
        fieldDimension = new Dimension(188, 122);
        offImage = createImage(fieldDimension.width, fieldDimension.height);
        offGraphics = offImage.getGraphics();
        offGraphics.setFont(goFont);
        offGraphics.setColor(snakeConfig.getBackgroundColor());
        offGraphics.fillRect(0, 0, fieldDimension.width, fieldDimension.height);
        offGraphics.setColor(PF_SNAKE);
        offGraphics.drawLine(0, 12, fieldDimension.width, 12);
        offGraphics.drawRect(0, 14, fieldDimension.width - 1, fieldDimension.height - 15);
        offGraphics.drawImage(playfield, 2, 16, fieldDimension.width - 4, fieldDimension.height - 2 - 16, Color.red, this);
        offGraphics.setColor(PF_SNAKE);
        squareSide = (fieldDimension.width - 4) / gridDimension.width;
        thinWalls();
        gridArray = new int[gridDimension.width * gridDimension.height];
        for(int i = 0; i < gridArray.length; i++)
            gridArray[i] = 0;

        showScore(0);
    }

    public void clearOffscreen()
    {
        offImage = null;
        offGraphics = null;
    }

    public void update(Graphics g)
    {
        reshape(0, 0, 188, 122);
        if(offImage == null)
            initialize();
        g.drawImage(offImage, 0, 0, snakeConfig.getBackgroundColor(), this);
    }

    public void paint(Graphics g)
    {
        update(g);
    }

    private void thinWalls()
    {
        boolean aflag[][] = Snake7110.playarea_map;
        offGraphics.setColor(snakeConfig.getBackgroundColor());
        for(int i = 0; i < gridDimension.width; i++)
        {
            for(int j = 0; j < gridDimension.height; j++)
                if(aflag[i][j])
                {
                    try
                    {
                        if(!aflag[i - 1][j - 1])
                            thinWallUpLeft(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallUpLeft(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallUpLeft(i, j);
                    }
                    try
                    {
                        if(!aflag[i][j - 1])
                            thinWallUp(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallUp(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallUp(i, j);
                    }
                    try
                    {
                        if(!aflag[i + 1][j - 1])
                            thinWallUpRight(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallUpRight(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallUpRight(i, j);
                    }
                    try
                    {
                        if(!aflag[i - 1][j])
                            thinWallLeft(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallLeft(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallLeft(i, j);
                    }
                    try
                    {
                        if(!aflag[i + 1][j])
                            thinWallRight(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallRight(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallRight(i, j);
                    }
                    try
                    {
                        if(!aflag[i - 1][j + 1])
                            thinWallDownLeft(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallDownLeft(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallDownLeft(i, j);
                    }
                    try
                    {
                        if(!aflag[i][j + 1])
                            thinWallDown(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallDown(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallDown(i, j);
                    }
                    try
                    {
                        if(!aflag[i + 1][j + 1])
                            thinWallDownRight(i, j);
                    }
                    catch(ArrayIndexOutOfBoundsException _ex)
                    {
                        thinWallDownRight(i, j);
                    }
                    catch(NullPointerException _ex)
                    {
                        thinWallDownRight(i, j);
                    }
                }

        }

    }

    private void thinWallUpLeft(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width, dimension.height, 1, 1);
    }

    private void thinWallUp(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width, dimension.height, squareSide - 1, 1);
    }

    private void thinWallUpRight(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width + (squareSide - 2), dimension.height, 1, 1);
    }

    private void thinWallLeft(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width, dimension.height, 1, squareSide - 1);
    }

    private void thinWallRight(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width + (squareSide - 2), dimension.height, 1, squareSide - 1);
    }

    private void thinWallDownLeft(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width, dimension.height + (squareSide - 2), 1, 1);
    }

    private void thinWallDown(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width, dimension.height + (squareSide - 2), squareSide - 1, 1);
    }

    private void thinWallDownRight(int i, int j)
    {
        Dimension dimension = gridToPixels(i, j);
        offGraphics.drawRect(dimension.width + (squareSide - 2), dimension.height + (squareSide - 2), 1, 1);
    }

    public Dimension getGridDimension()
    {
        return gridDimension;
    }

    public int getSquareContent(int i, int j)
    {
        try
        {
            return gridArray[j * gridDimension.width + i];
        }
        catch(IndexOutOfBoundsException _ex)
        {
            return -1;
        }
        catch(NullPointerException _ex)
        {
            return -1;
        }
    }

    public int setSquareContent(int i, int j, int k)
    {
        try
        {
            gridArray[j * gridDimension.width + i] = k;
            drawSquareContent(i, j);
            return 0;
        }
        catch(IndexOutOfBoundsException _ex)
        {
            return -1;
        }
        catch(NullPointerException _ex)
        {
            return -1;
        }
    }

    private void drawSquareContent(int i, int j)
    {
        if(offGraphics == null)
            return;
        Dimension dimension = gridToPixels(i, j);
        switch(getSquareContent(i, j))
        {
        case -1: 
            return;

        case 0: // '\0'
            offGraphics.setColor(snakeConfig.getBackgroundColor());
            offGraphics.fillRect(dimension.width, dimension.height, squareSide, squareSide);
            return;

        case 128: 
            drawSnakePiece(Snake7110.head_down, dimension);
            return;

        case 129: 
            drawSnakePiece(Snake7110.head_left, dimension);
            return;

        case 130: 
            drawSnakePiece(Snake7110.head_right, dimension);
            return;

        case 131: 
            drawSnakePiece(Snake7110.head_up, dimension);
            return;

        case 132: 
            drawSnakePiece(Snake7110.mouth_down, dimension);
            return;

        case 133: 
            drawSnakePiece(Snake7110.mouth_left, dimension);
            return;

        case 134: 
            drawSnakePiece(Snake7110.mouth_right, dimension);
            return;

        case 135: 
            drawSnakePiece(Snake7110.mouth_up, dimension);
            return;

        case 136: 
            drawSnakePiece(Snake7110.body_down, dimension);
            return;

        case 137: 
            drawSnakePiece(Snake7110.body_left, dimension);
            return;

        case 138: 
            drawSnakePiece(Snake7110.body_right, dimension);
            return;

        case 139: 
            drawSnakePiece(Snake7110.body_up, dimension);
            return;

        case 140: 
            drawSnakePiece(Snake7110.tail_down, dimension);
            return;

        case 141: 
            drawSnakePiece(Snake7110.tail_left, dimension);
            return;

        case 142: 
            drawSnakePiece(Snake7110.tail_right, dimension);
            return;

        case 143: 
            drawSnakePiece(Snake7110.tail_up, dimension);
            return;

        case 144: 
            drawSnakePiece(Snake7110.down_left_corner, dimension);
            return;

        case 145: 
            drawSnakePiece(Snake7110.down_right_corner, dimension);
            return;

        case 146: 
            drawSnakePiece(Snake7110.up_left_corner, dimension);
            return;

        case 147: 
            drawSnakePiece(Snake7110.up_right_corner, dimension);
            return;

        case 148: 
            drawSnakePiece(Snake7110.food_eaten_down, dimension);
            return;

        case 149: 
            drawSnakePiece(Snake7110.food_eaten_up, dimension);
            return;

        case 150: 
            drawSnakePiece(Snake7110.food_eaten_left, dimension);
            return;

        case 151: 
            drawSnakePiece(Snake7110.food_eaten_right, dimension);
            return;

        case 152: 
            drawSnakePiece(Snake7110.food_eaten_down_left_corner, dimension);
            return;

        case 153: 
            drawSnakePiece(Snake7110.food_eaten_down_right_corner, dimension);
            return;

        case 154: 
            drawSnakePiece(Snake7110.food_eaten_up_left_corner, dimension);
            return;

        case 155: 
            drawSnakePiece(Snake7110.food_eaten_up_right_corner, dimension);
            return;

        case 2: // '\002'
            offGraphics.setColor(PF_SNAKE);
            offGraphics.drawImage(Snake7110.food, dimension.width + 1, dimension.height + 1, squareSide - 1, squareSide - 1, snakeConfig.getBackgroundColor(), this);
            return;

        case 100: // 'd'
            drawBonus(Snake7110.bonus_turtle, dimension.width, dimension.height);
            return;

        case 101: // 'e'
            drawBonus(Snake7110.bonus_worm, dimension.width, dimension.height);
            return;

        case 102: // 'f'
            drawBonus(Snake7110.bonus_snail, dimension.width, dimension.height);
            return;

        case 103: // 'g'
            drawBonus(Snake7110.bonus_spider, dimension.width, dimension.height);
            return;

        case 104: // 'h'
            drawBonus(Snake7110.bonus_fluffy, dimension.width, dimension.height);
            return;

        case 105: // 'i'
            drawBonus(Snake7110.bonus_lizard, dimension.width, dimension.height);
            return;
        }
    }

    private void drawSnakePiece(Image image, Dimension dimension)
    {
        offGraphics.setColor(snakeConfig.getBackgroundColor());
        offGraphics.fillRect(dimension.width, dimension.height, squareSide, squareSide);
        offGraphics.setColor(PF_SNAKE);
        offGraphics.drawImage(image, dimension.width, dimension.height, squareSide, squareSide, snakeConfig.getBackgroundColor(), this);
    }

    public Dimension gridToPixels(int i, int j)
    {
        int k = i * squareSide + 2;
        int l = j * squareSide + 2 + 14;
        return new Dimension(k, l);
    }

    public void showScore(int i)
    {
        if(i > 999)
        {
            String s = Integer.toString(i).substring(0, 1);
            showNumberAt(Integer.valueOf(s).intValue(), 1);
            s = Integer.toString(i).substring(1, 2);
            showNumberAt(Integer.valueOf(s).intValue(), 9);
            s = Integer.toString(i).substring(2, 3);
            showNumberAt(Integer.valueOf(s).intValue(), 17);
            s = Integer.toString(i).substring(3, 4);
            showNumberAt(Integer.valueOf(s).intValue(), 25);
            return;
        }
        if(i > 99)
        {
            showNumberAt(0, 1);
            String s1 = Integer.toString(i).substring(0, 1);
            showNumberAt(Integer.valueOf(s1).intValue(), 9);
            s1 = Integer.toString(i).substring(1, 2);
            showNumberAt(Integer.valueOf(s1).intValue(), 17);
            s1 = Integer.toString(i).substring(2, 3);
            showNumberAt(Integer.valueOf(s1).intValue(), 25);
            return;
        }
        if(i > 9)
        {
            showNumberAt(0, 1);
            showNumberAt(0, 9);
            String s2 = Integer.toString(i).substring(0, 1);
            showNumberAt(Integer.valueOf(s2).intValue(), 17);
            s2 = Integer.toString(i).substring(1, 2);
            showNumberAt(Integer.valueOf(s2).intValue(), 25);
            return;
        }
        if(i > 0)
        {
            showNumberAt(0, 1);
            showNumberAt(0, 9);
            showNumberAt(0, 17);
            showNumberAt(i, 25);
            return;
        } else
        {
            showNumberAt(0, 1);
            showNumberAt(0, 9);
            showNumberAt(0, 17);
            showNumberAt(0, 25);
            return;
        }
    }

    void showNumberAt(int i, int j)
    {
        Image image = null;
        offGraphics.setColor(PF_SNAKE);
        switch(i)
        {
        case 0: // '\0'
            image = Snake7110.zero;
            break;

        case 1: // '\001'
            image = Snake7110.one;
            break;

        case 2: // '\002'
            image = Snake7110.two;
            break;

        case 3: // '\003'
            image = Snake7110.three;
            break;

        case 4: // '\004'
            image = Snake7110.four;
            break;

        case 5: // '\005'
            image = Snake7110.five;
            break;

        case 6: // '\006'
            image = Snake7110.six;
            break;

        case 7: // '\007'
            image = Snake7110.seven;
            break;

        case 8: // '\b'
            image = Snake7110.eight;
            break;

        case 9: // '\t'
            image = Snake7110.nine;
            break;
        }
        offGraphics.drawImage(image, j, 1, 6, 10, snakeConfig.getBackgroundColor(), this);
    }

    private void drawBonus(Image image, int i, int j)
    {
        offGraphics.setColor(PF_SNAKE);
        offGraphics.drawImage(image, i, j, 2 * squareSide, squareSide, snakeConfig.getBackgroundColor(), this);
    }

    public void showBonusCounter(int i, SPill spill)
    {
        int j = 0;
        if(i >= 10)
            j = (int)Math.floor(i / 10);
        int k = i - j * 10;
        switch(spill.getPillType())
        {
        case 100: // 'd'
            drawBonus(Snake7110.bonus_turtle, 153, 1);
            break;

        case 101: // 'e'
            drawBonus(Snake7110.bonus_worm, 153, 1);
            break;

        case 102: // 'f'
            drawBonus(Snake7110.bonus_snail, 153, 1);
            break;

        case 103: // 'g'
            drawBonus(Snake7110.bonus_spider, 153, 1);
            break;

        case 104: // 'h'
            drawBonus(Snake7110.bonus_fluffy, 153, 1);
            break;

        case 105: // 'i'
            drawBonus(Snake7110.bonus_lizard, 153, 1);
            break;
        }
        showNumberAt(j, 173);
        showNumberAt(k, 181);
    }

    public void removeBonusCounter()
    {
        offGraphics.setColor(snakeConfig.getBackgroundColor());
        offGraphics.fillRect(153, 0, 36, 11);
    }

    public void displayGameOver(int i)
    {
        String s = snakeConfig.getScoreText() + i;
        offGraphics.setColor(snakeConfig.getBackgroundColor());
        offGraphics.fillRect(0, 0, fieldDimension.width, fieldDimension.height);
        offGraphics.setColor(PF_SNAKE);
        offGraphics.drawString(snakeConfig.getGameOverText(), 12, 18);
        String as[] = GameUtil.wrapText(offGraphics, snakeConfig.getClickToPlayText(), snakeConfig.getTextMaxLength());
        offGraphics.drawString(as[0], 12, 74);
        if(as[1].length() > 0)
            offGraphics.drawString(as[1].trim(), 12, 74 + (offGraphics.getFontMetrics(goFont).getHeight() + 4));
        Graphics g = offImage.getGraphics();
        g.setColor(snakeConfig.getBackgroundColor());
        g.fillRect(12, 30, 100, 10);
        offGraphics.drawString(s, 12, 36);
        for(int j = 0; j < 6; j++)
            try
            {
                Thread.sleep(150L);
                offGraphics.drawImage(Snake7110.endAnimImages[j], 140, 4, 44, 52, snakeConfig.getBackgroundColor(), this);
                repaint();
            }
            catch(InterruptedException interruptedexception)
            {
                interruptedexception.printStackTrace();
            }

        g = null;
        System.gc();
        repaint();
    }

    static final int SC_ERROR = -1;
    static final int SC_EMPTY = 0;
    static final int SC_IS_SNAKE_PIECE = 1;
    static final int SC_PILL = 2;
    static final int SC_BONUS = 3;
    static final int SC_WALL = 4;
    static final int SC_BONUS_TURTLE = 100;
    static final int SC_BONUS_WORM = 101;
    static final int SC_BONUS_SNAIL = 102;
    static final int SC_BONUS_SPIDER = 103;
    static final int SC_BONUS_FLUFFY = 104;
    static final int SC_BONUS_LIZARD = 105;
    static final int SC_SNAKE_HEAD_DOWN = 128;
    static final int SC_SNAKE_HEAD_LEFT = 129;
    static final int SC_SNAKE_HEAD_RIGHT = 130;
    static final int SC_SNAKE_HEAD_UP = 131;
    static final int SC_SNAKE_MOUTH_DOWN = 132;
    static final int SC_SNAKE_MOUTH_LEFT = 133;
    static final int SC_SNAKE_MOUTH_RIGHT = 134;
    static final int SC_SNAKE_MOUTH_UP = 135;
    static final int SC_SNAKE_BODY_DOWN = 136;
    static final int SC_SNAKE_BODY_LEFT = 137;
    static final int SC_SNAKE_BODY_RIGHT = 138;
    static final int SC_SNAKE_BODY_UP = 139;
    static final int SC_SNAKE_TAIL_DOWN = 140;
    static final int SC_SNAKE_TAIL_LEFT = 141;
    static final int SC_SNAKE_TAIL_RIGHT = 142;
    static final int SC_SNAKE_TAIL_UP = 143;
    static final int SC_DOWN_LEFT_CORNER = 144;
    static final int SC_DOWN_RIGHT_CORNER = 145;
    static final int SC_UP_LEFT_CORNER = 146;
    static final int SC_UP_RIGHT_CORNER = 147;
    static final int SC_EATEN_DOWN = 148;
    static final int SC_EATEN_UP = 149;
    static final int SC_EATEN_LEFT = 150;
    static final int SC_EATEN_RIGHT = 151;
    static final int SC_EATEN_DOWN_LEFT_CORNER = 152;
    static final int SC_EATEN_DOWN_RIGHT_CORNER = 153;
    static final int SC_EATEN_UP_LEFT_CORNER = 154;
    static final int SC_EATEN_UP_RIGHT_CORNER = 155;
    static final Color PF_GRID;
    static final Color PF_SNAKE = new Color(30, 30, 30);
    static final int MARGIN_PIX = 4;
    Dimension fieldDimension;
    Image offImage;
    Graphics offGraphics;
    Applet ownerApplet;
    Image playfield;
    int squareSide;
    Dimension gridDimension;
    private Dimension gridInset;
    Font goFont;
    int gridArray[];
    SnakeConfig snakeConfig;

    static 
    {
        PF_GRID = Color.gray;
    }
}