Exemple #1
0
    public bool moveToEvent(int x, int y, bool start, bool follow, int speed, OnEventOver over)
    {
        if (GameUserData.instance.Stage > 15 &&
            UnitID == 8 &&
            GameUserData.instance.getUnitBase(8).InTeam == 0)
        {
            return(false);
        }

        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(MoveType);

        return(gameMovement.moveToEvent(x, y, unitMove, start, follow, speed, over));
    }
Exemple #2
0
    public bool moveToEvent(int x, int y, GameUnitMove unitMove, bool start, bool follow, int speed, OnEventOver over)
    {
        if (x == posX && y == posY)
        {
            return(false);
        }

        if (startMoving)
        {
            return(false);
        }

        int count = GameBattlePathFinder.instance.findPath(posX, posY, x, y,
                                                           unitMove.block, unitMove.fly, GameUnitCampType.Count);

        if (count == 0)
        {
            // try fly

            count = GameBattlePathFinder.instance.findPath(posX, posY, x, y,
                                                           GameBattlePathFinder.BLOCK_EVENT, true, GameUnitCampType.Count);
        }

        if (count == 0)
        {
            return(false);
        }

        for (int i = count - 3; i >= 0; i--)
        {
            moveBuffer.Add(GameBattlePathFinder.instance.pathResult[i]);
        }

        moveSpeed = GameDefine.getMoveSpeed(speed);

        sceneFollow        = follow;
        playStartAnimation = start;
        onEventOver        = over;
        canFollow          = GameCameraManager.instance.canFollow(posBattleX, posBattleY,
                                                                  GameBattleManager.instance.LayerHeight);

        moveNext();

        startAnimation();

        startMoving = true;

        return(true);
    }
Exemple #3
0
    public bool moveTo(int x, int y, bool start, bool follow, int speed, OnEventOver over)
    {
        lastMove = unitData.Move;

        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(MoveType);
        bool         fly      = checkEffect(GameSkillResutlEffect.Under) ? true : unitMove.fly;

        int cost = gameMovement.moveTo(x, y, unitMove.block, fly, UnitCampType, start, follow, speed, over);

        if (cost >= 0)
        {
            unitData.Move -= cost;
        }

        IsMoved = true;

        return(cost != GameDefine.INVALID_ID);
    }
    public void showUnits(int e)
    {
        GameBattleStage stage = GameBattleManager.instance.ActiveBattleStage;

        for (int i = 0; i < stage.Man.Length; i++)
        {
            GameBattleMan man = stage.Man[i];

            if (man.EventParm1 == e)
            {
                GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(i);

                GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(unit.MoveType);

                GameBattleUnit unit1 = GameBattleUnitManager.instance.getUnit(unit.PosX, unit.PosY);

                if (unit != unit1 &&
                    unit1 != null)
                {
                    GameBattlePathFinder.instance.findNearPos(unit.PosX, unit.PosY, unitMove.block);

                    unit.setPos(GameBattlePathFinder.instance.nearPosX,
                                GameBattlePathFinder.instance.nearPosY);
                }

//                 if ( GameBattlePathFinder.instance.isBlockPos( unit.PosX , unit.PosY , unitMove.block , unit ) )
//                 {
//                     GameBattlePathFinder.instance.findNearPos( unit.PosX , unit.PosY , unitMove.block );
//
//                     unit.setPos( GameBattlePathFinder.instance.nearPosX ,
//                         GameBattlePathFinder.instance.nearPosY );
//                 }

                unit.IsShow = true;

                unit.updateAlive();
                unit.playAnimationBattle(GameAnimationType.Stand,
                                         GameAnimationDirection.South, null);
                unit.updateInteam();
            }
        }
    }
