// Decompiled by DJ v3.0.0.63 Copyright 2002 Atanas Neshkov  Date: 11/09/2002 17:28:49
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SGame.java

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;

public class SGame
    implements Runnable
{

    SGame(int i, int j, SPlayfield splayfield, int k, Applet applet)
    {
        gameValid = true;
        sleepTime = LEVEL_DELAYS[k - 1];
        playfield = splayfield;
        gridDim = splayfield.getGridDimension();
        for(int l = 0; l < gridDim.width; l++)
        {
            for(int i1 = 0; i1 < gridDim.height; i1++)
                if(!Snake7110.playarea_map[l][i1])
                    gridSquares++;

        }

        Snake7110 = (Snake7110)applet;
        playerSnake = new Snake(gridDim.width / 2, gridDim.height / 2, j, 4, splayfield);
        level = k;
        score = 0;
        pills = 0;
        overruns = 0;
        gameValid = true;
    }

    public void startGame()
    {
        playerSnake.draw();
        pill = randomPill(false);
        pill.draw();
        playfield.requestFocus();
    }

    public boolean keyDown(Event event, int i)
    {
        switch(i)
        {
        default:
            break;

        case 56: // '8'
        case 1004: 
            playerSnake.setDirection(1);
            break;

        case 50: // '2'
        case 1005: 
            playerSnake.setDirection(2);
            break;

        case 52: // '4'
        case 1006: 
            playerSnake.setDirection(3);
            break;

        case 54: // '6'
        case 1007: 
            playerSnake.setDirection(4);
            break;

        case 49: // '1'
        case 1001: 
            int j = playerSnake.getDirection();
            if(j == 1 || j == 2)
                playerSnake.setDirection(3);
            else
                playerSnake.setDirection(2);
            break;

        case 51: // '3'
        case 1003: 
            int k = playerSnake.getDirection();
            if(k == 1 || k == 2)
                playerSnake.setDirection(4);
            else
                playerSnake.setDirection(2);
            break;

        case 55: // '7'
        case 1000: 
            int l = playerSnake.getDirection();
            if(l == 1 || l == 2)
                playerSnake.setDirection(3);
            else
                playerSnake.setDirection(1);
            break;

        case 57: // '9'
        case 1002: 
            int i1 = playerSnake.getDirection();
            if(i1 == 1 || i1 == 2)
                playerSnake.setDirection(4);
            else
                playerSnake.setDirection(1);
            break;
        }
        return false;
    }

    public int getScore()
    {
        return score;
    }

    public boolean isGameValid()
    {
        return gameValid;
    }

    public void run()
    {
        long l1 = System.currentTimeMillis();
        int j = 1;
        int k = 0;
        do
        {
            l1 += sleepTime;
            long l = l1 - System.currentTimeMillis();
            if(l <= 0L)
            {
                overruns++;
                if(overruns > 50)
                    gameValid = false;
            } else
            {
                try
                {
                    Thread.sleep(l);
                }
                catch(InterruptedException _ex) { }
            }
            if(playerSnake.inDanger())
                try
                {
                    l1 += 40L;
                    Thread.sleep(40L);
                }
                catch(InterruptedException _ex) { }
            int i = playerSnake.moveSnake();
            if(bonus != null)
                if(k <= 1)
                {
                    bonus.erase();
                    bonus = null;
                    playfield.removeBonusCounter();
                } else
                {
                    k--;
                    playfield.showBonusCounter(k, bonus);
                }
            switch(i)
            {
            case 1: // '\001'
            case 4: // '\004'
                Snake7110.deathClip.play();
                synchronized(Snake7110)
                {
                    Snake7110.state = 2;
                    Snake7110.notify();
                }
                return;

            case 2: // '\002'
                if(pill != null)
                {
                    pills++;
                    score += level;
                    playfield.showScore(score);
                    if(pills >= 299)
                    {
                        score += 100;
                        playfield.showScore(score);
                        synchronized(Snake7110)
                        {
                            Snake7110.state = 2;
                            Snake7110.notify();
                        }
                        return;
                    }
                    Snake7110.eatPillClip.play();
                    playerSnake.addLength(1);
                    pill = null;
                }
                break;

            case 3: // '\003'
            case 100: // 'd'
            case 101: // 'e'
            case 102: // 'f'
            case 103: // 'g'
            case 104: // 'h'
            case 105: // 'i'
                if(bonus != null)
                {
                    score = score + (level + 1) * 5 + 2 * k;
                    playfield.showScore(score);
                    playfield.removeBonusCounter();
                    Snake7110.eatPillClip.play();
                    playerSnake.addLength(1);
                    bonus = null;
                }
                break;
            }
            if(pill == null)
            {
                pill = randomPill(false);
                if(pill == null)
                {
                    synchronized(Snake7110)
                    {
                        Snake7110.state = 2;
                        Snake7110.notify();
                    }
                    return;
                }
                pill.draw();
                if(++j >= 5)
                {
                    bonus = randomPill(true);
                    j = 0;
                    if(bonus == null)
                    {
                        synchronized(Snake7110)
                        {
                            Snake7110.state = 2;
                            Snake7110.notify();
                        }
                        return;
                    }
                    k = 20;
                    playfield.showBonusCounter(k, bonus);
                    bonus.draw();
                }
            }
            playfield.repaint();
        } while(true);
    }

    private boolean freePosition(Dimension dimension, boolean flag)
    {
        if(dimension.width >= 0 && dimension.width < gridDim.width && dimension.height >= 0 && dimension.height < gridDim.height)
        {
            if(flag)
                if(dimension.width < gridDim.width - 1)
                    return playfield.getSquareContent(dimension.width, dimension.height) == 0 && playfield.getSquareContent(dimension.width + 1, dimension.height) == 0 && !Snake7110.playarea_map[dimension.width][dimension.height] && !Snake7110.playarea_map[dimension.width + 1][dimension.height];
                else
                    return false;
            return playfield.getSquareContent(dimension.width, dimension.height) == 0 && !Snake7110.playarea_map[dimension.width][dimension.height];
        } else
        {
            return false;
        }
    }

    private SPill randomPill(boolean flag)
    {
        Dimension dimension = new Dimension((int)(Math.random() * (double)(gridDim.width - 1)), (int)(Math.random() * (double)(gridDim.height - 1)));
        Dimension dimension1 = new Dimension(dimension.width, dimension.height);
        int i = 0;
        int j = 0;
        int k = 0;
        boolean flag1;
        for(flag1 = freePosition(dimension, flag); !flag1 && k < 4;)
        {
            k = 0;
            i++;
            dimension.width = dimension1.width + i;
            j++;
            dimension.height = dimension1.height + j;
            if(dimension.width >= gridDim.width && dimension.height >= gridDim.height)
                k++;
            for(; !flag1 && j > -i; flag1 = freePosition(dimension, flag))
            {
                j--;
                dimension.height = dimension1.height + j;
            }

            if(dimension.width >= gridDim.width && dimension.height < 0)
                k++;
            for(; !flag1 && i > j; flag1 = freePosition(dimension, flag))
            {
                i--;
                dimension.width = dimension1.width + i;
            }

            if(dimension.width < 0 && dimension.height < 0)
                k++;
            for(; !flag1 && -j > i; flag1 = freePosition(dimension, flag))
            {
                j++;
                dimension.height = dimension1.height + j;
            }

            if(dimension.width < 0 && dimension.height >= gridDim.height)
                k++;
            for(; !flag1 && j > i; flag1 = freePosition(dimension, flag))
            {
                i++;
                dimension.width = dimension1.width + i;
            }

        }

        if(flag1)
            return new SPill(dimension, playfield, flag);
        else
            return null;
    }

    static final int LEVEL_DELAYS[] = {
        490, 361, 282, 233, 172, 136, 108, 88, 75
    };
    static final int MAX_PILLS = 299;
    static final int LEVEL_BONUS = 100;
    static final int EXTRA_TIME = 40;
    static final int BONUS_FREQUENCY = 5;
    static final int BONUS_COUNTDOWN_TIME = 20;
    int level;
    int score;
    int pills;
    private int overruns;
    private boolean gameValid;
    Snake7110 Snake7110;
    SPlayfield playfield;
    Dimension gridDim;
    Snake playerSnake;
    SPill pill;
    SPill bonus;
    int sleepTime;
    int gridSquares;

}