Example #1
0
        void GenerateGrid()
        {
            ClearGrid();

            if (FreezeObjectsArea.Instances.Count != 0)
            {
                //init grid
                {
                    Bounds bounds = Bounds.Cleared;
                    foreach (FreezeObjectsArea area in FreezeObjectsArea.Instances)
                    {
                        bounds.Add(area.MapBounds);
                    }
                    if (bounds.GetSize().X < 1)
                    {
                        bounds.Expand(new Vec3(1, 0, 0));
                    }
                    if (bounds.GetSize().Y < 1)
                    {
                        bounds.Expand(new Vec3(0, 1, 0));
                    }
                    if (bounds.GetSize().Z < 1)
                    {
                        bounds.Expand(new Vec3(0, 0, 1));
                    }

                    gridStartPosition = bounds.Minimum;

                    gridCellSize = bounds.GetSize() / cellCount.ToVec3();
                    for (int n = 0; n < 3; n++)
                    {
                        if (gridCellSize[n] < cellMinSize[n])
                        {
                            gridCellSize[n] = cellMinSize[n];
                        }
                    }
                    gridCellSizeInv = 1.0f / gridCellSize;
                    grid            = new GridItem[cellCount.X, cellCount.Y, cellCount.Z];
                }

                //fill areas

                GridAreasIndexByListOfAreasKey        comparer = new GridAreasIndexByListOfAreasKey();
                Dictionary <FreezeObjectsArea[], int> gridAreasIndexByListOfAreas = new Dictionary <FreezeObjectsArea[], int>(comparer);
                List <FreezeObjectsArea> gridAreasList = new List <FreezeObjectsArea>();

                foreach (FreezeObjectsArea area in FreezeObjectsArea.Instances)
                {
                    Box    box    = area.GetBox();
                    Bounds bounds = box.ToBounds();

                    Vec3I startIndex = GetCellIndexWithoutClamp(bounds.Minimum);
                    Vec3I endIndex   = GetCellIndexWithoutClamp(bounds.Maximum);
                    ClampCellIndex(ref startIndex);
                    ClampCellIndex(ref endIndex);

                    for (int z = startIndex.Z; z <= endIndex.Z; z++)
                    {
                        for (int y = startIndex.Y; y <= endIndex.Y; y++)
                        {
                            for (int x = startIndex.X; x <= endIndex.X; x++)
                            {
                                Vec3I  cellIndex  = new Vec3I(x, y, z);
                                Bounds cellBounds = GetCellBounds(cellIndex);
                                if (box.IsIntersectsBounds(cellBounds))
                                {
                                    GridItem item = grid[cellIndex.X, cellIndex.Y, cellIndex.Z];

                                    FreezeObjectsArea[] array = new FreezeObjectsArea[item.count + 1];
                                    for (int n = 0; n < item.count; n++)
                                    {
                                        array[n] = gridAreas[item.startIndex + n];
                                    }
                                    array[array.Length - 1] = area;

                                    int gridAreasIndex;
                                    if (!gridAreasIndexByListOfAreas.TryGetValue(array, out gridAreasIndex))
                                    {
                                        gridAreasIndex = gridAreasList.Count;

                                        gridAreasIndexByListOfAreas.Add(array, gridAreasIndex);
                                        gridAreasList.AddRange(array);
                                    }

                                    item.startIndex = gridAreasIndex;
                                    item.count      = array.Length;
                                    grid[cellIndex.X, cellIndex.Y, cellIndex.Z] = item;
                                }
                            }
                        }
                    }

                    gridAreas = gridAreasList.ToArray();
                }
            }
        }
Example #2
0
        bool CalculateFrozenState(MapObject obj)
        {
            if (!IsReallyEnabled())
            {
                return(false);
            }
            if (obj._FreezeObjectsManagerNeverFreeze)
            {
                return(false);
            }

            //check by camera distance
            {
                Camera camera = RendererWorld.Instance.DefaultCamera;

                if (unfreezeByCameraZDistance != 0)
                {
                    float length2 = (camera.Position.ToVec2() - obj.Position.ToVec2()).Length();
                    if (length2 < unfreezeByCameraDistance)
                    {
                        return(false);
                    }
                    float lengthZ = Math.Abs(camera.Position.Z - obj.Position.Z);
                    if (lengthZ < unfreezeByCameraZDistance)
                    {
                        return(false);
                    }
                }
                else
                {
                    float length = (camera.Position - obj.Position).Length();
                    if (length < unfreezeByCameraDistance)
                    {
                        return(false);
                    }
                }
            }

            //check by areas
            if (grid != null)
            {
                Vec3  indexF = (obj.Position - gridStartPosition) * gridCellSizeInv;
                Vec3I index  = indexF.ToVec3I();
                if (index.X >= 0 && index.Y >= 0 && index.Z >= 0 && index.X < cellCount.X && index.Y < cellCount.Y && index.Z < cellCount.Z)
                {
                    GridItem item = grid[index.X, index.Y, index.Z];

                    for (int n = 0; n < item.count; n++)
                    {
                        FreezeObjectsArea area = gridAreas[n + item.startIndex];

                        bool unfreezing;
                        area._UpdateLastCheckState(out unfreezing);
                        if (unfreezing)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }