Пример #1
0
    void SetEscapeFromCity(int cIdx)
    {
        CityInfo cInfo = Informations.Instance.GetCityInfo(cIdx);

        do
        {
            ArmyInfo ai = new ArmyInfo();
            ai.king  = cInfo.king;
            ai.money = 0;

            Informations.Instance.armys.Add(ai);

            int count = cInfo.generals.Count;
            int min   = count - 5;
            min = Mathf.Clamp(min, 0, count);
            for (int i = count - 1; i >= min; i--)
            {
                int g = cInfo.generals[i];
                ai.generals.Add(g);
                cInfo.generals.RemoveAt(i);

                Informations.Instance.GetGeneralInfo(g).city = -1;
            }

            ai.commander = -1;
            FindArmyCommander(ai);

            int        cityEscTo = -1;
            List <int> clist     = MyPathfinding.GetCityNearbyIdx(cIdx);
            List <int> canGoList = new List <int>();

            if (clist.Count == 1)
            {
                cityEscTo = clist[0];
            }
            else
            {
                for (int i = 0; i < clist.Count; i++)
                {
                    if (Informations.Instance.GetCityInfo(clist[i]).king == ai.king)
                    {
                        canGoList.Add(clist[i]);
                    }
                }

                if (canGoList.Count > 0)
                {
                    cityEscTo = canGoList[Random.Range(0, canGoList.Count)];
                }

                if (cityEscTo == -1)
                {
                    for (int i = 0; i < clist.Count; i++)
                    {
                        if (Informations.Instance.GetCityInfo(clist[i]).king == -1)
                        {
                            canGoList.Add(clist[i]);
                        }
                    }

                    if (canGoList.Count > 0)
                    {
                        cityEscTo = canGoList[Random.Range(0, canGoList.Count)];
                    }
                }

                if (cityEscTo == -1)
                {
                    cityEscTo = clist[Random.Range(0, clist.Count)];
                }
            }

            ai.cityFrom = cityAttacked;
            ai.cityTo   = cityEscTo;

            ai.state = (int)ArmyController.ArmyState.Escape;
            ai.pos   = Vector3.zero;
        } while(cInfo.generals.Count > 0);
    }
Пример #2
0
    void CityAct(int cIdx)
    {
        CityInfo cInfo = Informations.Instance.GetCityInfo(cIdx);

        if (cInfo.king == -1 ||
            cInfo.king == Controller.kingIndex ||
            cInfo.king == Informations.Instance.kingNum)
        {
            return;
        }

        foreach (int gIdx in cInfo.generals)
        {
            if (cInfo.reservist == 0)
            {
                break;
            }

            GeneralInfo gInfo = Informations.Instance.GetGeneralInfo(gIdx);

            if (gInfo.knightCur < gInfo.knightMax)
            {
                int soldierAdd = gInfo.knightMax - gInfo.knightCur;
                soldierAdd = Mathf.Clamp(soldierAdd, 0, cInfo.reservist);

                gInfo.knightCur += soldierAdd;
                cInfo.reservist -= soldierAdd;
            }

            if (cInfo.reservist == 0)
            {
                break;
            }

            if (gInfo.soldierCur < gInfo.soldierMax)
            {
                int soldierAdd = gInfo.soldierMax - gInfo.soldierCur;
                soldierAdd = Mathf.Clamp(soldierAdd, 0, cInfo.reservist);

                gInfo.soldierCur += soldierAdd;
                cInfo.reservist  -= soldierAdd;
            }
        }

        List <int> cities = MyPathfinding.GetCityNearbyIdx(cIdx);

        for (int i = 0; i < cities.Count; i++)
        {
            CityInfo cityNeighbor = Informations.Instance.GetCityInfo(cities[i]);

            int count = 0;
            for (int j = 0; j < cInfo.generals.Count; j++)
            {
                GeneralInfo gInfo = Informations.Instance.GetGeneralInfo(cInfo.generals[j]);
                if (gInfo.healthCur > 50 && (gInfo.soldierCur + gInfo.knightCur) > (gInfo.soldierMax + gInfo.knightMax) / 2)
                {
                    count++;
                }
            }

            if (cityNeighbor.king == cInfo.king)
            {
                if (count < 8 && cityNeighbor.generals.Count > 2 && Random.Range(0, 100) > 88)
                {
                    SetArmyMove(cities[i], cIdx, Random.Range(1, cityNeighbor.generals.Count), 5);
                    break;
                }
            }
            else
            {
                if (cityNeighbor.king == -1)
                {
                    if (count > 3 && Random.Range(0, 100) > 80)
                    {
                        SetArmyMove(cIdx, cities[i], Random.Range(1, cInfo.generals.Count), 5);
                        break;
                    }
                }
                else
                {
                    if (count > 2 && Random.Range(0, 100) > 88)
                    {
                        if (count - cityNeighbor.generals.Count >= 2)
                        {
                            SetArmyMove(cIdx, cities[i], Random.Range(cityNeighbor.generals.Count + 1, cInfo.generals.Count), 5);
                            break;
                        }
                        else if (count - cityNeighbor.generals.Count > 0 && Random.Range(0, 100) > 50)
                        {
                            SetArmyMove(cIdx, cities[i], cityNeighbor.generals.Count, 5);
                            break;
                        }
                        else if (count - cityNeighbor.generals.Count == 0 && Random.Range(0, 100) > 88)
                        {
                            SetArmyMove(cIdx, cities[i], cityNeighbor.generals.Count - 1, 5);
                            break;
                        }
                    }
                }
            }
        }
    }
