public override bool Prepare( Vector3I[] marks ) { if( Player.World == null ) PlayerOpException.ThrowNoWorld( Player ); if( !base.Prepare( marks ) ) return false; BlocksTotalEstimate = Bounds.Volume; Coords = Bounds.MinVertex; // remember dimensions and orientation CopyState copyInfo = new CopyState( marks[0], marks[1] ); for( int x = Bounds.XMin; x <= Bounds.XMax; x++ ) { for( int y = Bounds.YMin; y <= Bounds.YMax; y++ ) { for( int z = Bounds.ZMin; z <= Bounds.ZMax; z++ ) { copyInfo.Buffer[x - Bounds.XMin, y - Bounds.YMin, z - Bounds.ZMin] = Map.GetBlock( x, y, z ); } } } copyInfo.OriginWorld = Player.World.Name; copyInfo.CopyTime = DateTime.UtcNow; Player.SetCopyInformation( copyInfo ); Player.Message( "{0} blocks cut into slot #{1}. You can now &H/Paste", Bounds.Volume, Player.CopySlot + 1 ); Player.Message( "Origin at {0} {1}{2} corner.", (copyInfo.Orientation.X == 1 ? "bottom" : "top"), (copyInfo.Orientation.Y == 1 ? "south" : "north"), (copyInfo.Orientation.Z == 1 ? "east" : "west") ); Context |= BlockChangeContext.Cut; return true; }
public override bool Begin() { // remember dimensions and orientation CopyState copyInfo = new CopyState(Marks[0], Marks[1]); for (int x = Bounds.XMin; x <= Bounds.XMax; x++) { for (int y = Bounds.YMin; y <= Bounds.YMax; y++) { for (int z = Bounds.ZMin; z <= Bounds.ZMax; z++) { copyInfo.Blocks[x - Bounds.XMin, y - Bounds.YMin, z - Bounds.ZMin] = Map.GetBlock(x, y, z); } } } // ReSharper disable PossibleNullReferenceException copyInfo.OriginWorld = Player.World.Name; // ReSharper restore PossibleNullReferenceException copyInfo.CopyTime = DateTime.UtcNow; Player.SetCopyState(copyInfo); Player.Message("{0} blocks cut into slot #{1}. You can now &H/Paste", Bounds.Volume, Player.CopySlot + 1); Player.Message("Origin at {0} {1}{2} corner.", (copyInfo.Orientation.Z == 1 ? "bottom" : "top"), (copyInfo.Orientation.Y == 1 ? "south" : "north"), (copyInfo.Orientation.X == 1 ? "east" : "west")); return(base.Begin()); }
public override bool Begin() { // remember dimensions and orientation CopyState copyInfo = new CopyState( Marks[0], Marks[1] ); for( int x = Bounds.XMin; x <= Bounds.XMax; x++ ) { for( int y = Bounds.YMin; y <= Bounds.YMax; y++ ) { for( int z = Bounds.ZMin; z <= Bounds.ZMax; z++ ) { copyInfo.Blocks[x - Bounds.XMin, y - Bounds.YMin, z - Bounds.ZMin] = Map.GetBlock( x, y, z ); } } } // ReSharper disable PossibleNullReferenceException copyInfo.OriginWorld = Player.World.Name; // ReSharper restore PossibleNullReferenceException copyInfo.CopyTime = DateTime.UtcNow; Player.SetCopyState( copyInfo ); Player.Message( "{0} blocks cut into slot #{1}. You can now &H/Paste", Bounds.Volume, Player.CopySlot + 1 ); Player.Message( "Origin at {0} {1}{2} corner.", ( copyInfo.Orientation.Z == 1 ? "bottom" : "top" ), ( copyInfo.Orientation.Y == 1 ? "south" : "north" ), ( copyInfo.Orientation.X == 1 ? "east" : "west" ) ); return base.Begin(); }
/// <summary> Creates a copy of an existing CopyState object. Replaces the buffer. </summary> public CopyState([NotNull] CopyState original, [NotNull] Block[, ,] buffer) { if (original == null) { throw new ArgumentNullException(); } Buffer = buffer; Orientation = original.Orientation; Slot = original.Slot; OriginWorld = original.OriginWorld; CopyTime = original.CopyTime; }
public override bool Prepare(Vector3I[] marks) { if (marks == null) throw new ArgumentNullException("marks"); if (marks.Length < 2) throw new ArgumentException("At least two marks needed.", "marks"); // Make sure that we have something to paste CopyInfo = Player.GetCopyState(); if (CopyInfo == null) { Player.Message("Nothing to paste! Copy something first."); return false; } // Calculate the buffer orientation Vector3I delta = marks[1] - marks[0]; Vector3I orientation = new Vector3I { X = (delta.X == 0 ? CopyInfo.Orientation.X : Math.Sign(delta.X)), Y = (delta.Y == 0 ? CopyInfo.Orientation.Y : Math.Sign(delta.Y)), Z = (delta.Z == 0 ? CopyInfo.Orientation.Z : Math.Sign(delta.Z)) }; // Calculate the start/end coordinates for pasting marks[1] = marks[0] + new Vector3I(orientation.X * (CopyInfo.Bounds.Width - 1), orientation.Y * (CopyInfo.Bounds.Length - 1), orientation.Z * (CopyInfo.Bounds.Height - 1)); Bounds = new BoundingBox(marks[0], marks[1]); Marks = marks; // Warn if paste will be cut off if (Bounds.XMin < 0 || Bounds.XMax > Map.Width - 1) { Player.Message("Warning: Not enough room horizontally (X), paste cut off."); } if (Bounds.YMin < 0 || Bounds.YMax > Map.Length - 1) { Player.Message("Warning: Not enough room horizontally (Y), paste cut off."); } if (Bounds.ZMin < 0 || Bounds.ZMax > Map.Height - 1) { Player.Message("Warning: Not enough room vertically, paste cut off."); } // Clip bounds to the map, to avoid unnecessary iteration beyond the map boundaries Start = Bounds.MinVertex; Bounds = Bounds.GetIntersection(Map.Bounds); // Set everything up for pasting Brush = this; Coords = Bounds.MinVertex; StartTime = DateTime.UtcNow; Context = BlockChangeContext.Drawn | BlockChangeContext.Pasted; BlocksTotalEstimate = Bounds.Volume; return true; }
/// <summary> Duplicates the given CopyState. /// Note that this is a deep copy -- Blocks array and everything else is duplicated too. </summary> public CopyState([NotNull] CopyState original) { if (original == null) { throw new ArgumentNullException(); } Blocks = (Block[, , ])original.Blocks.Clone(); Bounds = new BoundingBox(original.Bounds); Orientation = original.Orientation; Slot = original.Slot; OriginWorld = original.OriginWorld; CopyTime = original.CopyTime; }
/// <summary> Duplicates the given CopyState, but does not copy the Blocks array. /// Updates Bounds to match the new buffer's size, but preserves original Orientation. </summary> public CopyState([NotNull] CopyState original, [NotNull] Block[, ,] buffer) { if (original == null) { throw new ArgumentNullException(); } Blocks = buffer; Bounds = new BoundingBox(original.Bounds.MinVertex, buffer.GetLength(0), buffer.GetLength(1), buffer.GetLength(2)); Orientation = original.Orientation; Slot = original.Slot; OriginWorld = original.OriginWorld; CopyTime = original.CopyTime; }
public override bool Prepare(Vector3I[] marks) { if (Player.World == null) { PlayerOpException.ThrowNoWorld(Player); } if (!base.Prepare(marks)) { return(false); } BlocksTotalEstimate = Bounds.Volume; Coords = Bounds.MinVertex; // remember dimensions and orientation CopyState copyInfo = new CopyState(marks[0], marks[1]); for (int x = Bounds.XMin; x <= Bounds.XMax; x++) { for (int y = Bounds.YMin; y <= Bounds.YMax; y++) { for (int z = Bounds.ZMin; z <= Bounds.ZMax; z++) { copyInfo.Buffer[x - Bounds.XMin, y - Bounds.YMin, z - Bounds.ZMin] = Map.GetBlock(x, y, z); } } } copyInfo.OriginWorld = Player.World.Name; copyInfo.CopyTime = DateTime.UtcNow; Player.SetCopyInformation(copyInfo); Player.Message("{0} blocks cut into slot #{1}. You can now &H/Paste", Bounds.Volume, Player.CopySlot + 1); Player.Message("Origin at {0} {1}{2} corner.", (copyInfo.Orientation.X == 1 ? "bottom" : "top"), (copyInfo.Orientation.Y == 1 ? "south" : "north"), (copyInfo.Orientation.Z == 1 ? "east" : "west")); Context |= BlockChangeContext.Cut; return(true); }
static void MirrorHandler( Player player, CommandReader cmd ) { CopyState originalInfo = player.GetCopyState(); if( originalInfo == null ) { player.MessageNow( "Nothing to flip! Copy something first." ); return; } // clone to avoid messing up any paste-in-progress CopyState info = new CopyState( originalInfo ); bool flipX = false, flipY = false, flipH = false; string axis; while( (axis = cmd.Next()) != null ) { foreach( char c in axis.ToLower() ) { if( c == 'x' ) flipX = true; if( c == 'y' ) flipY = true; if( c == 'z' ) flipH = true; } } if( !flipX && !flipY && !flipH ) { CdMirror.PrintUsage( player ); return; } Block block; if( flipX ) { int left = 0; int right = info.Bounds.Width - 1; while( left < right ) { for( int y = info.Bounds.Length - 1; y >= 0; y-- ) { for( int z = info.Bounds.Height - 1; z >= 0; z-- ) { block = info.Blocks[left, y, z]; info.Blocks[left, y, z] = info.Blocks[right, y, z]; info.Blocks[right, y, z] = block; } } left++; right--; } } if( flipY ) { int left = 0; int right = info.Bounds.Length - 1; while( left < right ) { for( int x = info.Bounds.Width - 1; x >= 0; x-- ) { for( int z = info.Bounds.Height - 1; z >= 0; z-- ) { block = info.Blocks[x, left, z]; info.Blocks[x, left, z] = info.Blocks[x, right, z]; info.Blocks[x, right, z] = block; } } left++; right--; } } if( flipH ) { int left = 0; int right = info.Bounds.Height - 1; while( left < right ) { for( int x = info.Bounds.Width - 1; x >= 0; x-- ) { for( int y = info.Bounds.Length - 1; y >= 0; y-- ) { block = info.Blocks[x, y, left]; info.Blocks[x, y, left] = info.Blocks[x, y, right]; info.Blocks[x, y, right] = block; } } left++; right--; } } if( flipX ) { if( flipY ) { if( flipH ) { player.Message( "Flipped copy along all axes." ); } else { player.Message( "Flipped copy along X (east/west) and Y (north/south) axes." ); } } else { if( flipH ) { player.Message( "Flipped copy along X (east/west) and Z (vertical) axes." ); } else { player.Message( "Flipped copy along X (east/west) axis." ); } } } else { if( flipY ) { if( flipH ) { player.Message( "Flipped copy along Y (north/south) and Z (vertical) axes." ); } else { player.Message( "Flipped copy along Y (north/south) axis." ); } } else { player.Message( "Flipped copy along Z (vertical) axis." ); } } player.SetCopyState( info ); }
static void CopyCallback( Player player, Vector3I[] marks, object tag ) { int sx = Math.Min( marks[0].X, marks[1].X ); int ex = Math.Max( marks[0].X, marks[1].X ); int sy = Math.Min( marks[0].Y, marks[1].Y ); int ey = Math.Max( marks[0].Y, marks[1].Y ); int sz = Math.Min( marks[0].Z, marks[1].Z ); int ez = Math.Max( marks[0].Z, marks[1].Z ); BoundingBox bounds = new BoundingBox( sx, sy, sz, ex, ey, ez ); int volume = bounds.Volume; if( !player.CanDraw( volume ) ) { player.MessageNow( "You are only allowed to run commands that affect up to {0} blocks. This one would affect {1} blocks.", player.Info.Rank.DrawLimit, volume ); return; } // remember dimensions and orientation CopyState copyInfo = new CopyState( marks[0], marks[1] ); Map map = player.WorldMap; World playerWorld = player.World; if( playerWorld == null ) PlayerOpException.ThrowNoWorld( player ); for( int x = sx; x <= ex; x++ ) { for( int y = sy; y <= ey; y++ ) { for( int z = sz; z <= ez; z++ ) { copyInfo.Blocks[x - sx, y - sy, z - sz] = map.GetBlock( x, y, z ); } } } copyInfo.OriginWorld = playerWorld.Name; copyInfo.CopyTime = DateTime.UtcNow; player.SetCopyState( copyInfo ); player.MessageNow( "{0} blocks copied into slot #{1}, origin at {2} corner. You can now &H/Paste", volume, player.CopySlot + 1, copyInfo.OriginCorner ); Logger.Log( LogType.UserActivity, "{0} copied {1} blocks from world {2} (between {3} and {4}).", player.Name, volume, playerWorld.Name, bounds.MinVertex, bounds.MaxVertex ); }
static void RotateHandler( Player player, CommandReader cmd ) { CopyState originalInfo = player.GetCopyState(); if( originalInfo == null ) { player.MessageNow( "Nothing to rotate! Copy something first." ); return; } int degrees; if( !cmd.NextInt( out degrees ) || (degrees != 90 && degrees != -90 && degrees != 180 && degrees != 270) ) { CdRotate.PrintUsage( player ); return; } string axisName = cmd.Next(); Axis axis = Axis.Z; if( axisName != null ) { switch( axisName.ToLower() ) { case "x": axis = Axis.X; break; case "y": axis = Axis.Y; break; case "z": case "h": axis = Axis.Z; break; default: CdRotate.PrintUsage( player ); return; } } // allocate the new buffer Block[, ,] oldBuffer = originalInfo.Blocks; Block[, ,] newBuffer; if( degrees == 180 ) { newBuffer = new Block[oldBuffer.GetLength( 0 ), oldBuffer.GetLength( 1 ), oldBuffer.GetLength( 2 )]; } else if( axis == Axis.X ) { newBuffer = new Block[oldBuffer.GetLength( 0 ), oldBuffer.GetLength( 2 ), oldBuffer.GetLength( 1 )]; } else if( axis == Axis.Y ) { newBuffer = new Block[oldBuffer.GetLength( 2 ), oldBuffer.GetLength( 1 ), oldBuffer.GetLength( 0 )]; } else { // axis == Axis.Z newBuffer = new Block[oldBuffer.GetLength( 1 ), oldBuffer.GetLength( 0 ), oldBuffer.GetLength( 2 )]; } // clone to avoid messing up any paste-in-progress CopyState info = new CopyState( originalInfo, newBuffer ); // construct the rotation matrix int[,] matrix = { {1,0,0}, {0,1,0}, {0,0,1} }; int a, b; switch( axis ) { case Axis.X: a = 1; b = 2; break; case Axis.Y: a = 0; b = 2; break; default: a = 0; b = 1; break; } switch( degrees ) { case 90: matrix[a, a] = 0; matrix[b, b] = 0; matrix[a, b] = -1; matrix[b, a] = 1; break; case 180: matrix[a, a] = -1; matrix[b, b] = -1; break; case -90: case 270: matrix[a, a] = 0; matrix[b, b] = 0; matrix[a, b] = 1; matrix[b, a] = -1; break; } // apply the rotation matrix for( int x = oldBuffer.GetLength( 0 ) - 1; x >= 0; x-- ) { for( int y = oldBuffer.GetLength( 1 ) - 1; y >= 0; y-- ) { for( int z = oldBuffer.GetLength( 2 ) - 1; z >= 0; z-- ) { int nx = (matrix[0, 0] < 0 ? oldBuffer.GetLength( 0 ) - 1 - x : (matrix[0, 0] > 0 ? x : 0)) + (matrix[0, 1] < 0 ? oldBuffer.GetLength( 1 ) - 1 - y : (matrix[0, 1] > 0 ? y : 0)) + (matrix[0, 2] < 0 ? oldBuffer.GetLength( 2 ) - 1 - z : (matrix[0, 2] > 0 ? z : 0)); int ny = (matrix[1, 0] < 0 ? oldBuffer.GetLength( 0 ) - 1 - x : (matrix[1, 0] > 0 ? x : 0)) + (matrix[1, 1] < 0 ? oldBuffer.GetLength( 1 ) - 1 - y : (matrix[1, 1] > 0 ? y : 0)) + (matrix[1, 2] < 0 ? oldBuffer.GetLength( 2 ) - 1 - z : (matrix[1, 2] > 0 ? z : 0)); int nz = (matrix[2, 0] < 0 ? oldBuffer.GetLength( 0 ) - 1 - x : (matrix[2, 0] > 0 ? x : 0)) + (matrix[2, 1] < 0 ? oldBuffer.GetLength( 1 ) - 1 - y : (matrix[2, 1] > 0 ? y : 0)) + (matrix[2, 2] < 0 ? oldBuffer.GetLength( 2 ) - 1 - z : (matrix[2, 2] > 0 ? z : 0)); newBuffer[nx, ny, nz] = oldBuffer[x, y, z]; } } } player.Message( "Rotated copy (slot {0}) by {1} degrees around {2} axis.", info.Slot + 1, degrees, axis ); player.SetCopyState( info ); }
public override bool Prepare(Vector3I[] marks) { if (marks == null) { throw new ArgumentNullException("marks"); } if (marks.Length < 2) { throw new ArgumentException("At least two marks needed.", "marks"); } // Make sure that we have something to paste CopyInfo = Player.GetCopyInformation(); if (CopyInfo == null) { Player.Message("Nothing to paste! Copy something first."); return(false); } // Calculate the buffer orientation Vector3I delta = marks[1] - marks[0]; Vector3I orientation = new Vector3I { X = (delta.X == 0 ? CopyInfo.Orientation.X : Math.Sign(delta.X)), Y = (delta.Y == 0 ? CopyInfo.Orientation.Y : Math.Sign(delta.Y)), Z = (delta.Z == 0 ? CopyInfo.Orientation.Z : Math.Sign(delta.Z)) }; // Calculate the start/end coordinates for pasting marks[1] = marks[0] + new Vector3I(orientation.X * (CopyInfo.Dimensions.X - 1), orientation.Y * (CopyInfo.Dimensions.Y - 1), orientation.Z * (CopyInfo.Dimensions.Z - 1)); Bounds = new BoundingBox(marks[0], marks[1]); Marks = marks; // Warn if paste will be cut off if (Bounds.XMin < 0 || Bounds.XMax > Map.Width - 1) { Player.Message("Warning: Not enough room horizontally (X), paste cut off."); } if (Bounds.YMin < 0 || Bounds.YMax > Map.Length - 1) { Player.Message("Warning: Not enough room horizontally (Y), paste cut off."); } if (Bounds.ZMin < 0 || Bounds.ZMax > Map.Height - 1) { Player.Message("Warning: Not enough room vertically, paste cut off."); } // Clip bounds to the map, to avoid unnecessary iteration beyond the map boundaries Start = Bounds.MinVertex; Bounds = Bounds.GetIntersection(Map.Bounds); // Set everything up for pasting Brush = this; Coords = Bounds.MinVertex; StartTime = DateTime.UtcNow; Context = BlockChangeContext.Drawn | BlockChangeContext.Pasted; BlocksTotalEstimate = Bounds.Volume; return(true); }