Exemplo n.º 1
0
            public Entry Lookup(VectorInt3 pos)
            {
                int mapX = pos.X / _mapGranularity;
                int mapY = pos.Y / _mapGranularity;

                if (mapX < 0 || mapY < 0 || mapX >= _map.GetLength(2) || mapY >= _map.GetLength(1))
                {
                    return(null);
                }

                List <Entry> candidates = _map[pos.Z, mapY, mapX];

                foreach (Entry candidate in candidates)
                {
                    if ((pos.X >= candidate.Pos.X) && (pos.Y >= candidate.Pos.Y))
                    {
                        VectorInt2 size = candidate.Texture.To - candidate.Texture.From;
                        if ((pos.X < (candidate.Pos.X + size.X)) && (pos.Y < (candidate.Pos.Y + size.Y)))
                        {
                            return(candidate);
                        }
                    }
                }
                return(null);
            }
Exemplo n.º 2
0
 /// <summary>
 /// Tokenizes a VectorInt3.
 /// </summary>
 /// <param name="vector">The VectorInt3 instance to tokenize.</param>
 /// <param name="prefix">Prefix for token names. If the prefix is "Position" then the tokens "Position", "PositionX", "PositionY", and "PositionZ" will be created.</param>
 protected void TokenizeVectorInt3(VectorInt3 vector, string prefix)
 {
     this.tokens[prefix]       = $"X:{vector.x} Y:{vector.y} Z:{vector.z}";
     this.tokens[prefix + 'X'] = vector.x.ToString();
     this.tokens[prefix + 'Y'] = vector.y.ToString();
     this.tokens[prefix + 'Z'] = vector.z.ToString();
 }
Exemplo n.º 3
0
 public void PushRoomsMoved(List <Room> rooms, VectorInt3 delta)
 {
     if (delta != VectorInt3.Zero)
     {
         Push(new MoveRoomsUndoInstance(this, rooms, delta));
     }
 }
Exemplo n.º 4
0
 private VectorInt3?AllocateTexture(RenderingTexture texture)
 {
     if (texture.To.X < texture.From.X || texture.To.Y < texture.From.Y)
     {
         throw new ArgumentOutOfRangeException(); // Avoid totally currupted texture allocator state.
     }
     for (int i = 0; i < Pages.Length; ++i)
     {
         VectorInt2?allocatedPos = Pages[i].Packer.TryAdd(VectorInt2.Max((texture.To - texture.From) + new VectorInt2(2), VectorInt2.One));
         if (allocatedPos != null)
         {
             VectorInt3 pos = new VectorInt3(allocatedPos.Value.X, allocatedPos.Value.Y, i);
             if (texture.To.X == texture.From.X || texture.To.Y == texture.From.Y)
             {
                 UploadTexture(ImageC.CreateNew(1, 1), pos); // Attempt to handle this situation
             }
             else
             {
                 UploadTexture(texture, pos);
             }
             AvailableTextures.Add(texture, pos + new VectorInt3(1, 1, 0));
             return(pos + new VectorInt3(1, 1, 0));
         }
     }
     return(null);
 }
Exemplo n.º 5
0
    public static VectorInt3 Loop(VectorInt3 val, VectorInt3 min, VectorInt3 max)
    {
        if (val.x > max.x)
        {
            val.x = min.x;
        }
        if (val.y > max.y)
        {
            val.y = min.y;
        }
        if (val.z > max.z)
        {
            val.z = min.z;
        }

        if (val.x < min.x)
        {
            val.x = max.x;
        }
        if (val.y < min.y)
        {
            val.y = max.y;
        }
        if (val.z < min.z)
        {
            val.z = max.z;
        }

        return(val);
    }