Пример #3
0
    void SetEscapeFromCity(int cIdx)
    {
        CityInfo cInfo = Informations.Instance.GetCityInfo(cIdx);

        do
        {
            GameObject     go = (GameObject)Instantiate(armyPrefab, pathfinding.GetCityPos(cIdx), transform.rotation);
            ArmyController ac = go.GetComponent <ArmyController>();
            ArmyInfo       ai = new ArmyInfo();
            ai.king     = cInfo.king;
            ai.money    = 0;
            ai.armyCtrl = ac;

            ac.armyInfo = ai;
            ac.SetArmyKingFlag();

            Informations.Instance.armys.Add(ai);
            ai.armyCtrl.SetArmyKingFlag();

            go.transform.parent = GameObject.Find("ArmiesRoot").transform;

            int count = cInfo.generals.Count;
            int min   = count - 5;
            min = Mathf.Clamp(min, 0, count);
            for (int i = count - 1; i >= min; i--)
            {
                int g = cInfo.generals[i];
                ai.generals.Add(g);
                cInfo.generals.RemoveAt(i);

                Informations.Instance.GetGeneralInfo(g).city = -1;
            }

            ai.commander = -1;
            ai.armyCtrl.FindArmyCommander();

            int        cityEscTo = -1;
            List <int> clist     = MyPathfinding.GetCityNearbyIdx(cIdx);
            List <int> canGoList = new List <int>();

            if (clist.Count == 1)
            {
                cityEscTo = clist[0];
            }
            else
            {
                for (int i = 0; i < clist.Count; i++)
                {
                    if (Informations.Instance.GetCityInfo(clist[i]).king == ai.king)
                    {
                        canGoList.Add(clist[i]);
                    }
                }

                if (canGoList.Count > 0)
                {
                    cityEscTo = canGoList[Random.Range(0, canGoList.Count)];
                }

                if (cityEscTo == -1)
                {
                    for (int i = 0; i < clist.Count; i++)
                    {
                        if (Informations.Instance.GetCityInfo(clist[i]).king == -1)
                        {
                            canGoList.Add(clist[i]);
                        }
                    }

                    if (canGoList.Count > 0)
                    {
                        cityEscTo = canGoList[Random.Range(0, canGoList.Count)];
                    }
                }

                if (cityEscTo == -1)
                {
                    cityEscTo = clist[Random.Range(0, clist.Count)];
                }
            }

            ai.cityFrom = cIdx;
            ai.cityTo   = cityEscTo;

            if (pathfinding == null)
            {
                pathfinding = GameObject.FindWithTag("Pathfinding").GetComponent <MyPathfinding>();
            }

            ai.armyCtrl.SetRoute(pathfinding.GetRoute(ai.armyCtrl.transform.position, ai.cityTo));
            ai.armyCtrl.SetArmyEscape();
        } while (cInfo.generals.Count > 0);
    }