Exemple #5
0
    public bool selectUnit()
    {
        selectionUnit = GameBattleUnitManager.instance.getUnit(GameBattleCursor.instance.PosX,
                                                               GameBattleCursor.instance.PosY);

        if (selectionUnit == null)
        {
            return(false);
        }

        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(selectionUnit.MoveType);
        bool         fly      = selectionUnit.checkEffect(GameSkillResutlEffect.Under) ? true : unitMove.fly;

        if (selectionUnit.IsUser ||
            selectionUnit.IsNpc)
        {
            // user

            GameBattleUnitMovement.instance.show((short)selectionUnit.PosX, (short)selectionUnit.PosY,
                                                 selectionUnit.IsNpc || selectionUnit.IsActed ? selectionUnit.MoveMax : selectionUnit.Move, selectionUnit.MoveType, fly, selectionUnit.UnitCampType, true);

            if (selectionUnit.IsActed)
            {
                // show info ui

//                selectionUnit = null;
            }
        }
        else
        {
            GameBattleUnitMovement.instance.show((short)selectionUnit.PosX, (short)selectionUnit.PosY,
                                                 selectionUnit.MoveMax, selectionUnit.MoveType, fly, selectionUnit.UnitCampType, false);
        }

        return(true);
    }
    int getCost(int x, int y, GameUnitMove unitMove, GameUnitCampType camp, out bool bd)
    {
        //         byte baseCost = 0;
        //         byte block = 0;
        //         bool addMove = false;
        //         sbyte subMove = 0;
        //
        //         switch ( moveType )
        //         {
        //             case GameUnitMoveType.Walk0:
        //                 baseCost = 5;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Walk1:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Walk2:
        //                 baseCost = 5;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Walk3:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = 1;
        //                 break;
        //             case GameUnitMoveType.Fly4:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = false;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Fly5:
        //                 baseCost = 7;
        //                 block = 9;
        //                 addMove = false;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Fly6:
        //                 baseCost = 5;
        //                 block = 9;
        //                 addMove = false;
        //                 subMove = 0;
        //                 break;
        //             case GameUnitMoveType.Walk7:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = -1;
        //                 break;
        //             case GameUnitMoveType.Fly8:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = -2;
        //                 break;
        //             case GameUnitMoveType.Fly9:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = -2;
        //                 break;
        //             case GameUnitMoveType.Walk10:
        //                 baseCost = 6;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = -2;
        //                 break;
        //             case GameUnitMoveType.None:
        //                 baseCost = 100;
        //                 block = 7;
        //                 addMove = true;
        //                 subMove = 0;
        //                 break;
        //         }

        bd = false;

        int cost = unitMove.baseCost;

        GameBattleDTL dtl = GameBattleManager.instance.ActiveDTL;

        int index1 = dtl.Width * y + x;

        GameBattleDTL.Point point = dtl.Points[index1];

        int subMove = point.Move + unitMove.subMove;

        if (subMove < 0)
        {
            subMove = 0;
        }

        if (unitMove.addMove)
        {
            cost = unitMove.baseCost + subMove;
        }

        // right
        int index = dtl.Width * y + x + 1;

        if (index >= 0 && index < dtl.Points.Length)
        {
            GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(x + 1, y);

            if (unit != null)
            {
                if (unitMove.addMove &&
                    unit.IsAlive &&
                    unit.UnitCampType != camp)
                {
                    bd = true;
                }
            }
        }

        // left
        index = dtl.Width * y + x - 1;
        if (index >= 0 && index < dtl.Points.Length)
        {
            GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(x - 1, y);

            if (unit != null)
            {
                if (unitMove.addMove &&
                    unit.IsAlive &&
                    unit.UnitCampType != camp)
                {
                    bd = true;
                }
            }
        }

        // top
        index = dtl.Width * (y - 1) + x;
        if (index >= 0 && index < dtl.Points.Length)
        {
            GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(x, y - 1);

            if (unit != null)
            {
                if (unitMove.addMove &&
                    unit.IsAlive &&
                    unit.UnitCampType != camp)
                {
                    bd = true;
                }
            }
        }

        // bottom
        index = dtl.Width * (y + 1) + x;
        if (index >= 0 && index < dtl.Points.Length)
        {
            GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(x, y + 1);

            if (unit != null)
            {
                if (unitMove.addMove &&
                    unit.IsAlive &&
                    unit.UnitCampType != camp)
                {
                    bd = true;
                }
            }
        }

        return(cost);
    }
    void findCell(int move, GameUnitMoveType moveType, bool fly, GameUnitCampType camp)
    {
        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(moveType);

        bool user = (camp == GameUnitCampType.User);

        GameBattleDTL dtl = GameBattleManager.instance.ActiveDTL;

        closeList.Add(openList[0]);

        while (openList.Count > 0)
        {
            List <Cell> newOpenList = new List <Cell>();

            for (int i = 0; i < openList.Count; i++)
            {
                Cell cell = openList[i];
                GameBattleDTL.Point point = null;

                // right
                int index = dtl.Width * cell.y + cell.x + 1;
                if (index >= 0 && index < dtl.Points.Length &&
                    !isClose(cell.x + 1, cell.y, camp, fly))
                {
                    point = dtl.Points[index];

                    bool bd   = false;
                    int  cost = getCost(cell.x + 1, cell.y, unitMove, camp, out bd);

                    if (point.Move < unitMove.block &&
                        cell.f + cost <= move)
                    {
                        Cell newCell = new Cell();
                        newCell.x  = cell.x + 1;
                        newCell.y  = cell.y;
                        newCell.f  = cell.f + (bd ? cost * 2 : cost);
                        newCell.c  = cost;
                        newCell.ca = cell.f + cost;

                        newOpenList.Add(newCell);
                        closeList.Add(newCell);
                    }
                }

                // left
                index = dtl.Width * cell.y + cell.x - 1;
                if (index >= 0 && index < dtl.Points.Length &&
                    !isClose(cell.x - 1, cell.y, camp, fly))
                {
                    point = dtl.Points[index];

                    bool bd   = false;
                    int  cost = getCost(cell.x - 1, cell.y, unitMove, camp, out bd);

                    if (point.Move < unitMove.block &&
                        cell.f + cost <= move)
                    {
                        Cell newCell = new Cell();
                        newCell.x  = cell.x - 1;
                        newCell.y  = cell.y;
                        newCell.f  = cell.f + (bd ? cost * 2 : cost);
                        newCell.c  = cost;
                        newCell.ca = cell.f + cost;

                        newOpenList.Add(newCell);
                        closeList.Add(newCell);
                    }
                }

                // top
                index = dtl.Width * (cell.y - 1) + cell.x;
                if (index >= 0 && index < dtl.Points.Length &&
                    !isClose(cell.x, cell.y - 1, camp, fly))
                {
                    point = dtl.Points[index];

                    bool bd   = false;
                    int  cost = getCost(cell.x, cell.y - 1, unitMove, camp, out bd);

                    if (point.Move < unitMove.block &&
                        cell.f + cost <= move)
                    {
                        Cell newCell = new Cell();
                        newCell.x  = cell.x;
                        newCell.y  = cell.y - 1;
                        newCell.f  = cell.f + (bd ? cost * 2 : cost);
                        newCell.c  = cost;
                        newCell.ca = cell.f + cost;

                        newOpenList.Add(newCell);
                        closeList.Add(newCell);
                    }
                }

                // bottom
                index = dtl.Width * (cell.y + 1) + cell.x;
                if (index >= 0 && index < dtl.Points.Length &&
                    !isClose(cell.x, cell.y + 1, camp, fly))
                {
                    point = dtl.Points[index];

                    bool bd   = false;
                    int  cost = getCost(cell.x, cell.y + 1, unitMove, camp, out bd);

                    if (point.Move < unitMove.block &&
                        cell.f + cost <= move)
                    {
                        Cell newCell = new Cell();
                        newCell.x  = cell.x;
                        newCell.y  = cell.y + 1;
                        newCell.f  = cell.f + (bd ? cost * 2 : cost);
                        newCell.c  = cost;
                        newCell.ca = cell.f + cost;

                        newOpenList.Add(newCell);
                        closeList.Add(newCell);
                    }
                }
            }

            openList.Clear();
            openList = null;
            openList = newOpenList;
        }

        for (int i = 1; i < closeList.Count;)
        {
            if (isUnit(closeList[i].x, closeList[i].y))
            {
                closeList.RemoveAt(i);
            }
            else
            {
                i++;
            }
        }

        for (int i = 0; i < closeList.Count; i++)
        {
            addCell(closeList[i], user);
        }
    }
