Example #1
0
        public static bool AttackToIndex(GameUnit unit, Vector3Int posTarget)
        {
            var goodNeighbour = AI_Calculation.GetNearestNeighbour(unit.ChunkNumber, unit.CurrentPos, posTarget);

            if (goodNeighbour == Vector3Int.zero ||
                !PathCalcManager.IsReaching(ChunkUtil.GetDovvner(unit.CurrentPos), goodNeighbour))
            {
                return(false);
            }

            var path = PresetPath(unit, goodNeighbour);

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

            path.Add(new TilePosition(
                         Util.Get3DPosByIndex(posTarget.x, posTarget.y, posTarget.z + 1)
                         , posTarget));

            // Debug.Log("Attack order from: " + ent);
            GameOrderManager.DoOrder(unit, path, OrderTypes.AttackOrder);
            return(true);
        }
Example #2
0
        public static DynValue FindNearestReacheble(GameUnit unit, Table t)
        {
            var        dist    = -1;
            GameEntity nearest = null;

            foreach (var v in t.Values)
            {
                var ent = v.ToObject <GameEntity>();
                if (ent == null)
                {
                    continue;
                }

                if (!PathCalcManager.IsReaching(ChunkUtil.GetDovvner(unit.CurrentPos),
                                                ChunkUtil.GetDovvner(ent.CurrentPos)))
                {
                    continue;
                }

                var d = AI_Calculation.GetDistTo(unit.CurrentPos, ent.CurrentPos, false);
                if (dist == -1 || d < dist)
                {
                    dist    = d;
                    nearest = ent;
                }
            }
            return(nearest == null ? DynValue.Nil : DynValue.FromObject(LuaManager.ScriptObj, nearest));
        }
Example #3
0
        public static Vector3Int GetNearestNeighbour(int chunkNumber, Vector3Int myPos, Vector3Int pos)
        {
            var list = GetGoodNeighbors(chunkNumber, pos);

            var minDist = -1;
            var p       = new Vector3Int();

            foreach (var point in list)
            {
                var dist = GetDistTo(myPos, point, true);
                if (minDist != -1 && dist > minDist)
                {
                    continue;
                }
                if ((ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point)) &&
                     myPos != ChunkUtil.GetUpper(point)) ||
                    !ChunkUtil.IsCanStayHere(chunkNumber, ChunkUtil.GetDovvner(point)) ||
                    !PathCalcManager.IsReaching(ChunkUtil.GetDovvner(myPos), ChunkUtil.GetDovvner(point))
                    )
                {
                    continue;
                }

                minDist = dist;
                p       = point;
            }


            return(p);
        }
Example #4
0
        void Start()
        {
            LuaLibrariesIniter.PostInit();
            PathCalcManager.PreSetup();

            LanguageTextManager.Init();
            var map = new DefaultMap();

            SetupNewChunk(map);
            var center = CurrentChunk.Ground[0][(int)(StaticMapSize / 2f)][(int)(StaticMapSize / 2f)].CurrentPos;

            //SetCameraAt(new Vector2Int(center.x, center.y));

            PlayersManager.SetupPlayer();
        }
 public static void InitLibraries(LuaVM vm)
 {
     UnityOs.InitLuaModule(vm);
     ModifiersManager.InitLuaModule();
     ChunkFinder.InitLuaModule();
     SimpleOrderManager.InitLuaModule();
     GameMoveManager.InitLuaModule();
     AI_Calculation.InitLuaModule();
     PathCalcManager.InitLuaModule();
     FlagManager.InitLuaModule();
     ProgressUnitBar.InitLuaModule();
     LuaChunkManager.InitLuaModule();
     ErrorBar_HTML.InitLuaModule();
     LuaHelper.InitLuaModule();
     ResearchManager.InitLuaModule();
 }
