Esempio n. 1
0
        public static void CalculatePoint(Vector3Int pos)
        {
            if (!_preCalculated)
            {
                return;
            }
            var groundPos = ChunkUtil.GetDovvner(pos);


            var neighbourPoints = new Vector3Int[4];

            neighbourPoints[0] = new Vector3Int(pos.x + 1, pos.y, pos.z - 1);
            neighbourPoints[1] = new Vector3Int(pos.x - 1, pos.y, pos.z - 1);
            neighbourPoints[2] = new Vector3Int(pos.x, pos.y + 1, pos.z - 1);
            neighbourPoints[3] = new Vector3Int(pos.x, pos.y - 1, pos.z - 1);

            var chunk = ChunkManager.CurrentChunk;

            foreach (var point in neighbourPoints)
            {
                if (!chunk.IsMapPos(point))
                {
                    continue;
                }
                OnElementCalculate(point.x, point.y, point.z);

                var ent = chunk.GetGameObjectByIndex(point);
                if (ent != null)
                {
                    ent.PathInt = ReachMasInts[point.z][point.x][point.y];
                }
            }
            OnElementCalculate(groundPos.x, groundPos.y, groundPos.z);
        }
Esempio n. 2
0
 public static bool IsInvalidPoint(int chunkNumber, Vector3Int point)
 {
     return((point.z + 1 < ChunkManager.MaxGroundsLvls &&
             ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point))) ||
            ChunkUtil.IsGround(chunkNumber, point) ||
            ChunkUtil.IsEntity(chunkNumber, point));
 }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public static GameItem SpawnItem(string itemName, Vector3Int pos, Player owner)
        {
            if (owner == null)
            {
                owner = PlayersManager.Empty;
            }
            GameEntity ent   = null;
            var        chunk = ChunkManager.CurrentChunk;

            if (ChunkUtil.IsAnyEntity(chunk.ChunkNumber, pos))
            {
                ent = chunk.GetGameObjectByIndex(pos);
            }

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

            var item = chunk.SetupItem(itemName, pos, owner);

            SecondaryGroundLvL.SetGroundEnt(chunk.ChunkNumber, pos, item);

            chunk.SetIndex(pos, -1);
            chunk.SetObjectAtPos(pos, null);

            if (ent != null)
            {
                chunk.SetIndex(pos, ent.PrefabIndex);
                chunk.SetObjectAtPos(pos, ent);
            }

            return(item);
        }
Esempio n. 5
0
        public static void CastAbility(GameUnit unit, Vector3Int posTarget, Ability ability)
        {
            if (unit == null)
            {
                return;
            }
            if (posTarget == Vector3Int.zero)
            {
                return;
            }
            var goodNeighbour =
                AI_Calculation.FindFreePosNearPos(unit.ChunkNumber, ChunkUtil.GetUpper(posTarget), false);

            if (goodNeighbour == Vector3Int.zero)
            {
                Debug.Log("Neighbour is zero");
                return;
            }
            var neighDist = Util.GetDistanceFromTo(goodNeighbour, posTarget);
            var dist      = Util.GetDistanceFromTo(unit.CurrentPos, posTarget);


            if (dist > 1)
            {
                if (neighDist > 1)
                {
                    Debug.Log("Error. Cant reach target");
                    return;
                }
                MoveToIndex(unit, ChunkUtil.GetDovvner(goodNeighbour));
            }

            GameOrderManager.AddCastAbilityOrder(unit, new TilePosition(ChunkUtil.GetUpper(posTarget)), ability);
        }
Esempio n. 6
0
        public static bool Execute(UnitOrder order)
        {
            //Debug.Log("Executing to pos - " + order.GetTo().Index);
            var index       = ChunkUtil.GetUpper(order.GetTo().Index);
            var chunkNumber = order.GetUnit().ChunkNumber;
            var chunk       = ChunkManager.GetChunkByNum(chunkNumber);

            if (order.GetUnit().CurrentPos != index &&
                ChunkUtil.IsAnyEntity(chunkNumber, index))
            {
                var obj = chunk.GetGameObjectByIndex(index);
                if (obj == null)
                {
                    return(GameMoveManager.CancelPathVVay(order.GetUnit())); //return false;
                }
                var underEnt = obj.GetComponent <GameEntity>();
                if (underEnt.Owner != order.GetUnit().Owner)
                {
                    return(AttackTo(order));
                }
                //Debug.Log("Cancel attacking");
                return(GameMoveManager.CancelPathVVay(order.GetUnit())); //return false;
            }
            //Debug.Log("Moving to: " + order.GetTo().Index);
            return(MoveExecutor.Execute(order));
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public virtual void KillSelf()
        {
            if (Destroyed)
            {
                return;
            }
            var chunk = ChunkManager.GetChunkByNum(ChunkNumber);


            chunk.SetIndex(CurrentPos, -1);
            chunk.RemoveObject(CurrentPos);


            var ent = chunk.GetGameObjectByIndex(ChunkUtil.GetDovvner(CurrentPos));

            if (ent != null)
            {
                Coloring.RecolorObject(ent.CurrentPos);
            }


            Destroy(gameObject);

            if (SecondaryGroundLvL.isSecondaryGroup(Group))
            {
                SecondaryGroundLvL.RemovePos(ChunkNumber, CurrentPos);
            }


            Destroyed = true;
        }
Esempio n. 9
0
        public Vector3Int GetFreePosAnyLvl()
        {
            var emptySlots = new List <Vector3Int>();

            for (var i = 0; i < IndexMas[1].Length; i++)
            {
                for (var j = 0; j < IndexMas[1][i].Length; j++)
                {
                    for (int z = 1; z < ChunkManager.MaxGroundsLvls; z++)
                    {
                        if (IndexMas[1][i][j] != -1 ||
                            !ChunkUtil.IsGround(ChunkNumber,
                                                ChunkUtil.GetDovvner(new Vector3Int(i, j, z))))
                        {
                            continue;
                        }
                        emptySlots.Add(new Vector3Int(i, j, z));
                    }
                }
            }
            if (emptySlots.Count == 0)
            {
                return(new Vector3Int(0, 0, 0));
            }

            var range = Random.Range(0, emptySlots.Count);

            return(emptySlots[range]);
        }
Esempio n. 10
0
        public static void CancelOrders(GameUnit unit)
        {
            if (OrderList.ContainsKey(unit))
            {
                //OrderList[ent].Clear();

                var unitOrders = OrderList[unit];
                var cancList   = CanceledOrderList.ContainsKey(unit)
                    ? CanceledOrderList[unit]
                    : new List <UnitOrder>();
                cancList.AddRange(unitOrders);
                CanceledOrderList[unit] = cancList;
            }
            if (!unit.IsMoving)
            {
                return;
            }

            var tilePos = unit.CurrentTilePosition;

            tilePos.Index = ChunkUtil.GetDovvner(tilePos.Index);

            AddOrder(unit,
                     new List <UnitOrder> {
                new UnitOrder(unit, tilePos, OrderTypes.MoveOrder)
            });
            unit.CanceledOrders = true;
        }
Esempio n. 11
0
        public virtual bool IsTimeForReproduction(GameUnit unit)
        {
            var lairPos = ChunkUtil.GetUpper(unit.GroupObj.GetLairPos());

            return(IsSetUpedLairPos(unit) &&
                   !SecondaryGroundLvL.IsEmptyPos(unit.ChunkNumber, lairPos) &&
                   SecondaryGroundLvL.GetGroundEnt(unit.ChunkNumber, lairPos).OriginalName == "building_lair");
        }
Esempio n. 12
0
        public static bool IsInvalidPath(Vector3Int point)
        {
            var chunkNumber = ChunkManager.CurrentChunk.ChunkNumber;

            return((point.z + 1 < ChunkManager.MaxGroundsLvls &&
                    ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point))) ||
                   ChunkUtil.IsMarkedIndex(chunkNumber, point) ||
                   ChunkUtil.IsEntity(chunkNumber, point));
        }
Esempio n. 13
0
        public GameItem SetupItem(GameEntity ent, string name, Vector3Int pos, Player owner)
        {
            var index = Loader.GetIndexByName(name);
            var npc   = LuaNpcGetter.GetNpcById(index);

            ent.Owner        = owner;
            ent.OriginalName = name;
            ent.name         = name;
            var item = ent as GameItem;

            var evoTo = LuaNpcGetter.GetEvolutionTo(npc);

            if (evoTo.Length > 0)
            {
                if (!UnitEvolution.IsHasSoloEvolution(name))
                {
                    UnitEvolution.AddToSoloDict(name, evoTo);
                }

                if (item != null)
                {
                    item.SoloEvolution = true;
                }
            }

            var evoCross = LuaNpcGetter.GetNpcEvoCrossing(npc);

            if (evoCross.Keys.Count > 0)
            {
                foreach (var pair in evoCross)
                {
                    UnitEvolution.AddToStackDict(name, pair.Key, pair.Value);

                    if (!string.Equals(pair.Key, name, StringComparison.OrdinalIgnoreCase))
                    {
                        UnitEvolution.AddToStackDict(pair.Key, name, pair.Value);
                    }
                }
            }
            if (GroupUtil.IsItem(ent.Group))
            {
                SecondaryGroundLvL.SetGroundEnt(ChunkNumber, pos, item);
            }

            if (GroupUtil.isBuilding(ent.Group))
            {
                ChunkManager.AddVision(ent);
            }
            ItemEvents.OnCreateItem(item, firstCreate);

            Coloring.RecolorObject(ChunkUtil.GetDovvner(ent.CurrentPos));

            return(item);
        }
Esempio n. 14
0
        public static bool MoveTo(GameUnit unit, Vector3Int vecTo)
        {
            var indexPos = ChunkUtil.GetDovvner(vecTo);
            var pos3D    = Util.Get3DPosByIndex(vecTo);

            return(MoveExecutor.Execute(new UnitOrder
                                        (
                                            unit,
                                            new TilePosition(pos3D, indexPos),
                                            OrderTypes.MoveOrder
                                        )));
        }
Esempio n. 15
0
        public static bool IsAttackUnitInMemoryPos(GameUnit unit, GameUnit attackTarget)
        {
            if (!unit.IsMoving)
            {
                return(false);
            }
            var pos = unit.MovingPath.LastOrDefault();

            if (pos == null)
            {
                return(false);
            }
            return(ChunkUtil.GetDovvner(attackTarget.CurrentPos) == pos.Index);
        }
Esempio n. 16
0
        static void OnGroundEntity(GameEntity ent)
        {
            var eUP = ChunkManager.CurrentChunk.GetGameObjectByIndex
                          (ChunkUtil.GetUpper(ent.CurrentPos));

            if (eUP != null && IsIgnored(eUP) && IsVisionBefore(eUP))
            {
                SemiColor(ent, eUP);
            }
            else
            {
                HidingColor(ent, eUP);
            }
        }
Esempio n. 17
0
        public static void Update(GameUnit unit)
        {
            if (unit == null || unit.gameObject == null)
            {
                return;
            }
            if (!timeDict.ContainsKey(unit))
            {
                timeDict[unit] = 0;
            }

            timeDict[unit] += Time.deltaTime;

            if (timeDict[unit] < TIME_AI)
            {
                return;
            }
            timeDict[unit] = 0;

            var _ai_state = "";

            if (AI_Calculation.IsNearEnemy(unit, enemyRadiusCheck))
            {
                var p = new Vector3Int(-1, -1, -1);

                var path = unit.MovingPath.ToArray().LastOrDefault();
                if (path != null)
                {
                    p = path.Index;
                }


                var enemy = AI_Calculation.GetNearEnemy(unit, enemyRadiusCheck);
                if (!unit.IsMoving ||
                    (p != new Vector3Int(-1, -1, -1) &&
                     p != ChunkUtil.GetDovvner(enemy.CurrentPos)))
                {
                    SimpleOrderManager.AttackTarget(unit, enemy as GameUnit);
                }

                _ai_state = "AttackEnemy";
            }

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (AI_DEBUG && _ai_state != "")
            {
                Debug.LogError(unit + "  State: " + _ai_state);
            }
        }
Esempio n. 18
0
        int[][] MarkGroundUpper(int[][] map, int[][] upperMap)
        {
            for (int i = 0; i < map.Length; i++)
            {
                for (int j = 0; j < map[i].Length; j++)
                {
                    if (!ChunkUtil.IsGroundIndex(map[i][j]))
                    {
                        upperMap[i][j] = -1;
                    }
                }
            }

            return(upperMap);
        }
Esempio n. 19
0
        public GameUnit SetupUnit(GameEntity ent, string name, Vector3Int pos, Player owner)
        {
            var index = Loader.GetIndexByName(name);
            var npc   = LuaNpcGetter.GetNpcById(index);

            ent.Owner        = owner;
            ent.OriginalName = name;
            ent.name         = name;
            var unit = ent as GameUnit;

            var evoTo = LuaNpcGetter.GetEvolutionTo(npc);

            if (evoTo.Length > 0)
            {
                if (!UnitEvolution.IsHasSoloEvolution(name))
                {
                    UnitEvolution.AddToSoloDict(name, evoTo);
                }

                if (unit != null)
                {
                    unit.SoloEvolution = true;
                }
            }

            var evoCross = LuaNpcGetter.GetNpcEvoCrossing(npc);

            if (evoCross.Keys.Count > 0)
            {
                foreach (var pair in evoCross)
                {
                    UnitEvolution.AddToStackDict(name, pair.Key, pair.Value);

                    if (!string.Equals(pair.Key, name, StringComparison.OrdinalIgnoreCase))
                    {
                        UnitEvolution.AddToStackDict(pair.Key, name, pair.Value);
                    }
                }
            }

            ChunkManager.AddVision(ent);
            UnitEvents.OnUnitSpawned(unit);


            Coloring.RecolorObject(ChunkUtil.GetDovvner(ent.CurrentPos));

            return(unit);
        }
Esempio n. 20
0
        public virtual bool IsTimeForDeliverItem(GameUnit unit)
        {
            var lairPos = ChunkUtil.GetUpper(unit.GroupObj.GetLairPos());

            if (unit.pickUped == null || !unit.GroupObj.IsSetUpedLairPos(unit) ||
                (ChunkUtil.IsAnyEntity(unit.ChunkNumber, lairPos) && unit.CurrentPos != lairPos))
            {
                return(false);
            }
            if (!SecondaryGroundLvL.IsEmptyPos(unit.ChunkNumber, lairPos))
            {
                return(ItemGroup.IsCanBeStacked(unit.pickUped.OriginalName,
                                                SecondaryGroundLvL.GetGroundEnt(unit.ChunkNumber, lairPos).OriginalName));
            }
            return(true);
        }
Esempio n. 21
0
        public virtual bool IsTimeForPickupItem(GameUnit unit)
        {
            if (SecondaryGroundLvL.IsEmptyPos(unit.ChunkNumber, unit.CurrentPos))
            {
                return(false);
            }
            var item = SecondaryGroundLvL.GetGroundEnt(unit.ChunkNumber, unit.CurrentPos);

            return(item != null &&
                   item.CanBePickuped &&
                   !item.SoloEvolution &&
                   item.Group == "item" &&
                   unit.pickUped == null &&
                   TreeGroup.IsPickableTreeLog(item) &&
                   unit.GroupObj.GetLairPos() != ChunkUtil.GetDovvner(unit.CurrentPos));
        }
Esempio n. 22
0
        public static bool IsNearEnt(int chunkNumber, Vector3Int pos)
        {
            var neighbourPoints = new Vector3Int[8];

            neighbourPoints[0] = new Vector3Int(pos.x + 1, pos.y, pos.z);
            neighbourPoints[1] = new Vector3Int(pos.x - 1, pos.y, pos.z);
            neighbourPoints[2] = new Vector3Int(pos.x, pos.y + 1, pos.z);
            neighbourPoints[3] = new Vector3Int(pos.x, pos.y - 1, pos.z);

            var chunk = ChunkManager.GetChunkByNum(chunkNumber);

            return((from point in neighbourPoints
                    where chunk.IsMapPos(point) && ChunkUtil.IsEntity(chunkNumber, point)
                    select ChunkUtil.GetIndex(chunkNumber, point)).Any(
                       index => index > 0));
        }
Esempio n. 23
0
        /**
         * Reads whole (including chunk type) string block from stream.
         * Stream must be at the chunk type.
         */
        public static StringBlock read(IntReader reader)
        {
            ChunkUtil.readCheckType(reader, CHUNK_TYPE);

            int chunkSize        = reader.readInt();
            int stringCount      = reader.readInt();
            int styleOffsetCount = reader.readInt();

            /*?*/ reader.readInt();
            int stringsOffset = reader.readInt();
            int stylesOffset  = reader.readInt();

            StringBlock block = new StringBlock();

            block.m_stringOffsets = reader.readIntArray(stringCount);
            if (styleOffsetCount != 0)
            {
                block.m_styleOffsets = reader.readIntArray(styleOffsetCount);
            }

            {
                int size = ((stylesOffset == 0) ? chunkSize : stylesOffset) - stringsOffset;
                if ((size % 4) != 0)
                {
                    throw new IOException("String data size is not multiple of 4 (" + size + ").");
                }

                block.m_strings = reader.readIntArray(size / 4);
            }

            if (stylesOffset != 0)
            {
                int size = (chunkSize - stylesOffset);
                if ((size % 4) != 0)
                {
                    throw new IOException("Style data size is not multiple of 4 (" + size + ").");
                }
                block.m_styles = reader.readIntArray(size / 4);
            }

            return(block);
        }
