// Decompiled by DJ v3.0.0.63 Copyright 2002 Atanas Neshkov  Date: 11/09/2002 17:36:53
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Snake7110.java

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.PrintStream;

public class Snake7110 extends Applet
{

    public void init()
    {
        String s = getParameter("CONFIG_FILE");
        if(s != null)
        {
            snakeConfig = new SnakeConfig(s);
        } else
        {
            System.err.println("\nWarning!  Applet parameter CONFIG_FILE not specified.\nUsing default settings...\n");
            snakeConfig = new SnakeConfig();
        }
        s = getParameter("SRV_URL");
        if(s != null)
        {
            snakeConfig.setServerUrl(s);
            System.err.println("SRV_URL defined in the applet parameters.");
        }
        startAnimImage = getImage(getCodeBase(), "anim.gif");
        MediaTracker mediatracker = new MediaTracker(this);
        mediatracker.addImage(startAnimImage, 0);
        try
        {
            mediatracker.waitForID(0);
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        if(mediatracker.isErrorID(0))
            System.err.println("ERROR: An image required by the applet could not be loaded: anim.gif");
        bkImg = createImage(188, 122);
        bkImg = imageGrabToRGB(bkImg, 0, 0, 188, 122);
        setLayout(null);
        startAnim1 = imageGrabToRGB(startAnimImage, 0, 0, 96, 64);
        startAnim2 = imageGrabToRGB(startAnimImage, 96, 0, 96, 64);
        startAnim3 = imageGrabToRGB(startAnimImage, 192, 0, 96, 64);
        startAnim4 = imageGrabToRGB(startAnimImage, 0, 64, 96, 64);
        startAnim5 = imageGrabToRGB(startAnimImage, 96, 64, 96, 64);
        startAnim6 = imageGrabToRGB(startAnimImage, 192, 64, 96, 64);
        startAnimImages = new Image[6];
        startAnimImages[0] = startAnim1;
        startAnimImages[1] = startAnim2;
        startAnimImages[2] = startAnim3;
        startAnimImages[3] = startAnim4;
        startAnimImages[4] = startAnim5;
        startAnimImages[5] = startAnim6;
        ga = new GameAnimator(startAnimImages, 188, 122, 100, 700, snakeConfig.getBackgroundColor());
        add(ga);
        validate();
        ga.reshape(0, 0, 188, 122);
        validate();
        animationThread = new Thread(ga);
        animationThread.start();
        show(true);
        validate();
        repaint();
        playfieldImage = getImage(getCodeBase(), "playfield.gif");
        pieces = getImage(getCodeBase(), "pieces.gif");
        endAnimImage = getImage(getCodeBase(), "gameover.gif");
        MediaTracker mediatracker1 = new MediaTracker(this);
        mediatracker1.addImage(playfieldImage, 0);
        mediatracker1.addImage(pieces, 1);
        mediatracker1.addImage(endAnimImage, 2);
        try
        {
            mediatracker1.waitForAll();
        }
        catch(Exception exception1)
        {
            exception1.printStackTrace();
        }
        if(mediatracker1.isErrorID(0))
            System.err.println("ERROR: An image required by the applet could not be loaded: playfield.gif");
        if(mediatracker1.isErrorID(1))
            System.err.println("ERROR: An image required by the applet could not be loaded: pieces.gif");
        if(mediatracker1.isErrorID(2))
            System.err.println("ERROR: An image required by the applet could not be loaded: gameover.gif");
        playarea_map = imageToBooleanMap(playfieldImage);
        mouth_down = imageGrabToRGB(pieces, 4, 0, 4, 4);
        mouth_left = imageGrabToRGB(pieces, 0, 4, 4, 4);
        mouth_right = imageGrabToRGB(pieces, 4, 4, 4, 4);
        mouth_up = imageGrabToRGB(pieces, 0, 0, 4, 4);
        head_down = imageGrabToRGB(pieces, 12, 0, 4, 4);
        head_left = imageGrabToRGB(pieces, 8, 4, 4, 4);
        head_right = imageGrabToRGB(pieces, 12, 4, 4, 4);
        head_up = imageGrabToRGB(pieces, 8, 0, 4, 4);
        body_down = imageGrabToRGB(pieces, 20, 0, 4, 4);
        body_left = imageGrabToRGB(pieces, 16, 4, 4, 4);
        body_right = imageGrabToRGB(pieces, 20, 4, 4, 4);
        body_up = imageGrabToRGB(pieces, 16, 0, 4, 4);
        tail_down = imageGrabToRGB(pieces, 28, 0, 4, 4);
        tail_left = imageGrabToRGB(pieces, 24, 4, 4, 4);
        tail_right = imageGrabToRGB(pieces, 28, 4, 4, 4);
        tail_up = imageGrabToRGB(pieces, 24, 0, 4, 4);
        bonus_fluffy = imageGrabToRGB(pieces, 16, 8, 8, 4);
        bonus_lizard = imageGrabToRGB(pieces, 24, 8, 8, 4);
        bonus_snail = imageGrabToRGB(pieces, 16, 12, 8, 4);
        bonus_spider = imageGrabToRGB(pieces, 24, 12, 8, 4);
        bonus_turtle = imageGrabToRGB(pieces, 32, 8, 8, 4);
        bonus_worm = imageGrabToRGB(pieces, 32, 12, 8, 4);
        down_left_corner = imageGrabToRGB(pieces, 32, 4, 4, 4);
        down_right_corner = imageGrabToRGB(pieces, 36, 4, 4, 4);
        up_left_corner = imageGrabToRGB(pieces, 32, 0, 4, 4);
        up_right_corner = imageGrabToRGB(pieces, 36, 0, 4, 4);
        food = imageGrabToRGB(pieces, 30, 16, 3, 4);
        food_eaten_down = imageGrabToRGB(pieces, 4, 8, 4, 4);
        food_eaten_up = imageGrabToRGB(pieces, 0, 8, 4, 4);
        food_eaten_left = imageGrabToRGB(pieces, 0, 12, 4, 4);
        food_eaten_right = imageGrabToRGB(pieces, 4, 12, 4, 4);
        food_eaten_down_left_corner = imageGrabToRGB(pieces, 8, 12, 4, 4);
        food_eaten_down_right_corner = imageGrabToRGB(pieces, 12, 12, 4, 4);
        food_eaten_up_left_corner = imageGrabToRGB(pieces, 8, 8, 4, 4);
        food_eaten_up_right_corner = imageGrabToRGB(pieces, 12, 8, 4, 4);
        zero = imageGrabToRGB(pieces, 0, 16, 3, 5);
        one = imageGrabToRGB(pieces, 3, 16, 3, 5);
        two = imageGrabToRGB(pieces, 6, 16, 3, 5);
        three = imageGrabToRGB(pieces, 9, 16, 3, 5);
        four = imageGrabToRGB(pieces, 12, 16, 3, 5);
        five = imageGrabToRGB(pieces, 15, 16, 3, 5);
        six = imageGrabToRGB(pieces, 18, 16, 3, 5);
        seven = imageGrabToRGB(pieces, 21, 16, 3, 5);
        eight = imageGrabToRGB(pieces, 24, 16, 3, 5);
        nine = imageGrabToRGB(pieces, 27, 16, 3, 5);
        endAnim1 = imageGrabToRGB(endAnimImage, 0, 0, 22, 26);
        endAnim2 = imageGrabToRGB(endAnimImage, 22, 0, 22, 26);
        endAnim3 = imageGrabToRGB(endAnimImage, 44, 0, 22, 26);
        endAnim4 = imageGrabToRGB(endAnimImage, 66, 0, 22, 26);
        playfieldImage = imageGrabToRGB(playfieldImage, 0, 0, 23, 13);
        endAnimImages = new Image[6];
        endAnimImages[0] = endAnim1;
        endAnimImages[1] = endAnim2;
        endAnimImages[2] = endAnim3;
        endAnimImages[3] = endAnim4;
        endAnimImages[4] = endAnim3;
        endAnimImages[5] = endAnim1;
        eatPillClip = getAudioClip(getCodeBase(), "pill.au");
        eatPillClip.play();
        deathClip = getAudioClip(getCodeBase(), "gameover.au");
        deathClip.play();
        playfield = new SPlayfield(this, snakeConfig);
        levelUI = new GameLevel(9, SPlayfield.PF_SNAKE, snakeConfig.getBackgroundColor(), snakeConfig.getLevelText(), snakeConfig.getSelectLevelText(), 164);
        try
        {
            animationThread.join(3000L);
        }
        catch(InterruptedException interruptedexception)
        {
            interruptedexception.printStackTrace();
        }
        remove(ga);
        add(levelUI);
        levelUI.reshape(0, 0, 188, 122);
        validate();
        repaint();
    }

    public void start()
    {
        requestFocus();
    }

    public void stop()
    {
        if(gameThread != null)
            gameThread.stop();
    }

    public void paint(Graphics g)
    {
        g.drawImage(bkImg, 0, 0, snakeConfig.getBackgroundColor(), this);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public boolean mouseUp(Event event, int i, int j)
    {
        switch(state)
        {
        case 0: // '\0'
            startGame();
            break;

        case 3: // '\003'
            remove(playfield);
            add(levelUI);
            levelUI.reshape(0, 0, 188, 122);
            validate();
            state = 0;
            repaint();
            break;
        }
        return true;
    }

    public boolean keyDown(Event event, int i)
    {
        if(state == 1)
            switch(i)
            {
            case 49: // '1'
            case 50: // '2'
            case 51: // '3'
            case 52: // '4'
            case 54: // '6'
            case 55: // '7'
            case 56: // '8'
            case 57: // '9'
            case 1000: 
            case 1001: 
            case 1002: 
            case 1003: 
            case 1004: 
            case 1005: 
            case 1006: 
            case 1007: 
                if(game != null)
                    game.keyDown(event, i);
                break;

            default:
                return super.keyDown(event, i);
            }
        else
        if(state == 0)
            switch(i)
            {
            case 1004: 
            case 1007: 
                levelUI.increaseLevel();
                break;

            case 1005: 
            case 1006: 
                levelUI.decreaseLevel();
                break;

            default:
                startGame();
                return super.keyDown(event, i);
            }
        else
        if(state == 3)
        {
            remove(playfield);
            add(levelUI);
            levelUI.reshape(0, 0, 188, 122);
            validate();
            state = 0;
            repaint();
            levelUI.repaint();
            levelUI.requestFocus();
            return super.keyDown(event, i);
        }
        return super.keyDown(event, i);
    }

    private void startGame()
    {
        requestFocus();
        if(gameThread != null)
            gameThread.stop();
        remove(levelUI);
        add(playfield);
        playfield.reshape(0, 0, 188, 122);
        validate();
        repaint();
        playfield.initialize();
        level = 1;
        if(levelUI != null)
            level = levelUI.getLevel();
        try
        {
            Thread.sleep(level * 160);
        }
        catch(InterruptedException _ex) { }
        game = new SGame(3, 7, playfield, level, this);
        gameThread = new Thread(game);
        sterminator = new STerminator(this);
        state = 1;
        game.startGame();
        gameThread.start();
        sterminator.start();
    }

    public void terminateGame()
    {
        score = game.getScore();
        if(gameThread != null)
            gameThread.stop();
        gameThread = null;
        playfield.displayGameOver(score);
        repaint();
        speedOk = game.isGameValid();
        java.net.URL url = snakeConfig.getServerUrl();
        if(url != null)
        {
            String s = "Snake2#" + Integer.toString(level) + "#" + Integer.toString(score) + "#";
            HighScore.submitScore(this, url, s);
        }
        game = null;
        try
        {
            Thread.sleep(500L);
        }
        catch(InterruptedException _ex) { }
        state = 3;
    }

    public int getScore()
    {
        return score;
    }

    public Image imageGrabToRGB(Image image, int i, int j, int k, int l)
    {
        int ai[] = new int[k * l];
        PixelGrabber pixelgrabber = new PixelGrabber(image, i, j, k, l, ai, 0, k);
        try
        {
            pixelgrabber.grabPixels();
        }
        catch(InterruptedException interruptedexception)
        {
            interruptedexception.printStackTrace();
        }
        for(int j1 = 0; j1 < l; j1++)
        {
            for(int k1 = 0; k1 < k; k1++)
            {
                int i1 = ai[j1 * k + k1];
                if((i1 & 0xffffff) == 0xffffff)
                    ai[j1 * k + k1] = snakeConfig.getBackgroundColor().getRGB();
            }

        }

        MemoryImageSource memoryimagesource = new MemoryImageSource(k, l, ai, 0, k);
        Image image1 = createImage(memoryimagesource);
        image.flush();
        image = null;
        try
        {
            Thread.currentThread();
            Thread.sleep(1L);
        }
        catch(InterruptedException _ex) { }
        return image1;
    }

    private boolean[][] imageToBooleanMap(Image image)
    {
        int i = image.getWidth(this);
        int j = image.getHeight(this);
        int ai[] = new int[i * j];
        boolean aflag[][] = new boolean[i][j];
        PixelGrabber pixelgrabber = new PixelGrabber(image, 0, 0, i, j, ai, 0, i);
        try
        {
            pixelgrabber.grabPixels();
        }
        catch(InterruptedException _ex) { }
        for(int l = 0; l < j; l++)
        {
            for(int i1 = 0; i1 < i; i1++)
            {
                int k = ai[l * i + i1];
                if((k & 0xffffff) != 0xffffff)
                    aflag[i1][l] = true;
                else
                    aflag[i1][l] = false;
            }

        }

        return aflag;
    }

    public Snake7110()
    {
        speedOk = true;
    }

    static final int SNAKE_INITLENGTH = 7;
    static final String GAME_ID = "Snake2";
    static final int SET_LEVEL = 0;
    static final int PLAY_GAME = 1;
    static final int GAME_OVER = 2;
    static final int GAME_OVER2 = 3;
    static final int PLAYFIELD_X = 0;
    static final int PLAYFIELD_Y = 0;
    static final int PLAYFIELD_W = 188;
    static final int PLAYFIELD_H = 122;
    GameLevel levelUI;
    SPlayfield playfield;
    SGame game;
    Thread gameThread;
    STerminator sterminator;
    Image bkImg;
    GameAnimator ga;
    Thread animationThread;
    public static boolean playarea_map[][];
    static int gridWidth = 23;
    static int gridHeight = 13;
    public AudioClip eatPillClip;
    public AudioClip deathClip;
    int state;
    private int score;
    public boolean speedOk;
    public static Image playfieldImage;
    Image pieces;
    public static Image mouth_down;
    public static Image mouth_left;
    public static Image mouth_right;
    public static Image mouth_up;
    public static Image head_down;
    public static Image head_left;
    public static Image head_right;
    public static Image head_up;
    public static Image body_down;
    public static Image body_left;
    public static Image body_right;
    public static Image body_up;
    public static Image tail_down;
    public static Image tail_left;
    public static Image tail_right;
    public static Image tail_up;
    public static Image down_left_corner;
    public static Image down_right_corner;
    public static Image up_left_corner;
    public static Image up_right_corner;
    public static Image food;
    public static Image food_eaten_down;
    public static Image food_eaten_up;
    public static Image food_eaten_left;
    public static Image food_eaten_right;
    public static Image food_eaten_down_left_corner;
    public static Image food_eaten_down_right_corner;
    public static Image food_eaten_up_left_corner;
    public static Image food_eaten_up_right_corner;
    public static Image bonus_fluffy;
    public static Image bonus_lizard;
    public static Image bonus_snail;
    public static Image bonus_spider;
    public static Image bonus_turtle;
    public static Image bonus_worm;
    public static Image zero;
    public static Image one;
    public static Image two;
    public static Image three;
    public static Image four;
    public static Image five;
    public static Image six;
    public static Image seven;
    public static Image eight;
    public static Image nine;
    Image startAnimImage;
    public static Image startAnim1;
    public static Image startAnim2;
    public static Image startAnim3;
    public static Image startAnim4;
    public static Image startAnim5;
    public static Image startAnim6;
    Image startAnimImages[];
    Image endAnimImage;
    public static Image endAnim1;
    public static Image endAnim2;
    public static Image endAnim3;
    public static Image endAnim4;
    static Image endAnimImages[];
    private SnakeConfig snakeConfig;
    private int level;

}