Exemplo n.º 6
0
    private State getOut()
    {
        StateWithEventMap swem = new StateWithEventMap();

        swem.addAction("enter",
                       delegate(FSMEvent evt){
            GameObject go    = (GameObject)(evt.obj);
            VectorInt3 point = this._buoy.position2cell(go.transform.position);
            if (point != this._buoy.point)
            {
                this._buoy.point = point;
            }
            return("in");
        });

        swem.addAction("stay",
                       delegate(FSMEvent evt) {
            GameObject go    = (GameObject)(evt.obj);
            VectorInt3 point = this._buoy.position2cell(go.transform.position);
            if (point != this._buoy.point)
            {
                this._buoy.point = point;
            }
            ;
            return("in");
        });



        return(swem);
    }
Exemplo n.º 7
0
        void BlockSearch(IStructure str)
        {
            //in my test case, the y is off by 128
            VectorInt3 minWorld = new VectorInt3(
                str.MinPos.x,
                str.MinPos.y + 128,
                str.MinPos.z);

            VectorInt3 maxWorld = new VectorInt3(
                str.MaxPos.x,
                str.MaxPos.y + 128,
                str.MaxPos.z);


            for (int x = minWorld.x; x <= maxWorld.x; x++)
            {
                for (int y = minWorld.y; y <= maxWorld.y; y++)
                {
                    for (int z = minWorld.z; z <= maxWorld.z; z++)
                    {
                        IBlock bl = str.GetBlock(x, y, z);

                        mAPI.Log("Block at " + x + ", " + y + ", " + z +
                                 " custom name: " + bl.CustomName +
                                 ", Damage: " + bl.GetDamage() +
                                 ", HitPoints: " + bl.GetHitPoints());
                    }
                }
            }
        }