Example #6
0
        private const float MinDist = 0.05f; //0.05f

        //False if countinue moving
        private static bool DoMove(UnitOrder order)
        {
            var ent    = order.GetUnit();
            var moveTo = order.GetTo();

            if (ent == null)
            {
                return(true);
            }
            var vec = ((moveTo.Pos3D + ent.ExtraPos) - ent.transform.position);

            //var idealVec = (moveTo.Pos3D - Util.Get3DPosByIndex(ent.CurrentPos)).normalized;


            if (Mathf.Abs(vec.x) <= MinDist && Mathf.Abs(vec.y) <= MinDist)
            {
                ent.transform.position = moveTo.Pos3D + ent.ExtraPos;
                ent.MovingTo           = null;

                var upperIndexFrom = ent.CurrentPos;
                var upperIndexTo   = ChunkUtil.GetUpper(moveTo.Index);

                var chunk = ChunkManager.GetChunkByNum(ent.ChunkNumber);

                chunk.MoveFromTo(upperIndexFrom, upperIndexTo);

                ent.CurrentPos = upperIndexTo;



                if (upperIndexFrom != upperIndexTo)
                {
                    PathCalcManager.CalculatePoint(upperIndexFrom);
                }


                GameOrderManager.RemoveMark(ent, moveTo.Index);

                return(true);
            }
            if (ChunkUtil.GetDovvner(ent.CurrentPos) != order.GetTo().Index&&
                PathFind.IsInvalidPath(order.GetTo().Index))
            {
                SimpleOrderManager.CancelOrders(ent);
            }

            ent.State    = EventManager.InProgressEvents.Move;
            ent.MovingTo = moveTo;
            var speedMod  = GameMoveManager.StaticMoveSpeed;
            var moveSpeed = ent.EntStats.MoveSpeed;

            var modVec = MultVector(vec, new Vector3(speedMod, speedMod, 1f));

            var speedVec =
                MultVector(modVec, new Vector3(moveSpeed, moveSpeed, 1f))
                // vec * speedMod
                * Time.deltaTime;

            if (Mathf.Abs(vec.x) <= MinDist * 3 && Mathf.Abs(vec.y) <= MinDist * 3)
            {
                speedVec *= 3;
            }


            ent.transform.Translate(speedVec);
            return(false);
        }
Example #7
0
        void RghtClickedOnGameObject(GameObject obj)
        {
            if (_choosed)
            {
                var from = _choosedObj.GetComponent <GameUnit>();
                if (from == null || !from.IsMy())
                {
                    return;
                }
                var to = obj.GetComponent <GameEntity>();

                if (to == null)
                {
                    return;
                }

                if (!GroupUtil.isCreatureGroup(from.Group))
                {
                    return;
                }


                if (!FieldOfView.IsVisible(to) && !GroupUtil.IsGround(to.Group))
                {
                    ClickedOnSpace();
                    return;
                }

                Vector3Int f;
                if (from.MovingTo != null)
                {
                    f = from.MovingTo.Index;
                }
                else
                {
                    f = from.CurrentPos;
                    f.z--;
                }

                //Cant move ?
                if (from.UpgradedStats.MoveSpeed <= 0f)
                {
                    SimpleOrderManager.CancelOrders(from);
                    ErrorBar_HTML.SetupError("This unit can`t move!");
                    return;
                }

                //Attack
                var t             = to.CurrentPos;
                var buildingCheck = false;
                if (ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)))
                {
                    var underGround = ChunkManager.CurrentChunk.GetGameObjectByIndex(ChunkUtil.GetUpper(t));
                    var underEnt    = underGround.GetComponent <GameEntity>();
                    if (SecondaryGroundLvL.isSecondaryGroup(underEnt.Group))
                    {
                        buildingCheck = true;
                    }

                    if (!GroupUtil.IsGround(underEnt.Group) && underEnt.Owner != PlayersManager.GetMyPlayer())
                    {
                        var underP = underEnt.CurrentPos;
                        underP.z++;

                        if (!ChunkManager.CurrentChunk.IsMapPos(underP) ||
                            !ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(underEnt.CurrentPos)))
                        {
                            if (from.UpgradedStats.Dmg > 0)
                            {
                                SimpleOrderManager.AttackToIndex(from, t);
                            }
                            else if (from.UpgradedStats.Dmg == 0)
                            {
                                ErrorBar_HTML.SetupError("This unit can`t attack!");
                            }
                            return;
                        }
                    }
                }


                //Cancel or Move
                if ((ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)) ||
                     !PathCalcManager.IsReaching(f, t)) && !buildingCheck)
                {
                    ErrorBar_HTML.SetupError("Can`t reach that place!");
                    SimpleOrderManager.CancelOrders(from);
                }
                else if (ChunkUtil.IsCanStayHere(from.ChunkNumber, t) || buildingCheck)
                {
                    SimpleOrderManager.MoveToIndex(from, t);
                }
            }
            else
            {
                var clicked = obj.GetComponent <GameEntity>();
                if (clicked == null)
                {
                    return;
                }

                var pos = clicked.CurrentPos;

                if (FlagManager.IsFlagAtPos(pos))
                {
                    FlagManager.RemoveFlag(pos);
                }
                else
                {
                    FlagManager.SetupFlag(pos);
                }
            }
        }
