Skip to content

Commit

Permalink
Added new commands /Plane and /PlaneW (for drawing planes)
Browse files Browse the repository at this point in the history
Thanks LaoTszy for help on the maths in /Plane =)
  • Loading branch information
Jonty800 committed Aug 9, 2013
1 parent 1e947a3 commit a0061f0
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 30 deletions.
6 changes: 3 additions & 3 deletions ConfigGUI/AddWorldPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ private void AsyncGen( object sender, DoWorkEventArgs e ) {
GC.Collect( GC.MaxGeneration, GCCollectionMode.Forced );
Map generatedMap;
if ( tab == Tabs.Generator ) {
MapGenerator gen = new MapGenerator( generatorArgs );
MapGeneratorOld gen = new MapGeneratorOld( generatorArgs );
gen.ProgressChanged +=
( progressSender, progressArgs ) =>
bwGenerator.ReportProgress( progressArgs.ProgressPercentage, progressArgs.UserState );
generatedMap = gen.Generate();
} else {
generatedMap = MapGenerator.GenerateFlatgrass( Convert.ToInt32( nFlatgrassDimX.Value ),
generatedMap = MapGeneratorOld.GenerateFlatgrass( Convert.ToInt32( nFlatgrassDimX.Value ),
Convert.ToInt32( nFlatgrassDimY.Value ),
Convert.ToInt32( nFlatgrassDimZ.Value ) );
}
Expand Down Expand Up @@ -865,7 +865,7 @@ private void bSaveTemplate_Click( object sender, EventArgs e ) {
}

private void cTemplates_SelectedIndexChanged( object sender, EventArgs e ) {
generatorArgs = MapGenerator.MakeTemplate( ( MapGenTemplate )cTemplates.SelectedIndex );
generatorArgs = MapGeneratorOld.MakeTemplate( ( MapGenTemplate )cTemplates.SelectedIndex );
LoadGeneratorArgs();
bGenerate.PerformClick();
}
Expand Down
8 changes: 7 additions & 1 deletion ConfigGUI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions fCraft/Commands/BuildingCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ internal static void Init() {
CommandManager.RegisterCommand( CdDraw2D );
CommandManager.RegisterCommand( CdSetFont );
CommandManager.RegisterCommand( CdDrawImage );

CommandManager.RegisterCommand( CdPlane );
CommandManager.RegisterCommand( CdPlaneW );
}

#endregion Init
Expand Down Expand Up @@ -1103,6 +1106,34 @@ private static void TriangleHandler( Player player, Command cmd ) {
DrawOperationBegin( player, cmd, new TriangleDrawOperation( player ) );
}

private static readonly CommandDescriptor CdPlane = new CommandDescriptor {
Name = "Plane",
Aliases = new[] { "Quad" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a plane between three points.",
Handler = PlaneHandler
};

private static void PlaneHandler( Player player, Command cmd ) {
DrawOperationBegin( player, cmd, new PlaneDrawOperation( player ) );
}

private static readonly CommandDescriptor CdPlaneW = new CommandDescriptor {
Name = "PlaneW",
Aliases = new[] { "QuadW" },
Category = CommandCategory.Building,
Permissions = new[] { Permission.Draw },
RepeatableSelection = true,
Help = "Draws a wireframe plane between four points.",
Handler = PlaneWHandler
};

private static void PlaneWHandler( Player player, Command cmd ) {
DrawOperationBegin( player, cmd, new PlaneWireframeDrawOperation( player ) );
}

private static readonly CommandDescriptor CdTorus = new CommandDescriptor {
Name = "Torus",
Aliases = new[] { "donut", "bagel" },
Expand Down
2 changes: 1 addition & 1 deletion fCraft/Commands/Command Handlers/FeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void AddMessages() {
Messages.Clear();
}
Messages = new List<string>();
Messages.Add( "Server Time: " + DateTime.Now.ToString( "HH:mm:ss tt" ) );
Messages.Add( "Server Time: " + DateTime.UtcNow.ToString( "HH:mm:ss tt" ) );
Messages.Add( "Welcome to " + ConfigKey.ServerName.GetString() );
Messages.Add( Server.Players.Where( p => !p.Info.IsHidden ).Count().ToString() + " players online" );
Messages.Add( "Staff online: " + Server.Players.Where( p => !p.Info.IsHidden && p.Can( per ) ).JoinToString( r => String.Format( "{0}", r.Name ) ) );
Expand Down
6 changes: 3 additions & 3 deletions fCraft/Commands/Command Handlers/GunHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static void ClickedGlass( object sender, PlayerClickingEventArgs e ) {

public static void movePortal( object sender, PlayerMovingEventArgs e ) {
try {
if ( e.Player.LastUsedPortal != null && ( DateTime.Now - e.Player.LastUsedPortal ).TotalSeconds < 4 ) {
if ( e.Player.LastUsedPortal != null && ( DateTime.UtcNow - e.Player.LastUsedPortal ).TotalSeconds < 4 ) {
return;
}
Vector3I newPos = new Vector3I( e.NewPosition.X / 32, e.NewPosition.Y / 32, ( e.NewPosition.Z / 32 ) );
Expand All @@ -272,7 +272,7 @@ public static void movePortal( object sender, PlayerMovingEventArgs e ) {
L = e.Player.Position.L
} );
}
e.Player.LastUsedPortal = DateTime.Now;
e.Player.LastUsedPortal = DateTime.UtcNow;
}
}
}
Expand All @@ -289,7 +289,7 @@ public static void movePortal( object sender, PlayerMovingEventArgs e ) {
L = e.Player.Position.L
} );
}
e.Player.LastUsedPortal = DateTime.Now;
e.Player.LastUsedPortal = DateTime.UtcNow;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions fCraft/Commands/Command Handlers/RealmHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public static void RealmCreate( Player player, Command cmd, string themeName, st
return;
}

MapGeneratorArgs args = MapGenerator.MakeTemplate( template );
MapGeneratorArgs args = MapGeneratorOld.MakeTemplate( template );
args.MapWidth = wx;
args.MapLength = wy;
args.MapHeight = height;
Expand All @@ -332,9 +332,9 @@ public static void RealmCreate( Player player, Command cmd, string themeName, st
player.MessageNow( "Generating {0} {1}...", theme, template );
}
if ( theme == MapGenTheme.Forest && noTrees && template == MapGenTemplate.Flat ) {
map = MapGenerator.GenerateFlatgrass( args.MapWidth, args.MapLength, args.MapHeight );
map = MapGeneratorOld.GenerateFlatgrass( args.MapWidth, args.MapLength, args.MapHeight );
} else {
MapGenerator generator = new MapGenerator( args );
MapGeneratorOld generator = new MapGeneratorOld( args );
map = generator.Generate();
}
} catch ( Exception ex ) {
Expand Down
3 changes: 1 addition & 2 deletions fCraft/Commands/System.Drawing/FontHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public void Draw( Bitmap img ) {
}
//check if player can make the drawing
if ( !player.CanDraw( Count ) ) {
player.MessageNow( String.Format( "You are only allowed to run commands that affect up to {0} blocks. This one would affect {1} blocks.",
player.Info.Rank.DrawLimit, Count ) );
player.MessageNow( String.Format( "You are only allowed to run commands that affect up to {0} blocks. This one would affect {1} blocks.", player.Info.Rank.DrawLimit, Count ) );
return;
}
//check direction and draw
Expand Down
12 changes: 6 additions & 6 deletions fCraft/Commands/WorldCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ private static void TimeCheck( SchedulerTask task ) {
int sky;
int clouds;
int fog;
DateTime now = DateTime.Now;
DateTime now = DateTime.UtcNow;
var SunriseStart = new TimeSpan( 6, 30, 0 );
var SunriseEnd = new TimeSpan( 7, 29, 59 );
var MorningStart = new TimeSpan( 7, 30, 0 );
Expand Down Expand Up @@ -2350,13 +2350,13 @@ private static void GenHandler( Player player, Command cmd ) {
player.MessageNow( "Generating {0}...", templateFullName );

if ( genEmpty ) {
map = MapGenerator.GenerateEmpty( mapWidth, mapLength, mapHeight );
map = MapGeneratorOld.GenerateEmpty( mapWidth, mapLength, mapHeight );
} else if ( genOcean ) {
map = MapGenerator.GenerateOcean( mapWidth, mapLength, mapHeight );
map = MapGeneratorOld.GenerateOcean( mapWidth, mapLength, mapHeight );
} else if ( genFlatgrass ) {
map = MapGenerator.GenerateFlatgrass( mapWidth, mapLength, mapHeight );
map = MapGeneratorOld.GenerateFlatgrass( mapWidth, mapLength, mapHeight );
} else {
MapGeneratorArgs args = MapGenerator.MakeTemplate( template );
MapGeneratorArgs args = MapGeneratorOld.MakeTemplate( template );
if ( theme == MapGenTheme.Desert ) {
args.AddWater = false;
}
Expand All @@ -2370,7 +2370,7 @@ private static void GenHandler( Player player, Command cmd ) {
args.Theme = theme;
args.AddTrees = !noTrees;

MapGenerator generator = new MapGenerator( args );
MapGeneratorOld generator = new MapGeneratorOld( args );
map = generator.Generate();
}

Expand Down
121 changes: 121 additions & 0 deletions fCraft/Drawing/DrawOps/PlaneDrawOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// fCraft is Copyright 2009-2012 Matvei Stefarov <[email protected]>
// TriangleDrawOperation contributed by Conrad "Redshift" Morgan
using System;

namespace fCraft.Drawing {

public sealed class PlaneDrawOperation : DrawOperation {

public override string Name {
get { return "Plane"; }
}

public override int ExpectedMarks {
get { return 3; }
}

public PlaneDrawOperation( Player player )
: base( player ) {
}

// Plane vertices.
private Vector3I a, b, c, d;

// Edge planes perpendicular to surface, pointing outwards.
private Vector3F s1, s2, s3, s4;

private Vector3I normal;
private Vector3F normalF;

public override bool Prepare( Vector3I[] marks ) {
a = marks[0];
c = marks[1];
b = marks[2];

d = new Vector3I( a.X + c.X - b.X, a.Y + c.Y - b.Y, a.Z + c.Z - b.Z );

Bounds = new BoundingBox(
Math.Min( Math.Min( a.X, b.X ), Math.Min( c.X, d.X ) ),
Math.Min( Math.Min( a.Y, b.Y ), Math.Min( c.Y, d.Y ) ),
Math.Min( Math.Min( a.Z, b.Z ), Math.Min( c.Z, d.Z ) ),
Math.Max( Math.Max( a.X, b.X ), Math.Max( c.X, d.X ) ),
Math.Max( Math.Max( a.Y, b.Y ), Math.Max( c.Y, d.Y ) ),
Math.Max( Math.Max( a.Z, b.Z ), Math.Max( c.Z, d.Z ) )
);

Coords = Bounds.MinVertex;

if ( !base.Prepare( marks ) )
return false;

normal = ( b - a ).Cross( c - a );
normalF = normal.Normalize();
BlocksTotalEstimate = GetBlockTotalEstimate();

s1 = normal.Cross( a - b ).Normalize();
s2 = normal.Cross( b - c ).Normalize();
s3 = normal.Cross( c - d ).Normalize();
s4 = normal.Cross( d - a ).Normalize();

return true;
}

private int GetBlockTotalEstimate() {
Vector3I nabs = normal.Abs();
return Math.Max( Math.Max( nabs.X, nabs.Y ), nabs.Z ) / 2;
}

public override int DrawBatch( int maxBlocksToDraw ) {
int blocksDone = 0;
for ( ; Coords.X <= Bounds.XMax; Coords.X++ ) {
for ( ; Coords.Y <= Bounds.YMax; Coords.Y++ ) {
for ( ; Coords.Z <= Bounds.ZMax; Coords.Z++ ) {
if ( IsPlaneBlock() && DrawOneBlock() ) {
blocksDone++;
if ( blocksDone >= maxBlocksToDraw )
return blocksDone;
}
if ( TimeToEndBatch )
return blocksDone;
}
Coords.Z = Bounds.ZMin;
}
Coords.Y = Bounds.YMin;
}

IsDone = true;
return blocksDone;
}

private const float Extra = 0.5f;

private bool IsPlaneBlock() {
// Early out.
if ( Math.Abs( normalF.Dot( Coords - a ) ) > 1 )
return false;

// Check if within plane region.
if ( ( Coords - a ).Dot( s1 ) > Extra ||
( Coords - b ).Dot( s2 ) > Extra ||
( Coords - c ).Dot( s3 ) > Extra ||
( Coords - d ).Dot( s4 ) > Extra )
return false;

// Check if minimal plane block.
return TestAxis( 1, 0, 0 ) ||
TestAxis( 0, 1, 0 ) ||
TestAxis( 0, 0, 1 );
}

// Checks distance to plane along axis.
private bool TestAxis( int x, int y, int z ) {
Vector3I v = new Vector3I( x, y, z );
int numerator = normal.Dot( a - Coords );
int denominator = normal.Dot( v );
if ( denominator == 0 )
return numerator == 0;
double distance = ( double )numerator / denominator;
return distance > -0.5 && distance <= 0.5;
}
}
}
Loading

0 comments on commit a0061f0

Please sign in to comment.