Esempio n. 24
0
        public static List <GameUnit> GetNearUnits(int chunkNumber, GameEntity ent, Vector3Int pos)
        {
            var neighbourPoints = new Vector3Int[4];

            neighbourPoints[0] = new Vector3Int(pos.x + 1, pos.y, pos.z);
            neighbourPoints[1] = new Vector3Int(pos.x - 1, pos.y, pos.z);
            neighbourPoints[2] = new Vector3Int(pos.x, pos.y + 1, pos.z);
            neighbourPoints[3] = new Vector3Int(pos.x, pos.y - 1, pos.z);

            var chunk = ChunkManager.GetChunkByNum(chunkNumber);

            return((from point in neighbourPoints
                    where chunk.IsMapPos(point)
                    let index = ChunkUtil.GetIndex(chunkNumber, point)
                                where index > 0 && ChunkUtil.IsEntity(chunkNumber, point)
                                select chunk.GetGameObjectByIndex(point) as GameUnit
                                into obj
                                where obj != null
                                select obj).ToList());
        }
Esempio n. 25
0
        public static GameEntity GetNearFriendUnit(int chunkNumber, GameEntity ent, Vector3Int pos)
        {
            var neighbourPoints = new Vector3Int[4];

            neighbourPoints[0] = new Vector3Int(pos.x + 1, pos.y, pos.z);
            neighbourPoints[1] = new Vector3Int(pos.x - 1, pos.y, pos.z);
            neighbourPoints[2] = new Vector3Int(pos.x, pos.y + 1, pos.z);
            neighbourPoints[3] = new Vector3Int(pos.x, pos.y - 1, pos.z);

            var chunk = ChunkManager.GetChunkByNum(chunkNumber);

            return((from point in neighbourPoints
                    where chunk.IsMapPos(point)
                    let index = ChunkUtil.GetIndex(chunkNumber, point)
                                where index > 0 && ChunkUtil.IsEntity(chunkNumber, point)
                                select chunk.GetGameObjectByIndex(point)
                                into obj
                                where obj != null
                                select obj.GetComponent <GameEntity>()).FirstOrDefault(entEnemy =>
                                                                                       Equals(entEnemy.Owner, ent.Owner)));
        }
Esempio n. 26
0
        static List <TilePosition> PresetPath(GameUnit unit, Vector3Int posTarget)
        {
            var indexes = unit.PathFindObj.FindPath(ChunkUtil.GetDovvner(unit.CurrentPos), posTarget);

            // Debug.Log("Path = " + indexes);
            if (indexes == null)
            {
                var chunk = ChunkManager.GetChunkByNum(unit.ChunkNumber);
                chunk.ReindexTiles();
                return(null);
            }

            var lastOrDefault = unit.MovingPath.LastOrDefault();

            if (lastOrDefault != null && lastOrDefault.Index == posTarget)
            {
                return(null);
            }


            //Removing first point in some case
            if (unit.MovingPath.Count > 0 &&
                unit.MovingPath[0].Index != unit.CurrentPos &&
                indexes.Contains(unit.MovingPath[0].Index))
            {
                indexes = indexes.Where(t => t != ChunkUtil.GetDovvner(unit.CurrentPos)).ToList();
            }

            CancelOrders(unit);

            var path = (from ind in indexes
                        let pos3D = Util.Get3DPosByIndex(ind.x, ind.y, ind.z + 1)
                                    select new TilePosition(pos3D, ind)).ToList();

            unit.IsMoving   = true;
            unit.MovingPath = path;

            return(path);
        }
Esempio n. 27
0
        public static void MoveToIndex(GameUnit unit, Vector3Int posTarget)
        {
            if (posTarget == Vector3Int.zero)
            {
                return;
            }
            var ent = ChunkManager.CurrentChunk.GetGameObjectByIndex(posTarget);
            var pos = posTarget;

            if (ent == null || !GroupUtil.IsGround(ent.Group))
            {
                pos = ChunkUtil.GetDovvner(pos);
            }
            var path = PresetPath(unit, pos);

            if (path == null)
            {
                return;
            }

            GameOrderManager.DoOrder(unit, path, OrderTypes.MoveOrder);
        }
