computerworm.java

来自「thq 发布的《worms》 motorola 源代码」· Java 代码 · 共 243 行

JAVA
243
字号
import java.util.Random;

public class ComputerWorm extends Worm
{

    private final int COMPUTER_STATE_STAND_ATTACK = 0;
    private final int COMPUTER_STATE_WALK_ATTACK = 1;
    private final int COMPUTER_STATE_FLEE_ATTACK = 3;
    private final int COMPUTER_STATE_SELECT_ANGLE = 4;
    private final int COMPUTER_STATE_AIM = 5;
    private final int COMPUTER_STATE_FIRE = 6;
    private int computerState;
    private int computerTargetAngle;
    private int computerTargetVelocity;
    private long computerPauseTime;
    private int nearestEnemyDirection;
    private int nearestEnemy;
    private int walkLength;
    private int prevWalkDir;

    public ComputerWorm(boolean flag, int i, boolean flag1)
    {
        super(flag, i, flag1);
        computerState = -1;
        pointerVisible = false;
    }

    private void calcComputerAngle()
    {
        calcNearestEnemy();
        if(nearestEnemyDirection == 0)
            computerTargetAngle = (120 + Math.abs(random.nextInt()) % 40) - 20;
        else
            computerTargetAngle = (60 + Math.abs(random.nextInt()) % 40) - 20;
    }

    protected void endTurn()
    {
        super.endTurn();
        computerState = -1;
        setIdle();
    }

    private void calcComputerVelocity()
    {
        char c = '\0';
        c = '\200';
        int i = 0x7fffffff;
        for(int j = 22; j < 100; j++)
        {
            Game _tmp = game;
            int k = WeaponIterator.iterate(positionX, positionY, Game.windFactor, j * c >> 8, computerTargetAngle, game.teams[myTeamNo ^ 0x1]);
            if(k < i)
            {
                i = k;
                computerTargetVelocity = j;
            }
        }

    }

    public void calcNearestEnemy()
    {
        nearestEnemy = 0x30d40;
        for(int i = 0; i < game.teams[myTeamNo ^ 0x1].wormList.length; i++)
        {
            int j = game.teams[myTeamNo ^ 0x1].wormList[i].positionX - positionX;
            if(Math.abs(j) < nearestEnemy)
            {
                if(j < 0)
                    nearestEnemyDirection = 0;
                else
                    nearestEnemyDirection = 1;
                nearestEnemy = Math.abs(j);
            }
        }

    }

    private boolean friendlyWormTooClose()
    {
        if(!game.teams[myTeamNo].wormList[0].isDead() && !game.teams[myTeamNo].wormList[1].isDead())
        {
            int i = game.teams[myTeamNo].wormList[0].positionX - game.teams[myTeamNo].wormList[1].positionX;
            if(Math.abs(i) < 25)
                return true;
        }
        return false;
    }

    public void reset(Game game, Landscape landscape, int i, int ai[])
    {
    	System.out.println("====computerWorm====94==i=="+i);
        super.reset(game, landscape, i);
        computerState = -1;
    }

    public void activate()
    {
        super.activate();
        adjustCrosshair = false;
        computerPauseTime = Worms.getTime();
        int i = Math.abs(random.nextInt()) % 3;
        calcNearestEnemy();
        walkLength = Math.abs(random.nextInt()) % 30 + 10;
        if(health > 30)
        {
            if(Math.abs(random.nextInt()) % 100 < 85)
            {
                computerState = 0;
                calcComputerAngle();
            } else
            {
                computerState = 1;
            }
        } else
        {
            computerState = 3;
        }
        if(Math.abs(random.nextInt()) % 8 == 0)
            computerState = 3;
        else
        if(Math.abs(random.nextInt()) % 8 == 0)
            computerState = 1;
        if(friendlyWormTooClose())
            if(Math.abs(random.nextInt()) % 4 == 0)
                computerState = 1;
            else
                computerState = 3;
    }

    private void aim()
    {
        computerState = 5;
        calcComputerAngle();
    }

    public boolean runFrame()
    {
        if(super.runFrame())
        {
            if(falling)
                return true;
            if(Worms.getTime() - computerPauseTime > 2000L)
            {
                turnKeyPressed = true;
                pointerActive = false;
                if(computerState == 0)
                    aim();
                else
                if(computerState == 1)
                {
                    walkLength--;
                    byte byte0;
                    if(state == 3)
                        byte0 = 15;
                    else
                        byte0 = -15;
                    if(walkLength <= 0 || wormObstructed || landscape.getLandHeight(positionX + byte0) < 5)
                        if(!friendlyWormTooClose())
                            aim();
                        else
                            computerState = 3;
                    if(nearestEnemyDirection == 0)
                        state = 2;
                    else
                        state = 3;
                    prevWalkDir = state;
                } else
                if(computerState == 3)
                {
                    walkLength--;
                    byte byte1;
                    if(state == 3)
                        byte1 = 15;
                    else
                        byte1 = -15;
                    if(walkLength <= 0 || wormObstructed || landscape.getLandHeight(positionX + byte1) < 5)
                    {
                        walkLength = 0;
                        aim();
                    }
                    if(nearestEnemyDirection == 0)
                        state = 3;
                    else
                        state = 2;
                    prevWalkDir = state;
                } else
                if(computerState == 5)
                {
                    if(Math.abs(angle - computerTargetAngle) < 4)
                    {
                        adjustCrosshair = false;
                        computerState = 6;
                    } else
                    if(angle < computerTargetAngle)
                    {
                        adjustCrosshair = true;
                        state = 4;
                    } else
                    {
                        adjustCrosshair = true;
                        state = 5;
                    }
                } else
                if(computerState == 6)
                {
                    if(fireVelocityTime == -1L)
                    {
                        calcComputerVelocity();
                        fireVelocityTime = Worms.getTime();
                        state = 6;
                    }
                    if(state == 6 && calcVelocity() >= computerTargetVelocity)
                    {
                        fire();
                        if(activeWeapon != 4)
                        {
                            if(direction == 0)
                                state = 0;
                            else
                                state = 1;
                        } else
                        {
                            walkLength = 60;
                            computerState = 3;
                        }
                    }
                }
            }
        }
        return true;
    }

    public void keyReleased(int i)
    {
    }

    public void keyPressed(int i)
    {
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?