Exemplo n.º 8
0
        private bool UpdateRoomPosition(Vector2 newRoomPos, Room roomReference, bool moveRooms)
        {
            VectorInt2 newRoomPosInt = VectorInt2.FromRounded(newRoomPos);
            VectorInt2 roomMovement  = newRoomPosInt - roomReference.SectorPos;

            if (roomMovement != new VectorInt2())
            {
                if (EditorActions.CheckForLockedRooms(this, _roomsToMove))
                {
                    _roomMouseClicked = null;
                }
                else
                {
                    var delta = new VectorInt3(roomMovement.X, 0, roomMovement.Y);

                    if (roomMovement.X != 0 || roomMovement.Y != 0)
                    {
                        _overallDelta += delta;
                        if (moveRooms)
                        {
                            EditorActions.MoveRooms(delta, _roomsToMove, true);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 9
0
 public Cube getCube(VectorInt3 position)
 {
     if (_cubes.ContainsKey(position))
     {
         return(_cubes [position]);
     }
     return(null);
 }
Exemplo n.º 10
0
 public TeleporterData(IStructure structure, VectorInt3 pos) : base(structure, pos)
 {
     _teleporter = GetDevice() as ITeleporter;
     if (_teleporter != null)
     {
         _targetData = _teleporter.TargetData;
     }
 }
Exemplo n.º 11
0
        public static ulong CompressUvw(VectorInt3 position, Vector2 textureScaling, Vector2 uv, uint highestBits = 0)
        {
            uint blendMode2 = Math.Min(highestBits, 15);
            uint x          = (uint)((position.X + uv.X) * textureScaling.X);
            uint y          = (uint)((position.Y + uv.Y) * textureScaling.Y);

            return(x | ((ulong)y << 24) | ((ulong)position.Z << 48) | ((ulong)blendMode2 << 60));
        }
Exemplo n.º 12
0
 public void Write(VectorInt3 value)
 {
     if (_ptr > _endPtrWithSafetyMarging)
     {
         Flush();
     }
     WriteUnchecked(value.X); WriteUnchecked(value.Y); WriteUnchecked(value.Z);
 }
Exemplo n.º 13
0
        public override unsafe void RenderSprites(RenderingTextureAllocator textureAllocator, bool linearFilter, params Sprite[] sprites)
        {
            Vector2 textureScaling = new Vector2(16777216.0f) / new Vector2(textureAllocator.Size.X, textureAllocator.Size.Y);

            // Build vertex buffer
            int vertexCount = sprites.Length * 6;
            int bufferSize  = vertexCount * (sizeof(Vector2) + sizeof(ulong));

            fixed(byte *data = new byte[bufferSize])
            {
                Vector2 *positions = (Vector2 *)(data);
                ulong *  uvws      = (ulong *)(data + vertexCount * sizeof(Vector2));

                // Setup vertices
                int count = sprites.Length;

                for (int i = 0; i < count; ++i)
                {
                    Sprite     sprite  = sprites[i];
                    VectorInt3 texPos  = textureAllocator.Get(sprite.Texture);
                    VectorInt2 texSize = sprite.Texture.To - sprite.Texture.From;

                    positions[i * 6 + 0] = sprite.Pos00;
                    positions[i * 6 + 2] = positions[i * 6 + 3] = sprite.Pos10;
                    positions[i * 6 + 1] = positions[i * 6 + 4] = sprite.Pos01;
                    positions[i * 6 + 5] = sprite.Pos11;
                    uvws[i * 6 + 1]      = uvws[i * 6 + 4] = Dx11RenderingDevice.CompressUvw(texPos, textureScaling, new Vector2(0.5f, 0.5f));
                    uvws[i * 6 + 5]      = Dx11RenderingDevice.CompressUvw(texPos, textureScaling, new Vector2(texSize.X - 0.5f, 0.5f));
                    uvws[i * 6 + 0]      = Dx11RenderingDevice.CompressUvw(texPos, textureScaling, new Vector2(0.5f, texSize.Y - 0.5f));
                    uvws[i * 6 + 2]      = uvws[i * 6 + 3] = Dx11RenderingDevice.CompressUvw(texPos, textureScaling, new Vector2(texSize.X - 0.5f, texSize.Y - 0.5f));
                }

                // Create GPU resources
                using (var VertexBuffer = new Buffer(Device.Device, new IntPtr(data),
                                                     new BufferDescription(bufferSize, ResourceUsage.Immutable, BindFlags.VertexBuffer,
                                                                           CpuAccessFlags.None, ResourceOptionFlags.None, 0)))
                {
                    var VertexBufferBindings = new VertexBufferBinding[] {
                        new VertexBufferBinding(VertexBuffer, sizeof(Vector2), (int)((byte *)positions - data)),
                        new VertexBufferBinding(VertexBuffer, sizeof(ulong), (int)((byte *)uvws - data))
                    };

                    // Render
                    Bind();
                    Device.SpriteShader.Apply(Device.Context);
                    Device.Context.PixelShader.SetSampler(0, linearFilter ? Device.SamplerDefault : Device.SamplerRoundToNearest);
                    Device.Context.PixelShader.SetShaderResources(0, ((Dx11RenderingTextureAllocator)(textureAllocator)).TextureView);
                    Device.Context.InputAssembler.SetVertexBuffers(0, VertexBufferBindings);
                    Device.Context.OutputMerger.SetDepthStencilState(Device.DepthStencilNoZBuffer);

                    // Render
                    Device.Context.Draw(vertexCount, 0);

                    // Reset state
                    Device.Context.OutputMerger.SetDepthStencilState(Device.DepthStencilDefault);
                }
            }
        }
Exemplo n.º 14
0
        public DeviceLock(IPlayfield playfield, IStructure structure, VectorInt3 position)
        {
            var witherror = false;

            try
            {
                if (ScriptExecQueue.Iteration % EmpyrionScripting.Configuration.Current.DeviceLockOnlyAllowedEveryXCycles != 0)
                {
                    return;
                }

                if (playfield.IsStructureDeviceLocked(structure.Id, position))
                {
                    return;
                }

                var lockkey = $"{structure.Id}#{position.x}#{position.y}#{position.z}";

                var e = new AutoResetEvent(false);
                playfield.LockStructureDevice(structure.Id, position, true, (s) =>
                {
                    if (witherror)
                    {
                        Log($"Lock:Callback:Error {playfield.Name} {structure.Id} {position}", LogLevel.Debug);
                        playfield.LockStructureDevice(structure.Id, position, false, null);
                    }
                    else
                    {
                        Success = s;
                        e.Set();
                    }
                });
                witherror = !e.WaitOne(10000);
                if (witherror)
                {
                    Log($"Lock:WaitOne:Error {playfield.Name} {structure.Id} {position}", LogLevel.Debug);
                }

                if (Success)
                {
                    unlockAction = () =>
                    {
                        e.Reset();
                        playfield.LockStructureDevice(structure.Id, position, false, (s) => e.Set());
                        if (!e.WaitOne(10000))
                        {
                            Log($"Unlock:Timeout {playfield.Name} {structure.Id} {position}", LogLevel.Debug);
                        }
                    }
                }
                ;
            }
            catch (Exception error)
            {
                witherror = true;
                throw new Exception($"DeviceLock:failed on Playfield:{playfield?.Name} at Id:{structure.Id} on {position} with: {error}");
            }
        }
Exemplo n.º 15
0
        public static void LockDeviceHelper(TextWriter output, object rootObject, HelperOptions options, dynamic context, object[] arguments)
        {
            if (arguments.Length != 2 && arguments.Length != 4)
            {
                throw new HandlebarsException("{{lockdevice structure device|x y z}} helper must have two or four argument: @root (structure) (device)|(x) (y) (z)");
            }

            var        root = rootObject as IScriptRootData;
            var        S    = arguments[0] as IStructureData;
            VectorInt3 position;

            if (!root.IsElevatedScript)
            {
                throw new HandlebarsException("{{lockdevice}} only allowed in elevated scripts");
            }

            if (!root.DeviceLockAllowed)
            {
                Log($"LockDevice: NoLockAllowed({root.ScriptId}): {root.CycleCounter} % {EmpyrionScripting.Configuration.Current.DeviceLockOnlyAllowedEveryXCycles}", LogLevel.Debug);
                return;
            }

            if (arguments.Length == 2)
            {
                var block = arguments[1] as BlockData;
                position = block?.Position ?? new VectorInt3();
            }
            else
            {
                int.TryParse(arguments[1].ToString(), out var x);
                int.TryParse(arguments[2].ToString(), out var y);
                int.TryParse(arguments[3].ToString(), out var z);

                position = new VectorInt3(x, y, z);
            }

            try
            {
                using (var locked = CreateDeviceLock(root, root.GetCurrentPlayfield(), S.E?.S.GetCurrent(), position))
                {
                    if (locked.Success)
                    {
                        options.Template(output, context as object);
                    }
                    else
                    {
                        options.Inverse(output, context as object);
                    }
                }
            }
            catch (Exception error)
            {
                if (!CsScriptFunctions.FunctionNeedsMainThread(error, root))
                {
                    output.Write("{{lockdevice}} error " + EmpyrionScripting.ErrorFilter(error));
                }
            }
        }
Exemplo n.º 16
0
 // Use this for initialization
 void Awake()
 {
     shipArray = new Block[gridSize, gridSize, gridSize];
     ResetArray();
     shipObjects        = new GameObject[gridSize, gridSize, gridSize];
     cursorPos          = new VectorInt3(gridSize / 2, gridSize / 2, gridSize / 2);
     grid.localPosition = new Vector3(0f, -(gridSize / 2), 0.0f);
     MoveCursor(new VectorInt3(0, 0, 0));
 }
Exemplo n.º 17
0
    public void setup(VectorInt3 position, Color color, float unit)
    {
        Debug.Log(unit);
        position_ = position;
        this.transform.localPosition = new Vector3(position.x * unit, position.y * unit, position.z * unit);

        color_ = color;
        this.transform.localScale = Vector3.one * unit;
    }
Exemplo n.º 18
0
        static float    VecDistance(VectorInt3 a, VectorInt3 b)
        {
            int x = a.x - b.x;
            int y = a.y - b.y;
            int z = a.z - b.z;

            int lenSQ = (x * x) + (y * y) + (z * z);

            return((float)Math.Sqrt(lenSQ));
        }
Exemplo n.º 19
0
    public void setup(VectorInt3 offset, float unit)
    {
        unit_ = unit;
        //Debug.Log ("offset" + offset);
        this.transform.localPosition = new Vector3(-((float)(offset.x) - 1f) / 2f * unit_,
                                                   -((float)(offset.y) - 1f) / 2f * unit_,
                                                   -((float)(offset.z) - 1f) / 2f * unit_);

        //this.transform.localPosition = new Vector3()
    }
Exemplo n.º 20
0
    public override void brush(VectorInt3 point, Building building)
    {
        //	Debug.Log ("Add");
        Debug.Log("!!" + point);
        Cube cube = building.getCube(point);

        if (cube == null)
        {
            building.addCube(point, Color.red);
        }
    }
Exemplo n.º 21
0
    private void RotateCursor(VectorInt3 direction)
    {
        /*   cursorRotation += direction;
         * cursorRotation = VectorInt3.Loop( cursorRotation, new VectorInt3( 0, 0, 0 ), new VectorInt3( 3, 3, 3 ) );
         * cursor.localEulerAngles = new Vector3( cursorRotation.y * 90.0f, cursorRotation.x * 90.0f, 0.0f );*/
        cursor.Rotate(Vector3.up, 90f * direction.x, Space.World);
        cursor.Rotate(Vector3.right, 90f * direction.y, Space.World);
        Vector3 angles = cursor.localEulerAngles;

        cursorRotation = new VectorInt3(Mathf.RoundToInt(angles.x), Mathf.RoundToInt(angles.y), Mathf.RoundToInt(angles.z));
    }
Exemplo n.º 22
0
        public RenderingTextureAllocator(RenderingDevice device, Description description)
        {
            logger.Info("Rendering texture allocated of size \"" + description.Size + "\".");

            Pages = new TexturePage[description.Size.Z];
            for (int i = 0; i < description.Size.Z; ++i)
            {
                Pages[i] = new TexturePage(new VectorInt2(description.Size.X, description.Size.Y));
            }
            Size = description.Size;
        }
Exemplo n.º 23
0
        public BlockData(IStructure structure, VectorInt3 pos)
        {
            _structure = structure;
            _block     = structure?.GetBlock(pos);
            _device    = structure?.GetDevice <IDevice>(pos);
            Position   = pos;

            if (_device is IContainer c)
            {
                Device = new ContainerData(c);
            }
        }
Exemplo n.º 24
0
        public MoveRoomsUndoInstance(EditorUndoManager parent, List <Room> rooms, VectorInt3 delta) : base(parent, null)
        {
            Delta = -delta;
            Rooms = rooms;
            Rooms.ForEach(room => Sizes.Add(room, room.SectorSize));

            Valid = () => Rooms != null && Rooms.All(room => room != null && room.ExistsInLevel && !room.Locked) &&
                    Rooms.All(room => !Parent.Editor.Level.GetConnectedRooms(room).Except(Rooms).Any()) &&
                    Rooms.All(room => Sizes.ContainsKey(room) && room.SectorSize == Sizes[room]);
            UndoAction   = () => EditorActions.MoveRooms(Delta, Rooms, true);
            RedoInstance = () => new MoveRoomsUndoInstance(Parent, Rooms, Delta);
        }
Exemplo n.º 25
0
    public override void brush(VectorInt3 point, Building building)
    {
        //	Debug.Log ("Add");
        Cube cube = building.getCube(point);

        Debug.Log("@@@@" + point);
        if (cube != null)
        {
            Debug.Log("@@@@");
            building.removeCube(point);
        }
    }
Exemplo n.º 26
0
    public VectorInt3 position2cell(Vector3 position)
    {
        Vector3    o    = new Vector3((size_.x * unit_), (size_.y * unit_), (size_.z * unit_)) / 2.0f + position - this.gameObject.transform.parent.position;
        VectorInt3 cell = new VectorInt3(Mathf.FloorToInt(o.x / unit_), Mathf.FloorToInt(o.y / unit_), Mathf.FloorToInt(o.z / unit_));

        cell.x = Mathf.Max(cell.x, 0);
        cell.y = Mathf.Max(cell.y, 0);
        cell.z = Mathf.Max(cell.z, 0);
        cell.x = Mathf.Min(cell.x, 5 - 1);
        cell.y = Mathf.Min(cell.y, 3 - 1);
        cell.z = Mathf.Min(cell.z, 4 - 1);
        return(cell);
    }
Exemplo n.º 27
0
    private VectorInt3 position2cell(Vector3 position)
    {
        Vector3    o    = _box.size / 2 + position - this.gameObject.transform.position;
        VectorInt3 cell = new VectorInt3(Mathf.FloorToInt(o.x / _unit), Mathf.FloorToInt(o.y / _unit), Mathf.FloorToInt(o.z / _unit));

        cell.x = Mathf.Max(cell.x, 0);
        cell.y = Mathf.Max(cell.y, 0);
        cell.z = Mathf.Max(cell.z, 0);
        cell.x = Mathf.Min(cell.x, 5 - 1);
        cell.y = Mathf.Min(cell.y, 3 - 1);
        cell.z = Mathf.Min(cell.z, 4 - 1);
        return(cell);
    }
Exemplo n.º 28
0
 private VectorInt3 UpdateQuickAccess(ref RenderingTexture texture, VectorInt3 result)
 {
     if (LastUsedTextureIndex == 0)
     {
         QuickAccessTexture1  = new KeyValuePair <RenderingTexture, VectorInt3>(texture, result);
         LastUsedTextureIndex = 1;
     }
     else
     {
         QuickAccessTexture0  = new KeyValuePair <RenderingTexture, VectorInt3>(texture, result);
         LastUsedTextureIndex = 0;
     }
     return(result);
 }
Exemplo n.º 29
0
    public void removeCube(VectorInt3 point)
    {
        Debug.Log(point);

        //_cubes.
        Cube cube = null;

        if (_cubes.ContainsKey(point))
        {
            cube = _cubes [point];
            _cubes.Remove(point);
            cube.gameObject.SetActive(false);
        }
    }
Exemplo n.º 30
0
    private State getIn()
    {
        StateWithEventMap swem = new StateWithEventMap();

        swem.onStart += delegate {
            if (drawing_ && brush_)
            {
                brush_.brush(this._buoy.point, this._buinding);
            }

            _buoy.open();
        };
        swem.onOver += delegate {
            _buoy.close();
        };

        swem.addAction("drawing", delegate(FSMEvent evt) {
            if (drawing_ && this.brush_ != null)
            {
                this.brush_.brush(this._buoy.point, this._buinding);
            }
        });


        swem.addAction("brush", delegate(FSMEvent evt) {
//			Debug.Log("!!!!!!DSFDSF");
            if (drawing_ && this.brush_ != null)
            {
                this.brush_.brush(this._buoy.point, this._buinding);
            }
        });
        swem.addAction("exit", "out");
        swem.addAction("stay", delegate(FSMEvent evt) {
//			Debug.Log(drawing_+ this.brush_.gameObject.name);
            GameObject go    = (GameObject)(evt.obj);
            VectorInt3 point = this._buoy.position2cell(go.transform.position);
            if (point != this._buoy.point)
            {
                this._buoy.point = point;
                if (drawing_ && this.brush_ != null)
                {
                    this.brush_.brush(point, this._buinding);
                }
            }
        });


        return(swem);
    }