Exemple #8
0
    bool AIUseItem(List <GameBattleUnitMovement.Cell> cells)
    {
        gameItemAI = null;

        int itemIndex = GameDefine.INVALID_ID;

        for (int i = 0; i < Items.Length; i++)
        {
            if (Items[i] != GameDefine.INVALID_ID)
            {
                GameItem item = GameItemData.instance.getData(Items[i]);

                if (item.UseType == GameItemUseType.Cure)
                {
                    itemIndex  = i;
                    gameItemAI = item;
                    break;
                }
            }
        }

        if (itemIndex == GameDefine.INVALID_ID)
        {
            return(false);
        }

        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(MoveType);
        bool         fly      = checkEffect(GameSkillResutlEffect.Under) ? true : unitMove.fly;

        GameBattleUnit moveToUnit1 = null;

        GameBattleUnitMovement.Cell cellMove1 = null;

        for (int j = 0; j < cells.Count; j++)
        {
            int size = 1;
            for (int i0 = -size; i0 <= size; i0++)
            {
                for (int j0 = -size; j0 <= size; j0++)
                {
                    if (Mathf.Abs(i0) + Mathf.Abs(j0) > size)
                    {
                        continue;
                    }

                    GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(cells[j].x + i0, cells[j].y + j0);

                    if (unit != null && unit != this && unit.UnitCampType == UnitCampType && (unit.HP / (float)unit.HPMax < 0.4f))
                    {
                        if (moveToUnit1 == null ||
                            Random.Range(0, 100) > 50)
                        {
                            moveToUnit1 = unit;
                            cellMove1   = cells[j];
                        }
                    }
                }
            }
        }

        if (moveToUnit1 != null)
        {
            GameBattleUnitItemSelection.instance.setAttackAI(moveToUnit1, moveToUnit1.PosX, moveToUnit1.PosY);

            gameBattleUnitAIType = GameBattleUnitAIType.Item;

            if (cellMove1.x != PosX || cellMove1.y != PosY)
            {
                bool b = moveTo(cellMove1.x, cellMove1.y, true, false, 3, AIOnMoveOver);

                if (!b)
                {
                    return(false);
                }
                else
                {
                    GameBattleCursor.instance.show();
                    GameBattleCursor.instance.moveTo(cellMove1.x, cellMove1.y,
                                                     GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                     true,
                                                     null);
                }
            }
            else
            {
                AIOnMoveOver();
            }

            return(true);
        }



        if (HP / (float)HPMax < 0.4f)
        {
            List <GameBattleUnit> User  = GameBattleUnitManager.instance.User;
            List <GameBattleUnit> Enemy = GameBattleUnitManager.instance.Enemy;
            List <GameBattleUnit> Npc   = GameBattleUnitManager.instance.Npc;

            GameBattleUnit moveToUnit            = null;
            GameBattleUnitMovement.Cell cellMove = null;
            int disMove = 999;
            int cost    = 999;

            moveCostAI = 0;

            if (UnitCampType == GameUnitCampType.User)
            {
                for (int j = 0; j < Enemy.Count; j++)
                {
                    GameBattleUnit enemy = Enemy[j];

                    if (!enemy.IsAlive)
                    {
                        continue;
                    }

                    enemy.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, enemy.PosX, enemy.PosY,
                                                                     unitMove.block, fly, UnitCampType);

                    enemy.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = enemy;
                    }
                }
            }
            else
            {
                for (int j = 0; j < Npc.Count; j++)
                {
                    GameBattleUnit npc = Npc[j];

                    if (!npc.IsAlive)
                    {
                        continue;
                    }

                    npc.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, npc.PosX, npc.PosY,
                                                                     unitMove.block, fly, UnitCampType);

                    npc.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = npc;
                    }
                }

                for (int j = 0; j < User.Count; j++)
                {
                    GameBattleUnit user = User[j];

                    if (!user.IsAlive)
                    {
                        continue;
                    }

                    user.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, user.PosX, user.PosY
                                                                     , unitMove.block, fly, UnitCampType);

                    user.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = user;
                    }
                }
            }

            if (moveToUnit != null)
            {
                int disMove1 = 0;

                for (int i = 0; i < cells.Count; i++)
                {
                    GameBattleUnitMovement.Cell cell = cells[i];

                    int dis = GameBattlePathFinder.instance.findPath(moveToUnit.PosX, moveToUnit.PosY, cell.x, cell.y
                                                                     , unitMove.block, fly, UnitCampType);

                    if ((dis > 0 && dis > disMove1) ||
                        (dis > 0 && dis >= disMove1 && Random.Range(0, 100) > 50))
                    {
                        disMove1 = dis;
                        cellMove = cell;
                    }
                }
            }

            gameBattleUnitAIType = GameBattleUnitAIType.Item;

            if (cellMove != null &&
                (cellMove.x != PosX || cellMove.y != PosY))
            {
                GameBattleUnitItemSelection.instance.setAttackAI(this, cellMove.x, cellMove.y);

                bool b = moveTo(cellMove.x, cellMove.y, true, false, 3, AIOnMoveOver);

                if (!b)
                {
                    return(false);
                }
                else
                {
                    GameBattleCursor.instance.show();
                    GameBattleCursor.instance.moveTo(cellMove.x, cellMove.y,
                                                     GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                     true,
                                                     null);
                }
            }
            else
            {
                AIOnMoveOver();
            }

            return(true);
        }

        return(false);
    }