Example #8
0
        public static GameEntity InitObject(int chunkNumber, string name, Vector3Int vecInt, int index,
                                            Table npc, Player owner)
        {
            var chunk = GetChunkByNum(chunkNumber);

            if (chunk == null)
            {
                return(null);
            }

            var vec = Util.Get3DPosByIndex(vecInt.x, vecInt.y, vecInt.z);

            var vecIntPos = new Vector3Int(vecInt.x, vecInt.y, vecInt.z);


            var prefab = Loader.GetPrefabByIndex(index);
            var obj    = Instantiate(prefab, vec + prefab.transform.position, new Quaternion());

            if (IsGroupVVithName(prefab.name))
            {
                var trans = GetGroupVVithName(prefab.name);
                obj.transform.SetParent(trans);
            }
            else
            {
                var o = new GameObject {
                    name = prefab.name
                };

                o.transform.SetParent(BATYAtrans);
                Groups.Add(o);

                obj.transform.SetParent(o.transform);
            }


            var group = LuaNpcGetter.GetNpcGroup(npc);

            Stats stats = null;

            if (!GroupUtil.IsGround(group))
            {
                stats     = LuaNpcGetter.GetStatsFromTable(npc);
                obj.layer = 9;


                if (staticFogEnabled)
                {
                    obj.AddComponent <FogCoverable>();
                }
            }


            var abilities = new List <Ability>();

            if (!GroupUtil.IsGround(group))
            {
                if (!GroupUtil.IsItem(group) && staticFogEnabled)
                {
                    var spRend = obj.GetComponent <SpriteRenderer>();
                    if (spRend != null)
                    {
                        spRend.enabled = false;
                    }
                }

                if (!GroupUtil.IsNeutral(group) && !GroupUtil.IsItem(group))
                {
                    var npcAbilitiesNames = LuaNpcGetter.GetNpcAbilitiesNames(npc);
                    foreach (var KV in npcAbilitiesNames)
                    {
                        var ability = LuaAbilitiesGetter.GetAbility(KV.Value);
                        ability.Owner = owner;
                        abilities.Add(ability);
                    }
                }
            }

            GameEntity ent;

            if (GroupUtil.IsGround(group))
            {
                ent = obj.AddComponent <GameEntity>();
                ent.Init(chunkNumber, vecIntPos, false, index);
                ent.Group = group;

                chunk.SetIndex(vecIntPos, index);

                obj.layer = 8;
                chunk.IndexMas[vecInt.z][vecInt.x][vecInt.y] = index;
                chunk.Ground[vecInt.z][vecInt.x][vecInt.y]   = ent;
            }
            else if (GroupUtil.IsItem(group))
            {
                var item = obj.AddComponent <GameItem>();

                item.Init(chunkNumber, vecIntPos, false, index, stats);
                item.Group = group;

                var npcModifiersNames = LuaNpcGetter.GetNpcModifiersNames(npc);
                item.ModifierNamesList.AddRange(npcModifiersNames);

                ent = item;
            }
            else
            {
                var unit = obj.AddComponent <GameUnit>();

                unit.Init(chunkNumber, vecIntPos, false, index, stats);
                unit.Group = group;

                var soundVolume  = LuaNpcGetter.GetNpcSoundVolume(npc);
                var soundByIndex = Loader.GetSoundByIndex(index, soundVolume);

                unit.GameAudio = soundByIndex;
                unit.itemDrop  = LuaNpcGetter.GetNpcItemDrop(npc);

                CreatureGroupManager.Init(unit);

                if (GroupUtil.isBuilding(group))
                {
                    var researchName = LuaNpcGetter.GetNpcResearch(npc);
                    if (researchName.Length > 0)
                    {
                        var research = ResearchManager.SetupResearch(researchName);
                        unit.research = research;
                        owner.AddResearch(research);
                        AbilityBar_HTML.Update();
                    }
                    BuildingsGroupManager.Init(unit);
                }

                foreach (var ab in abilities)
                {
                    unit.AddAbility(ab);
                }

                ent = unit;
                chunk.IndexMas[vecInt.z][vecInt.x][vecInt.y] = index;
                chunk.Ground[vecInt.z][vecInt.x][vecInt.y]   = ent;
            }
            ent.OriginalName = name;
            ent.ExtraPos     = prefab.transform.position;


            ent.foodCost = LuaNpcGetter.GetNpcFoodCost(npc);
            ent.foodGive = LuaNpcGetter.GetNpcFoodGive(npc);

            owner.foodCount += ent.foodCost;
            owner.foodMax   += ent.foodGive;


            ent.goldDrop = LuaNpcGetter.GetNpcGoldDrop(npc);


            //obj.name = prefab.name + "_" + vecInt.z + "_" + vecInt.x + "_" + vecInt.y;


            PathCalcManager.CalculatePoint(vecInt);
            PathCalcManager.CalculatePoint(ChunkUtil.GetDovvner(vecInt));


            return(ent);
        }