Esempio n. 28
0
        static void HidingColor(GameEntity ent, GameEntity eUP)
        {
            var check = false;

            if (eUP == null)
            {
                var friends =
                    AI_Calculation.GetGoodNeighbors
                        (ChunkManager.CurrentChunk.ChunkNumber, ent.CurrentPos);

                foreach (var friend in friends)
                {
                    var fUP = ChunkManager.CurrentChunk.GetGameObjectByIndex
                                  (ChunkUtil.GetUpper(friend));
                    if (fUP != null && !IsHided(fUP))
                    {
                        check = true;
                    }
                }
            }
            else
            {
                check = !IsHided(eUP);
            }
            if (!check)
            {
                var spD = ent.GetComponent <SpriteRenderer>();
                spD.color = static_nonVisionColor;
            }
            else
            {
                Coloring.RecolorObject(ent, true);

                Coloring.RecolorObject(ChunkUtil.GetUpper(ent.CurrentPos), true);

                Coloring.RecolorObject(ChunkUtil.GetDovvner(ent.CurrentPos), true);
            }
        }
Esempio n. 29
0
        public static GameEntity GetNearEnemy(GameEntity ent, int radius)
        {
            var chunkNumber     = ent.ChunkNumber;
            var chunk           = ChunkManager.GetChunkByNum(chunkNumber);
            var pos             = ent.CurrentPos;
            var neighbourPoints = new Vector3Int[8];

            for (var i = 0; i < radius; i++)
            {
                neighbourPoints[0] = new Vector3Int(pos.x + i, pos.y, pos.z);
                neighbourPoints[1] = new Vector3Int(pos.x - i, pos.y, pos.z);
                neighbourPoints[2] = new Vector3Int(pos.x, pos.y + i, pos.z);
                neighbourPoints[3] = new Vector3Int(pos.x, pos.y - i, pos.z);

                neighbourPoints[4] = new Vector3Int(pos.x + i, pos.y + i, pos.z);
                neighbourPoints[5] = new Vector3Int(pos.x - i, pos.y - i, pos.z);
                neighbourPoints[6] = new Vector3Int(pos.x - i, pos.y + i, pos.z);
                neighbourPoints[7] = new Vector3Int(pos.x + i, pos.y - i, pos.z);


                var enemy = (from point in neighbourPoints
                             where chunk.IsMapPos(point)
                             let index = ChunkUtil.GetIndex(chunkNumber, point)
                                         where index > 0 && ChunkUtil.IsEntity(chunkNumber, point)
                                         select chunk.GetGameObjectByIndex(point)
                                         into obj
                                         where obj != null
                                         select obj.GetComponent <GameEntity>()).FirstOrDefault(entEnemy =>
                                                                                                !Equals(entEnemy.Owner, ent.Owner));

                if (enemy != null)
                {
                    return(enemy);
                }
            }
            return(null);
        }
Esempio n. 30
0
File: Chunk.cs Progetto: LX0/X_NET40
        public bool TryCacheInMemory(bool shouldCacheNextChunk)
        {
            lock (_cacheSyncObj)
            {
                if (!_chunkConfig.EnableCache || _isMemoryChunk || !_isCompleted || _memoryChunk != null)
                {
                    _cachingChunk = 0;
                    return(false);
                }

                try
                {
                    var chunkSize = (ulong)GetChunkSize(_chunkHeader);
                    if (!ChunkUtil.IsMemoryEnoughToCacheChunk(chunkSize, (uint)_chunkConfig.ChunkCacheMaxPercent))
                    {
                        return(false);
                    }
                    _memoryChunk = FromCompletedFile(_filename, _chunkManager, _chunkConfig, true);
                    if (shouldCacheNextChunk)
                    {
                        Task.Factory.StartNew(() => _chunkManager.TryCacheNextChunk(this));
                    }
                    return(true);
                }
                catch (OutOfMemoryException) { return(false); }
                catch (Exception ex)
                {
                    Log.Error(string.Format("Failed to cache completed chunk {0}", this), ex);
                    return(false);
                }
                finally
                {
                    _cachingChunk = 0;
                }
            }
        }