Exemple #9
0
    void AIFindNearEnemy()
    {
        if (BattleAIType == GameBattleAIType.AIStay)
        {
            AIOver();
            return;
        }

        GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(MoveType);
        bool         fly      = checkEffect(GameSkillResutlEffect.Under) ? true : unitMove.fly;

        GameBattleUnitMovement.instance.show(PosX, PosY, Move, MoveType, fly, UnitCampType, false);

        List <GameBattleUnitMovement.Cell> cells = GameBattleUnitMovement.instance.Cells;

        listUnitsAI.Clear();
        listCellsAI.Clear();

        gameSkillAI = null;

        GameBattleUnitSkillSelection.instance.AttackUnits.Clear();

        if (AIUseItem(cells))
        {
            return;
        }

        int targetID = AITargetID;

        if (BattleAIType == GameBattleAIType.AIPositive ||
            BattleAIType == GameBattleAIType.AINegative ||
            battleAICheckFight)
        {
            battleAICheckFight = false;

            // skill

            List <int> useSkills = new List <int>();
            List <GameBattleUnitMovement.Cell> useSkillsCells = new List <GameBattleUnitMovement.Cell>();

            for (int i = 0; i < GameDefine.MAX_SLOT; i++)
            {
                if (Skill[i] == GameDefine.INVALID_ID)
                {
                    continue;
                }

                gameSkillAI = GameSkillData.instance.getData(Skill[i]);

                if (canUseSkill(gameSkillAI))
                {
                    GameBattleUnitSkillSelection.instance.canAttackAI(this, PosX, PosY, PosX, PosY, gameSkillAI, UnitCampType, targetID);

                    if (GameBattleUnitSkillSelection.instance.AttackUnits.Count != 0 &&
                        GameBattleUnitSkillSelection.instance.IsAttackUnits)
                    {
                        useSkills.Add(i);

                        if (gameSkillAI.ResultType == GameSkillResutlType.Cure)
                        {
                            break;
                        }
                    }
                }
            }


            if (useSkills.Count > 0)
            {
                int r = Random.Range(0, useSkills.Count);

                gameSkillAI = GameSkillData.instance.getData(Skill[useSkills[r]]);

                GameBattleUnitSkillSelection.instance.AttackUnits.Clear();
                GameBattleUnitSkillSelection.instance.canAttackAI(this, PosX, PosY, PosX, PosY, gameSkillAI, UnitCampType, targetID);
                GameBattleUnitSkillSelection.instance.getUnits(this, gameSkillAI);

                gameBattleUnitAIType = GameBattleUnitAIType.Skill;
                AIOnMoveOver();
                return;
            }


            gameSkillAI = null;
            GameBattleUnitSkillSelection.instance.AttackUnits.Clear();

            for (int i = 0; i < GameDefine.MAX_SLOT; i++)
            {
                if (Skill[i] == GameDefine.INVALID_ID)
                {
                    continue;
                }

                GameSkill skill = GameSkillData.instance.getData(Skill[i]);

                for (int j = 0; j < cells.Count; j++)
                {
                    if (canUseSkillMove(skill, cells[j].ca))
                    {
                        GameBattleUnitSkillSelection.instance.canAttackAI(this, PosX, PosY, cells[j].x, cells[j].y,
                                                                          skill, UnitCampType, AIMoveToID);

                        if (GameBattleUnitSkillSelection.instance.IsAttackUnits)
                        {
                            gameSkillAI = skill;
                        }
                    }
                }
            }


            if (GameBattleUnitSkillSelection.instance.AttackUnits.Count > 0 &&
                Random.Range(1, 100) > 10)
            {
                gameBattleUnitAIType = GameBattleUnitAIType.Skill;

                GameBattleUnitSkillSelection.instance.getUnits(this, gameSkillAI);

                bool b = moveTo(GameBattleUnitSkillSelection.instance.AttackMoveCell.x,
                                GameBattleUnitSkillSelection.instance.AttackMoveCell.y, true, false, 3, AIOnMoveOver);

                if (!b)
                {
                    GameBattleUnitMovement.instance.unShow();
                    AIOver();

#if UNITY_EDITOR
                    Debug.LogError("error ai skill move ");
#endif
                }
                else
                {
                    GameBattleCursor.instance.moveTo(GameBattleUnitSkillSelection.instance.AttackMoveCell.x,
                                                     GameBattleUnitSkillSelection.instance.AttackMoveCell.y,
                                                     GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                     true,
                                                     null);
                }

                return;
            }



            // attack

            for (int i = 0; i < cells.Count; i++)
            {
                GameBattleUnit u = GameBattleUnitAttackSelection.instance.canAttack(cells[i].x, cells[i].y, Weapon, UnitCampType);

                if (u != null)
                {
                    listUnitsAI.Add(u);
                    listCellsAI.Add(cells[i]);
                }
            }

            if (listUnitsAI.Count != 0)
            {
                AIrandomTarget(targetID);

                gameBattleUnitAIType = GameBattleUnitAIType.Attack;

                GameBattleUnit target            = listUnitsAI[targetIndexAI];
                GameBattleUnitMovement.Cell cell = listCellsAI[targetIndexAI];

                if (cell.x != PosX ||
                    cell.y != PosY)
                {
                    bool b = moveTo(cell.x, cell.y, true, false, 3, AIOnMoveOver);

                    if (!b)
                    {
#if UNITY_EDITOR
                        Debug.LogError("error ai move ");
#endif

                        GameBattleUnitMovement.instance.unShow();
                        AIOver();
                    }
                    else
                    {
                        GameBattleCursor.instance.moveTo(cell.x, cell.y,
                                                         GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                         true,
                                                         null);
                    }
                }
                else
                {
                    AIOnMoveOver();
                }

                return;
            }

            if (BattleAIType == GameBattleAIType.AINegative)
            {
                GameBattleUnitMovement.instance.unShow();
                AIOver();
                return;
            }
        }


        // move

        List <GameBattleUnit> User  = GameBattleUnitManager.instance.User;
        List <GameBattleUnit> Enemy = GameBattleUnitManager.instance.Enemy;
        List <GameBattleUnit> Npc   = GameBattleUnitManager.instance.Npc;

        GameBattleUnit moveToUnit            = null;
        GameBattleUnitMovement.Cell cellMove = null;
        int disMove = 999;
        int cost    = 999;

        moveCostAI = 0;

        if (BattleAIType == GameBattleAIType.AIPositive)
        {
            if (UnitCampType == GameUnitCampType.User)
            {
                for (int j = 0; j < Enemy.Count; j++)
                {
                    GameBattleUnit enemy = Enemy[j];

                    if (!enemy.IsAlive)
                    {
                        continue;
                    }

                    enemy.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, enemy.PosX, enemy.PosY,
                                                                     unitMove.block, fly, UnitCampType);

                    enemy.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = enemy;
                    }
                }
            }
            else
            {
                for (int j = 0; j < Npc.Count; j++)
                {
                    GameBattleUnit npc = Npc[j];

                    if (!npc.IsAlive)
                    {
                        continue;
                    }

                    npc.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, npc.PosX, npc.PosY,
                                                                     unitMove.block, fly, UnitCampType);

                    npc.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = npc;
                    }
                }

                for (int j = 0; j < User.Count; j++)
                {
                    GameBattleUnit user = User[j];

                    if (!user.IsAlive)
                    {
                        continue;
                    }

                    user.IsShow = false;

                    int dis = GameBattlePathFinder.instance.findPath(PosX, PosY, user.PosX, user.PosY
                                                                     , unitMove.block, fly, UnitCampType);

                    user.IsShow = true;

                    if ((dis > 0 && dis < disMove) ||
                        (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost       = GameBattlePathFinder.instance.cost;
                        disMove    = dis;
                        moveToUnit = user;
                    }
                }
            }

            if (moveToUnit != null)
            {
                int disMove1 = 999;
                int cost1    = 999;
                int dis1     = 0;

                for (int i = 0; i < cells.Count; i++)
                {
                    GameBattleUnitMovement.Cell cell = cells[i];

                    int dis = GameBattlePathFinder.instance.findPath(moveToUnit.PosX, moveToUnit.PosY, cell.x, cell.y
                                                                     , unitMove.block, fly, UnitCampType);

                    for (int i0 = 0; i0 < GameDefine.MAX_SLOT; i0++)
                    {
                        if (Skill[i0] == GameDefine.INVALID_ID)
                        {
                            continue;
                        }

                        GameSkill skill = GameSkillData.instance.getData(Skill[i0]);

                        if (canUseSkillStand(skill))
                        {
                            GameBattleUnitSkillSelection.instance.canAttackAI(this, cell.x, cell.y, cell.x, cell.y, skill, UnitCampType, targetID);

                            if (GameBattleUnitSkillSelection.instance.AttackUnits.Count != 0 &&
                                GameBattleUnitSkillSelection.instance.IsAttackUnits)
                            {
                                if (dis > dis1)
                                {
                                    dis1 = dis;
                                    dis  = 1;
                                }
                            }
                        }
                    }

                    if ((dis > 0 && dis < disMove1) ||
                        (dis > 0 && dis <= disMove1 && cost1 > GameBattlePathFinder.instance.cost) ||
                        ((dis > 0 && dis <= disMove1 && cost1 == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                    {
                        cost1    = GameBattlePathFinder.instance.cost;
                        disMove1 = dis;
                        cellMove = cell;
                    }
                }
            }

            gameBattleUnitAIType = GameBattleUnitAIType.Move;

            if (cellMove != null &&
                (cellMove.x != PosX || cellMove.y != PosY))
            {
                int dis2 = GameBattlePathFinder.instance.findPath(PosX, PosY, moveToUnit.PosX, moveToUnit.PosY
                                                                  , unitMove.block, true, UnitCampType);

                if (dis2 > 0 &&
                    disMove > dis2)
                {
                    AIOnMoveOver();
                }
                else
                {
                    bool b = moveTo(cellMove.x, cellMove.y, true, false, 3, AIOnMoveOver);

                    if (!b)
                    {
                        AIOver();
                    }
                    else
                    {
                        GameBattleCursor.instance.show();
                        GameBattleCursor.instance.moveTo(cellMove.x, cellMove.y,
                                                         GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                         true,
                                                         null);
                    }
                }
            }
            else
            {
                AIOnMoveOver();
            }
        }
        else if (BattleAIType == GameBattleAIType.AIMoveToUnit ||
                 BattleAIType == GameBattleAIType.AIMoveToPos)
        {
            int mx = AIMoveToX;
            int my = AIMoveToY;

            if (BattleAIType == GameBattleAIType.AIMoveToUnit)
            {
                GameBattleUnit u = GameBattleUnitManager.instance.getUnit(AIMoveToID);

                if (u == null && u.UnitID >= GameDefine.MAX_USER)
                {
                    move();
                    AIOver();
                    battleAICheckFight = true;
//                    BattleAIType = GameBattleAIType.AINegative;
                    return;
                }

                mx = u.PosX;
                my = u.PosY;
            }

            if (GameBattlePathFinder.instance.isBlockPos(mx, my, unitMove.block))
            {
                if (GameBattlePathFinder.instance.findNearPos(mx, my, PosX, PosY, UnitMove.block))
                {
                    mx = GameBattlePathFinder.instance.nearPosX;
                    my = GameBattlePathFinder.instance.nearPosY;
                }
                else
                {
#if UNITY_EDITOR
                    Debug.LogError("ai cann't move.");
#endif

                    GameBattleUnitMovement.instance.unShow();
                    AIOver();
                    return;
                }
            }
            else
            {
            }

            for (int i = 0; i < cells.Count; i++)
            {
                GameBattleUnitMovement.Cell cell = cells[i];

                if (cell.x == mx && cell.y == my)
                {
                    disMove  = 2;
                    cellMove = cell;
                    break;
                }

                int dis = GameBattlePathFinder.instance.findPathNear(cell.x, cell.y, mx, my,
                                                                     unitMove.block, fly, UnitCampType);

                if ((dis > 0 && dis < disMove) ||
                    (dis > 0 && dis <= disMove && cost > GameBattlePathFinder.instance.cost) ||
                    ((dis > 0 && dis <= disMove && cost == GameBattlePathFinder.instance.cost) && Random.Range(0, 100) > 50))
                {
                    cost       = GameBattlePathFinder.instance.cost;
                    disMove    = dis;
                    cellMove   = cell;
                    moveCostAI = cell.ca;
                }
            }


            gameBattleUnitAIType = GameBattleUnitAIType.Move;

            if (cellMove != null &&
                (cellMove.x != PosX || cellMove.y != PosY))
            {
                int dis2 = GameBattlePathFinder.instance.findPathNear(PosX, PosY, mx, my
                                                                      , unitMove.block, true, UnitCampType);

                if (dis2 > 0 &&
                    disMove > dis2)
                {
                    GameBattleUnitMovement.instance.unShow();
                    AIOver();
                }
                else
                {
                    bool b = false;

                    b = moveTo(cellMove.x, cellMove.y, true, false, 3, AIOnMoveAttackOver);

                    if (!b)
                    {
                        AIOver();
                    }
                    else
                    {
                        GameBattleCursor.instance.show();
                        GameBattleCursor.instance.moveTo(cellMove.x, cellMove.y,
                                                         GameBattleCursor.SpeedX3, GameBattleCursor.SpeedY3,
                                                         true,
                                                         null);
                    }
                }
            }
            else
            {
                AIOnMoveAttackOver();
            }
        }
    }
    public void showUnits(int parm, int check)
    {
        bool b = false;

        GameBattleMan[] Man   = GameBattleManager.instance.ActiveBattleStage.Man;
        GameBattleStage stage = GameBattleManager.instance.ActiveBattleStage;

        for (int i = 0; i < Man.Length; i++)
        {
            GameBattleMan man = Man[i];

            if (man.EventParm1 == parm)
            {
                GameBattleUnit unit = GameBattleUnitManager.instance.getUnit(i);

                //                 if ( unit.IsKilled )
                //                 {
                //                     unit.initHPMP();
                //                     GameBattleXY xy = stage.XY[ i ];
                //                     unit.setPos( xy.X , xy.Y );
                //                 }

                if (unit.IsAlive)
                {
                    unit.setPos(stage.XY[unit.BattleManID].X, stage.XY[unit.BattleManID].Y);
                }

                GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(unit.MoveType);

                GameBattleUnit unit1 = GameBattleUnitManager.instance.getUnit(unit.PosX, unit.PosY);

                if (check == 1 &&
                    unit != unit1 &&
                    unit1 != null)
                {
                    GameBattlePathFinder.instance.findNearPos(unit.PosX, unit.PosY, unitMove.block);

                    unit.setPos(GameBattlePathFinder.instance.nearPosX,
                                GameBattlePathFinder.instance.nearPosY);
                }

                if (check == 1 &&
                    GameBattlePathFinder.instance.isBlockPos(unit.PosX, unit.PosY, unitMove.block, unit))
                {
                    GameBattlePathFinder.instance.findNearPos(unit.PosX, unit.PosY, unitMove.block);

                    unit.setPos(GameBattlePathFinder.instance.nearPosX,
                                GameBattlePathFinder.instance.nearPosY);
                }

                unit.IsShow = true;
                //                            unit.IsActed = true;

                if (unit.IsKilled)
                {
                    unit.IsKilled = false;
                    unit.setAlpha(1.0f);
                    unit.gameObject.SetActive(true);
                }


                unit.updateAlive();
                unit.playAnimationBattle(GameAnimationType.Stand,
                                         GameAnimationDirection.South, null);
                unit.updateInteam();

//                 if ( !b && GameBattleTurn.instance.Turn > 1 )
//                 {
//                     if ( GameUserData.instance.Stage == 12 && parm == 4 )
//                     {
//                         // stage 12 bug
//                         continue;
//                     }
//
//                     b = true;
//
//                     GameBattleCursor.instance.show();
//                     GameBattleCursor.instance.moveTo( unit.PosX , unit.PosY ,
//                                     GameBattleCursor.SpeedX , GameBattleCursor.SpeedY ,
//                                      onShowUnitOver );
//                 }
            }
        }

        GameBattleUnitManager.instance.resetEnemyAI();

        if (!b)
        {
            onShowUnitOver();
        }
    }
    public void canAttackAI(GameBattleUnit unit1, int ux, int uy, int x, int y, GameSkill sk, GameUnitCampType camp, int id)
    {
        targetID = id;

        IsAttackUnits = false;
        checkUnitAI   = false;

        list.Clear();

        int width  = GameBattleManager.instance.Width;
        int height = GameBattleManager.instance.Height;

        int min = (sk.AttackRangeMin == GameDefine.INVALID_ID ? 1 : sk.AttackRangeMin + 1);
        int max = sk.AttackRangeMax;

        if (min > max)
        {
            min = max;
        }

        rangeType = sk.AttackRangeType;
        posX      = x;
        posY      = y;

        switch (rangeType)
        {
        case GameAttackRangeType.Circle:
        {
            for (int i = -max; i <= max; i++)
            {
                for (int j = -max; j <= max; j++)
                {
                    int xx = Mathf.Abs(j);
                    int yy = Mathf.Abs(i);

                    if (xx + yy <= max && xx + yy >= min)
                    {
                        Cell cell = new Cell();
                        cell.x = x + j;
                        cell.y = y + i;
                        list.Add(cell);
                    }
                }
            }

            if (sk.AttackRangeMin == 0)
            {
                Cell cell = new Cell();
                cell.x = x;
                cell.y = y;
                list.Add(cell);
            }

            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].x < 0 || list[i].y < 0 ||
                    list[i].x >= width || list[i].y >= height)
                {
                    continue;
                }

                getUnitsCircleAI(unit1, ux, uy, x, y, list[i].x, list[i].y, sk, camp);
            }
        }
        break;

        case GameAttackRangeType.Line:
        {
            getUnitsLineAI(unit1, ux, uy, sk, camp);
        }
        break;
        }

        if (checkUnitAI)
        {
            for (int i = 0; i < attackUnits.Count; i++)
            {
                GameBattleUnit unit = attackUnits[i];

                GameUnitMove unitMove = GameUnitMoveTypeData.instance.getData(unit.MoveType);
                bool         fly      = unit.checkEffect(GameSkillResutlEffect.Under) ? true : unitMove.fly;

                List <GameBattleUnitMovement.Cell> cells = GameBattleUnitMovement.instance.getMoveList(
                    unit.PosX, unit.PosY, unit.Move, unit.MoveType, fly, unit.UnitCampType);

                for (int j = 0; j < cells.Count; j++)
                {
                    GameBattleUnit u = GameBattleUnitAttackSelection.instance.canAttack(
                        cells[j].x, cells[j].y, unit.Weapon, unit.UnitCampType);

                    if (u != null)
                    {
                        return;
                    }
                }
            }

            list.Clear();
            attackUnits.Clear();
        }
    }