Example #9
0
        public static void Update(AbstractGameObject unit)
        {
            var EvolutionTime = unit.EvolutionTime;

            if (unit.State != EventManager.InProgressEvents.Stay)
            {
                ProgressUnitBar.RemoveProgressBar(unit);


                if (stackTarget.ContainsKey(unit))
                {
                    var target       = stackTarget[unit];
                    var progressName = ProgressUnitBar.ProgressName.GroupEvolution;
                    ProgressUnitBar.RemoveProgressBar(target, progressName);
                    stackTarget.Remove(unit);
                    stackTarget.Remove(target);
                }

                return;
            }

            var chunk = ChunkManager.GetChunkByNum(unit.ChunkNumber);

            //Solo evolution
            if (unit.SoloEvolution)
            {
                var progressName = ProgressUnitBar.ProgressName.SoloEvolution;
                var pos          = unit.CurrentPos;
                if (!SecondaryGroundLvL.IsEmptyPos(chunk.ChunkNumber, pos) &&
                    ChunkUtil.IsAnyEntity(chunk.ChunkNumber, pos))
                {
                    ProgressUnitBar.RemoveProgressBar(unit, progressName);
                    return;
                }

                ProgressUnitBar.Setup(unit, progressName, EvolutionTime);


                if (!ProgressUnitBar.IsReady(unit) ||
                    !ProgressUnitBar.IsThisProgressName(unit, progressName))
                {
                    return;
                }

                // Debug.Log("Original name = " + unit.OriginalName);


                if (SoloEvolutionDict.ContainsKey(unit.OriginalName) || unit.EvolutionNext.Length > 0)
                {
                    var evoName = "";
                    evoName = unit.EvolutionNext.Length == 0
                        ? SoloEvolutionDict[unit.OriginalName]
                        : unit.EvolutionNext;

                    if (evoName.Length > 0)
                    {
                        //  Debug.Log("Evolution name = " + evoName);

                        var prevIndex = ChunkUtil.GetIndex(chunk.ChunkNumber, pos);
                        var prevEnt   = chunk.GetGameObjectByIndex(pos);
                        var check     = unit.PrefabIndex == prevIndex;


                        unit.KillSelf();

                        if (!SecondaryGroundLvL.IsEmptyPos(chunk.ChunkNumber, pos))
                        {
                            SecondaryGroundLvL.GetGroundEnt(chunk.ChunkNumber, pos).KillSelf();
                            SecondaryGroundLvL.RemovePos(chunk.ChunkNumber, pos);
                        }

                        var ent = chunk.PreSetupObject(evoName, pos, unit.Owner);

                        if (GroupUtil.isBuilding(ent.Group) || GroupUtil.IsItem(ent.Group))
                        {
                            chunk.SetupItem(ent, evoName, pos, unit.Owner);
                        }
                        else
                        {
                            chunk.SetupUnit(ent, evoName, pos, unit.Owner);
                        }
                    }
                }
            }
            //Group evolution
            else if (stackResult.ContainsKey(unit.OriginalName))
            {
                var progressName = ProgressUnitBar.ProgressName.GroupEvolution;
                var friends      = AI_Calculation.GetNearFriendUnits(chunk.ChunkNumber, unit, unit.CurrentPos);


                GameEntity target = null;
                if (stackTarget.ContainsKey(unit))
                {
                    target = stackTarget[unit];
                }

                if (target != null && !friends.Contains(target as GameUnit))
                {
                    ProgressUnitBar.RemoveProgressBar(target, progressName);
                    target = null;
                    stackTarget.Remove(unit);
                }
                foreach (var obj in friends)
                {
                    if (obj.Destroyed)
                    {
                        continue;
                    }
                    if (!stackResult[unit.OriginalName].ContainsKey(obj.OriginalName))
                    {
                        continue;
                    }
                    if (obj.State != EventManager.InProgressEvents.Stay)
                    {
                        continue;
                    }

                    stackTarget[unit] = obj;
                    target            = obj;
                    break;
                }

                if (target == null)
                {
                    return;
                }

                ProgressUnitBar.Setup(unit, progressName, EvolutionTime);

                //EvolutionTimeList[target] = EvolutionTimeList[ent];
                // UpdateProgressBar(target);

                if (!ProgressUnitBar.IsReady(unit) ||
                    !ProgressUnitBar.IsThisProgressName(unit, progressName))
                {
                    return;
                }

                var evoName = stackResult[unit.OriginalName][target.OriginalName];
                var pos     = unit.CurrentPos;
                var owner   = unit.Owner;

                unit.KillSelf();
                target.KillSelf();

                var evoUnit = chunk.SetupUnit(evoName, pos, owner);
                QuestManager.OnEvolution(evoUnit);

                Coloring.RecolorObject(ChunkUtil.GetDovvner(unit.CurrentPos));
                Coloring.RecolorObject(ChunkUtil.GetDovvner(target.CurrentPos));

                UnitEvents.OnEvolution(evoUnit);

                PathCalcManager.CalculatePoint(ChunkUtil.GetDovvner(pos));
                PathCalcManager.CalculatePoint(pos);
            }
        }