Beispiel #1
0
        public static CompileSpecification Parse(GenericStructure gs)
        {
            var spec = new CompileSpecification {
                ID = gs["ID"] ?? "", Name = gs["Name"] ?? ""
            };
            var tools = gs.GetChildren("Tool");

            spec.Tools.AddRange(tools.Select(CompileTool.Parse));
            var presets = gs.GetChildren("Preset");

            spec.Presets.AddRange(presets.Select(CompilePreset.Parse));
            return(spec);
        }
Beispiel #2
0
        public void Read(GenericStructure gs)
        {
            ID                       = gs.PropertyInteger("ID");
            Name                     = gs["Name"];
            Specification            = gs["Specification"];
            Engine                   = (Engine)Enum.Parse(typeof(Engine), gs["EngineID"]);
            DontRedirectOutput       = gs.PropertyBoolean("DontRedirectOutput");
            Path                     = gs["Path"];
            Bsp                      = gs["Bsp"];
            Csg                      = gs["Csg"];
            Vis                      = gs["Vis"];
            Rad                      = gs["Rad"];
            IncludePathInEnvironment = gs.PropertyBoolean("IncludePathInEnvironment", true);

            WorkingDirectory  = gs.PropertyEnum("WorkingDirectory", CompileWorkingDirectory.TemporaryDirectory);
            AfterCopyBsp      = gs.PropertyBoolean("AfterCopyBsp");
            AfterRunGame      = gs.PropertyBoolean("AfterRunGame");
            AfterAskBeforeRun = gs.PropertyBoolean("AfterAskBeforeRun");

            CopyBsp = gs.PropertyBoolean("CopyBsp");
            CopyRes = gs.PropertyBoolean("CopyRes");
            CopyLin = gs.PropertyBoolean("CopyLin");
            CopyMap = gs.PropertyBoolean("CopyMap");
            CopyPts = gs.PropertyBoolean("CopyPts");
            CopyLog = gs.PropertyBoolean("CopyLog");
            CopyErr = gs.PropertyBoolean("CopyErr");

            foreach (var prof in gs.GetChildren("Profile"))
            {
                var bp = new BuildProfile();
                bp.Read(prof);
                Profiles.Add(bp);
            }
        }
Beispiel #3
0
        public static IEnumerable <MapObject> ExtractCopyStream(GenericStructure gs, IDGenerator generator, Dictionary <string, UInt32> entcnt)
        {
            if (gs == null || gs.Name != "clipboard")
            {
                return(null);
            }
            var dummyGen = new IDGenerator();
            var list     = new List <MapObject>();
            var world    = ReadWorld(gs, dummyGen);

            foreach (var entity in gs.GetChildren("entity"))
            {
                var ent = ReadEntity(entity, dummyGen);

                for (int x = 0; x < ent.EntityData.Properties.Count(); x++)
                {
                    if (ent.EntityData.Properties[x].Key == "%name%")
                    {
                        entcnt[ent.ClassName.ToLower()]   += 1;
                        ent.EntityData.Properties[x].Value = ent.ClassName.ToLower() + entcnt[ent.ClassName.ToLower()].ToString();
                        //entity.EntityData.Properties[x] = Document.Map.WorldSpawn.MetaData.Get<string>["EntityCounts"];
                    }
                }

                var groupid   = entity.Children.Where(x => x.Name == "editor").Select(x => x.PropertyInteger("groupid")).FirstOrDefault();
                var entParent = groupid > 0 ? world.Find(x => x.ID == groupid && x is Group).FirstOrDefault() ?? world : world;
                ent.SetParent(entParent);
            }
            list.AddRange(world.GetChildren());
            Reindex(list, generator);
            return(list);
        }
Beispiel #4
0
        private static Group ReadGroup(GenericStructure group, IDGenerator generator)
        {
            var g      = new Group(GetObjectID(group, generator));
            var editor = group.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");

            g.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
            g.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse).Where(x => x > 0));
            return(g);
        }
Beispiel #5
0
        private static Entity ReadEntity(GenericStructure entity, IDGenerator generator)
        {
            var ret = new Entity(GetObjectID(entity, generator))
            {
                ClassName  = entity["classname"],
                EntityData = ReadEntityData(entity),
                Origin     = entity.PropertyCoordinate("origin")
            };
            var editor = entity.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");

            ret.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
            ret.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse).Where(x => x > 0));
            foreach (var child in entity.GetChildren("solid").Select(solid => ReadSolid(solid, generator)).Where(s => s != null))
            {
                child.SetParent(ret, false);
            }
            ret.UpdateBoundingBox(false);
            return(ret);
        }
Beispiel #6
0
        private static Displacement ReadDisplacement(long id, GenericStructure dispinfo)
        {
            var disp = new Displacement(id);

            // power, startposition, flags, elevation, subdiv, normals{}, distances{},
            // offsets{}, offset_normals{}, alphas{}, triangle_tags{}, allowed_verts{}
            disp.SetPower(dispinfo.PropertyInteger("power", 3));
            disp.StartPosition = dispinfo.PropertyCoordinate("startposition");
            disp.Elevation     = dispinfo.PropertyDecimal("elevation");
            disp.SubDiv        = dispinfo.PropertyInteger("subdiv") > 0;
            var size          = disp.Resolution + 1;
            var normals       = dispinfo.GetChildren("normals").FirstOrDefault();
            var distances     = dispinfo.GetChildren("distances").FirstOrDefault();
            var offsets       = dispinfo.GetChildren("offsets").FirstOrDefault();
            var offsetNormals = dispinfo.GetChildren("offset_normals").FirstOrDefault();
            var alphas        = dispinfo.GetChildren("alphas").FirstOrDefault();

            //var triangleTags = dispinfo.GetChildren("triangle_tags").First();
            //var allowedVerts = dispinfo.GetChildren("allowed_verts").First();
            for (var i = 0; i < size; i++)
            {
                var row  = "row" + i;
                var norm = normals != null?normals.PropertyCoordinateArray(row, size) : Enumerable.Range(0, size).Select(x => Coordinate.Zero).ToArray();

                var dist = distances != null?distances.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();

                var offn = offsetNormals != null?offsetNormals.PropertyCoordinateArray(row, size) : Enumerable.Range(0, size).Select(x => Coordinate.Zero).ToArray();

                var offs = offsets != null?offsets.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();

                var alph = alphas != null?alphas.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();

                for (var j = 0; j < size; j++)
                {
                    disp.Points[i, j].Displacement       = new Vector(norm[j], dist[j]);
                    disp.Points[i, j].OffsetDisplacement = new Vector(offn[j], offs[j]);
                    disp.Points[i, j].Alpha = alph[j];
                }
            }
            return(disp);
        }
Beispiel #7
0
        public static CompileTool Parse(GenericStructure gs)
        {
            var tool = new CompileTool
            {
                Name        = gs["Name"] ?? "",
                Description = gs["Description"] ?? "",
                Order       = gs.PropertyInteger("Order"),
                Enabled     = gs.PropertyBoolean("Enabled", true)
            };
            var parameters = gs.GetChildren("Parameter");

            tool.Parameters.AddRange(parameters.Select(CompileParameter.Parse));
            return(tool);
        }
Beispiel #8
0
 public static IEnumerable<MapObject> ExtractCopyStream(GenericStructure gs, IDGenerator generator)
 {
     if (gs == null || gs.Name != "clipboard") return null;
     var dummyGen = new IDGenerator();
     var list = new List<MapObject>();
     var world = ReadWorld(gs, dummyGen);
     foreach (var entity in gs.GetChildren("entity"))
     {
         var ent = ReadEntity(entity, dummyGen);
         var groupid = entity.Children.Where(x => x.Name == "editor").Select(x => x.PropertyInteger("groupid")).FirstOrDefault();
         var entParent = groupid > 0 ? world.Find(x => x.ID == groupid && x is Group).FirstOrDefault() ?? world : world;
         ent.SetParent(entParent);
     }
     list.AddRange(world.GetChildren());
     Reindex(list, generator);
     return list;
 }
Beispiel #9
0
        private static Face ReadFace(GenericStructure side, IDGenerator generator)
        {
            var id = side.PropertyLong("id");

            if (id == 0)
            {
                id = generator.GetNextFaceID();
            }
            var dispinfo = side.GetChildren("dispinfo").FirstOrDefault();
            var ret      = dispinfo != null?ReadDisplacement(id, dispinfo) : new Face(id);

            // id, plane, material, uaxis, vaxis, rotation, lightmapscale, smoothing_groups
            var uaxis = side.PropertyTextureAxis("uaxis");
            var vaxis = side.PropertyTextureAxis("vaxis");

            ret.Texture.Name     = side["material"];
            ret.Texture.UAxis    = uaxis.Item1;
            ret.Texture.XShift   = uaxis.Item2;
            ret.Texture.XScale   = uaxis.Item3;
            ret.Texture.VAxis    = vaxis.Item1;
            ret.Texture.YShift   = vaxis.Item2;
            ret.Texture.YScale   = vaxis.Item3;
            ret.Texture.Rotation = side.PropertyDecimal("rotation");
            ret.Plane            = side.PropertyPlane("plane");
            //NOTE(SVK) Paste RF data
            ret.Texture.Flags            = (FaceFlags)side.PropertyInteger("flags");
            ret.Texture.Translucency     = (int)side.PropertyDecimal("translucency");
            ret.Texture.Opacity          = side.PropertyDecimal("opacity");
            ret.Texture.PositionRF       = side.PropertyCoordinate("positionrf");
            ret.Texture.TransformAngleRF = side.PropertyMatrix("transformanglerf");

            var verts = side.Children.FirstOrDefault(x => x.Name == "vertex");

            if (verts != null)
            {
                var count = verts.PropertyInteger("count");
                for (var i = 0; i < count; i++)
                {
                    ret.Vertices.Add(new Vertex(verts.PropertyCoordinate("vertex" + i), ret));
                }
            }

            return(ret);
        }
Beispiel #10
0
        public static IEnumerable <MapObject> ExtractCopyStream(GenericStructure gs, IDGenerator generator)
        {
            if (gs == null || gs.Name != "clipboard")
            {
                return(null);
            }
            var dummyGen = new IDGenerator();
            var list     = new List <MapObject>();
            var world    = ReadWorld(gs, dummyGen);

            foreach (var entity in gs.GetChildren("entity"))
            {
                var ent       = ReadEntity(entity, dummyGen);
                var groupid   = entity.Children.Where(x => x.Name == "editor").Select(x => x.PropertyInteger("groupid")).FirstOrDefault();
                var entParent = groupid > 0 ? world.Find(x => x.ID == groupid && x is Group).FirstOrDefault() ?? world : world;
                ent.SetParent(entParent);
            }
            list.AddRange(world.GetChildren());
            Reindex(list, generator);
            return(list);
        }
Beispiel #11
0
 private static Entity ReadEntity(GenericStructure entity, IDGenerator generator)
 {
     var ret = new Entity(GetObjectID(entity, generator))
                   {
                       ClassName = entity["classname"],
                       EntityData = ReadEntityData(entity),
                       Origin = entity.PropertyCoordinate("origin")
                   };
     var editor = entity.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
     ret.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
     ret.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse));
     foreach (var child in entity.GetChildren("solid").Select(solid => ReadSolid(solid, generator)).Where(s => s != null))
     {
         child.SetParent(ret);
     }
     ret.UpdateBoundingBox(false);
     return ret;
 }
Beispiel #12
0
        protected override DataStructures.MapObjects.Map GetFromStream(Stream stream, string fgd = null)
        {
            using (var reader = new StreamReader(stream))
            {
                var parent = new GenericStructure("Root");
                parent.Children.AddRange(GenericStructure.Parse(reader));
                // Sections from a Hammer map:
                // - world
                // - entity
                // - visgroups
                // - cordon
                // Not done yet
                // - versioninfo
                // - viewsettings
                // - cameras

                var map = new DataStructures.MapObjects.Map();

                var world        = parent.GetChildren("world").FirstOrDefault();
                var entities     = parent.GetChildren("entity");
                var visgroups    = parent.GetChildren("visgroups").SelectMany(x => x.GetChildren("visgroup"));
                var cameras      = parent.GetChildren("cameras").FirstOrDefault();
                var cordon       = parent.GetChildren("cordon").FirstOrDefault();
                var viewsettings = parent.GetChildren("viewsettings").FirstOrDefault();

                foreach (var visgroup in visgroups)
                {
                    var vg = ReadVisgroup(visgroup);
                    if (vg.ID < 0 && vg.Name == "Auto")
                    {
                        continue;
                    }
                    map.Visgroups.Add(vg);
                }

                if (world != null)
                {
                    map.WorldSpawn = ReadWorld(world, map.IDGenerator);
                }
                foreach (var entity in entities)
                {
                    var ent       = ReadEntity(entity, map.IDGenerator);
                    var groupid   = entity.Children.Where(x => x.Name == "editor").Select(x => x.PropertyInteger("groupid")).FirstOrDefault();
                    var entParent = groupid > 0 ? map.WorldSpawn.Find(x => x.ID == groupid && x is Group).FirstOrDefault() ?? map.WorldSpawn : map.WorldSpawn;
                    ent.SetParent(entParent);
                }

                var activeCamera = 0;
                if (cameras != null)
                {
                    activeCamera = cameras.PropertyInteger("activecamera");
                    foreach (var cam in cameras.GetChildren("camera"))
                    {
                        var pos  = cam.PropertyCoordinate("position");
                        var look = cam.PropertyCoordinate("look");
                        if (pos != null && look != null)
                        {
                            map.Cameras.Add(new Camera {
                                EyePosition = pos, LookPosition = look
                            });
                        }
                    }
                }
                if (!map.Cameras.Any())
                {
                    map.Cameras.Add(new Camera {
                        EyePosition = Coordinate.Zero, LookPosition = Coordinate.UnitY
                    });
                }
                if (activeCamera < 0 || activeCamera >= map.Cameras.Count)
                {
                    activeCamera = 0;
                }
                map.ActiveCamera = map.Cameras[activeCamera];

                if (cordon != null)
                {
                    var start = cordon.PropertyCoordinate("mins", map.CordonBounds.Start);
                    var end   = cordon.PropertyCoordinate("maxs", map.CordonBounds.End);
                    map.CordonBounds = new Box(start, end);
                    map.Cordon       = cordon.PropertyBoolean("active", map.Cordon);
                }

                if (viewsettings != null)
                {
                    map.SnapToGrid         = viewsettings.PropertyBoolean("bSnapToGrid", map.SnapToGrid);
                    map.Show2DGrid         = viewsettings.PropertyBoolean("bShowGrid", map.Show2DGrid);
                    map.Show3DGrid         = viewsettings.PropertyBoolean("bShow3DGrid", map.Show3DGrid);
                    map.GridSpacing        = viewsettings.PropertyDecimal("nGridSpacing", map.GridSpacing);
                    map.IgnoreGrouping     = viewsettings.PropertyBoolean("bIgnoreGrouping", map.IgnoreGrouping);
                    map.HideFaceMask       = viewsettings.PropertyBoolean("bHideFaceMask", map.HideFaceMask);
                    map.HideNullTextures   = viewsettings.PropertyBoolean("bHideNullTextures", map.HideNullTextures);
                    map.TextureLock        = viewsettings.PropertyBoolean("bTextureLock", map.TextureLock);
                    map.TextureScalingLock = viewsettings.PropertyBoolean("bTextureScalingLock", map.TextureScalingLock);
                }

                return(map);
            }
        }
Beispiel #13
0
        private static World ReadWorld(GenericStructure world, IDGenerator generator)
        {
            var ret = new World(GetObjectID(world, generator))
            {
                ClassName  = "worldspawn",
                EntityData = ReadEntityData(world)
            };

            // Load groups
            var groups = new Dictionary <Group, long>();

            foreach (var group in world.GetChildren("group"))
            {
                var g      = ReadGroup(group, generator);
                var editor = group.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                var gid    = editor.PropertyLong("groupid");
                groups.Add(g, gid);
            }

            // Build group tree
            var assignedGroups = groups.Where(x => x.Value == 0).Select(x => x.Key).ToList();

            foreach (var ag in assignedGroups)
            {
                // Add the groups with no parent
                ag.SetParent(ret, false);
                groups.Remove(ag);
            }
            while (groups.Any())
            {
                var canAssign = groups.Where(x => assignedGroups.Any(y => y.ID == x.Value)).ToList();
                if (!canAssign.Any())
                {
                    break;
                }
                foreach (var kv in canAssign)
                {
                    // Add the group to the tree and the assigned list, remove it from the groups list
                    var parent = assignedGroups.First(y => y.ID == kv.Value);
                    kv.Key.SetParent(parent, false);
                    assignedGroups.Add(kv.Key);
                    groups.Remove(kv.Key);
                }
            }

            // Load visible solids
            foreach (var read in world.GetChildren("solid").AsParallel().Select(x => new { Solid = ReadSolid(x, generator), Structure = x }))
            {
                var s     = read.Solid;
                var solid = read.Structure;
                if (s == null)
                {
                    continue;
                }

                var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                var gid    = editor.PropertyLong("groupid");
                var parent = gid > 0 ? assignedGroups.FirstOrDefault(x => x.ID == gid) ?? (MapObject)ret : ret;
                s.SetParent(parent, false);
            }

            // Load hidden solids
            foreach (var hidden in world.GetChildren("hidden"))
            {
                foreach (var read in hidden.GetChildren("solid").AsParallel().Select(x => new { Solid = ReadSolid(x, generator), Structure = x }))
                {
                    var s     = read.Solid;
                    var solid = read.Structure;
                    if (s == null)
                    {
                        continue;
                    }

                    s.IsVisgroupHidden = true;

                    var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                    var gid    = editor.PropertyLong("groupid");
                    var parent = gid > 0 ? assignedGroups.FirstOrDefault(x => x.ID == gid) ?? (MapObject)ret : ret;
                    s.SetParent(parent, false);
                }
            }

            assignedGroups.ForEach(x => x.UpdateBoundingBox());
            ret.UpdateBoundingBox();

            return(ret);
        }
Beispiel #14
0
 private static Group ReadGroup(GenericStructure group, IDGenerator generator)
 {
     var g = new Group(GetObjectID(group, generator));
     var editor = group.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
     g.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
     g.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse));
     return g;
 }
Beispiel #15
0
        private static Solid ReadSolid(GenericStructure solid, IDGenerator generator)
        {
            var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
            var faces = solid.GetChildren("side").Select(x => ReadFace(x, generator)).ToList();

            Solid ret;

            if (faces.All(x => x.Vertices.Count >= 3))
            {
                // Vertices were stored in the VMF
                ret = new Solid(GetObjectID(solid, generator));
                ret.Faces.AddRange(faces);
            }
            else
            {
                // Need to grab the vertices using plane intersections
                var idg = new IDGenerator(); // No need to increment the id generator if it doesn't have to be
                ret = Solid.CreateFromIntersectingPlanes(faces.Select(x => x.Plane), idg);
                ret.ID = GetObjectID(solid, generator);

                for (var i = 0; i < ret.Faces.Count; i++)
                {
                    var face = ret.Faces[i];
                    var f = faces.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(ret.Faces[i].Plane.Normal));
                    if (f == null)
                    {
                        // TODO: Report invalid solids
                        Debug.WriteLine("Invalid solid! ID: " + solid["id"]);
                        return null;
                    }
                    face.Texture = f.Texture;

                    var disp = f as Displacement;
                    if (disp == null) continue;

                    disp.Plane = face.Plane;
                    disp.Vertices = face.Vertices;
                    disp.Texture = f.Texture;
                    disp.AlignTextureToWorld();
                    disp.CalculatePoints();
                    ret.Faces[i] = disp;
                }
            }

            ret.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
            ret.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse));
            foreach (var face in ret.Faces)
            {
                face.Parent = ret;
                face.Colour = ret.Colour;
                face.UpdateBoundingBox();
            }

            if (ret.Faces.Any(x => x is Displacement))
            {
                ret.Faces.ForEach(x => x.IsHidden = !(x is Displacement));
            }

            ret.UpdateBoundingBox(false);

            return ret;
        }
Beispiel #16
0
        private static Solid ReadSolid(GenericStructure solid, IDGenerator generator)
        {
            var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
            var faces  = solid.GetChildren("side").Select(x => ReadFace(x, generator)).ToList();

            Solid ret;

            if (faces.All(x => x.Vertices.Count >= 3))
            {
                // Vertices were stored in the VMF
                ret = new Solid(GetObjectID(solid, generator));
                ret.Faces.AddRange(faces);
            }
            else
            {
                // Need to grab the vertices using plane intersections
                var idg = new IDGenerator(); // No need to increment the id generator if it doesn't have to be
                ret    = Solid.CreateFromIntersectingPlanes(faces.Select(x => x.Plane), idg);
                ret.ID = GetObjectID(solid, generator);

                for (var i = 0; i < ret.Faces.Count; i++)
                {
                    var face = ret.Faces[i];
                    var f    = faces.FirstOrDefault(x => x.Plane.Normal.EquivalentTo(ret.Faces[i].Plane.Normal));
                    if (f == null)
                    {
                        // TODO: Report invalid solids
                        Debug.WriteLine("Invalid solid! ID: " + solid["id"]);
                        return(null);
                    }
                    face.Texture = f.Texture;

                    var disp = f as Displacement;
                    if (disp == null)
                    {
                        continue;
                    }

                    disp.Plane    = face.Plane;
                    disp.Vertices = face.Vertices;
                    disp.Texture  = f.Texture;
                    disp.AlignTexture();
                    disp.CalculatePoints();
                    ret.Faces[i] = disp;
                }
            }

            ret.Flags  = (UInt32)solid.PropertyInteger("flags");
            ret.Colour = editor.PropertyColour("color", Colour.GetRandomBrushColour());
            ret.Visgroups.AddRange(editor.GetAllPropertyValues("visgroupid").Select(int.Parse).Where(x => x > 0));
            foreach (var face in ret.Faces)
            {
                face.Parent = ret;
                face.Colour = ret.Colour;
                face.UpdateBoundingBox();
            }

            if (ret.Faces.Any(x => x is Displacement))
            {
                ret.Faces.ForEach(x => x.IsHidden = !(x is Displacement));
            }

            ret.UpdateBoundingBox(false);

            return(ret);
        }
Beispiel #17
0
        private static Face ReadFace(GenericStructure side, IDGenerator generator)
        {
            var id = side.PropertyLong("id");
            if (id == 0) id = generator.GetNextFaceID();
            var dispinfo = side.GetChildren("dispinfo").FirstOrDefault();
            var ret = dispinfo != null ? ReadDisplacement(id, dispinfo) : new Face(id);
            // id, plane, material, uaxis, vaxis, rotation, lightmapscale, smoothing_groups
            var uaxis = side.PropertyTextureAxis("uaxis");
            var vaxis = side.PropertyTextureAxis("vaxis");
            ret.Texture.Name = side["material"];
            ret.Texture.UAxis = uaxis.Item1;
            ret.Texture.XShift = uaxis.Item2;
            ret.Texture.XScale = uaxis.Item3;
            ret.Texture.VAxis = vaxis.Item1;
            ret.Texture.YShift = vaxis.Item2;
            ret.Texture.YScale = vaxis.Item3;
            ret.Texture.Rotation = side.PropertyDecimal("rotation");
            ret.Plane = side.PropertyPlane("plane");

            var verts = side.Children.FirstOrDefault(x => x.Name == "vertex");
            if (verts != null)
            {
                var count = verts.PropertyInteger("count");
                for (var i = 0; i < count; i++)
                {
                    ret.Vertices.Add(new Vertex(verts.PropertyCoordinate("vertex"+i), ret));
                }
            }

            return ret;
        }
Beispiel #18
0
 private static Displacement ReadDisplacement(long id, GenericStructure dispinfo)
 {
     var disp = new Displacement(id);
     // power, startposition, flags, elevation, subdiv, normals{}, distances{},
     // offsets{}, offset_normals{}, alphas{}, triangle_tags{}, allowed_verts{}
     disp.SetPower(dispinfo.PropertyInteger("power", 3));
     disp.StartPosition = dispinfo.PropertyCoordinate("startposition");
     disp.Elevation = dispinfo.PropertyDecimal("elevation");
     disp.SubDiv = dispinfo.PropertyInteger("subdiv") > 0;
     var size = disp.Resolution + 1;
     var normals = dispinfo.GetChildren("normals").FirstOrDefault();
     var distances = dispinfo.GetChildren("distances").FirstOrDefault();
     var offsets = dispinfo.GetChildren("offsets").FirstOrDefault();
     var offsetNormals = dispinfo.GetChildren("offset_normals").FirstOrDefault();
     var alphas = dispinfo.GetChildren("alphas").FirstOrDefault();
     //var triangleTags = dispinfo.GetChildren("triangle_tags").First();
     //var allowedVerts = dispinfo.GetChildren("allowed_verts").First();
     for (var i = 0; i < size; i++)
     {
         var row = "row" + i;
         var norm = normals != null ? normals.PropertyCoordinateArray(row, size) : Enumerable.Range(0, size).Select(x => Coordinate.Zero).ToArray();
         var dist = distances != null ? distances.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();
         var offn = offsetNormals != null ? offsetNormals.PropertyCoordinateArray(row, size) : Enumerable.Range(0, size).Select(x => Coordinate.Zero).ToArray();
         var offs = offsets != null ? offsets.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();
         var alph = alphas != null ? alphas.PropertyDecimalArray(row, size) : Enumerable.Range(0, size).Select(x => 0m).ToArray();
         for (var j = 0; j < size; j++)
         {
             disp.Points[i, j].Displacement = new Vector(norm[j], dist[j]);
             disp.Points[i, j].OffsetDisplacement = new Vector(offn[j], offs[j]);
             disp.Points[i, j].Alpha = alph[j];
         }
     }
     return disp;
 }
Beispiel #19
0
        protected override DataStructures.MapObjects.Map GetFromStream(Stream stream)
        {
            using (var reader = new StreamReader(stream))
            {
                var parent = new GenericStructure("Root");
                parent.Children.AddRange(GenericStructure.Parse(reader));
                // Sections from a Hammer map:
                // - world
                // - entity
                // - visgroups
                // - cordon
                // Not done yet
                // - versioninfo
                // - viewsettings
                // - cameras

                var map = new DataStructures.MapObjects.Map();

                var world = parent.GetChildren("world").FirstOrDefault();
                var entities = parent.GetChildren("entity");
                var visgroups = parent.GetChildren("visgroups").SelectMany(x => x.GetChildren("visgroup"));
                var cameras = parent.GetChildren("cameras").FirstOrDefault();
                var cordon = parent.GetChildren("cordon").FirstOrDefault();
                var viewsettings = parent.GetChildren("viewsettings").FirstOrDefault();

                foreach (var visgroup in visgroups)
                {
                    var vg = ReadVisgroup(visgroup);
                    if (vg.ID < 0 && vg.Name == "Auto") continue;
                    map.Visgroups.Add(vg);
                }

                if (world != null) map.WorldSpawn = ReadWorld(world, map.IDGenerator);
                foreach (var entity in entities)
                {
                    var ent = ReadEntity(entity, map.IDGenerator);
                    var groupid = entity.Children.Where(x => x.Name == "editor").Select(x => x.PropertyInteger("groupid")).FirstOrDefault();
                    var entParent = groupid > 0 ? map.WorldSpawn.Find(x => x.ID == groupid && x is Group).FirstOrDefault() ?? map.WorldSpawn : map.WorldSpawn;
                    ent.SetParent(entParent);
                }

                var activeCamera = 0;
                if (cameras != null)
                {
                    activeCamera = cameras.PropertyInteger("activecamera");
                    foreach (var cam in cameras.GetChildren("camera"))
                    {
                        var pos = cam.PropertyCoordinate("position");
                        var look = cam.PropertyCoordinate("look");
                        if (pos != null && look != null)
                        {
                            map.Cameras.Add(new Camera {EyePosition = pos, LookPosition = look});
                        }
                    }
                }
                if (!map.Cameras.Any())
                {
                    map.Cameras.Add(new Camera { EyePosition = Coordinate.Zero, LookPosition = Coordinate.UnitY });
                }
                if (activeCamera < 0 || activeCamera >= map.Cameras.Count)
                {
                    activeCamera = 0;
                }
                map.ActiveCamera = map.Cameras[activeCamera];

                if (cordon != null)
                {
                    var start = cordon.PropertyCoordinate("mins", map.CordonBounds.Start);
                    var end = cordon.PropertyCoordinate("maxs", map.CordonBounds.End);
                    map.CordonBounds = new Box(start, end);
                    map.Cordon = cordon.PropertyBoolean("active", map.Cordon);
                }

                if (viewsettings != null)
                {
                    map.SnapToGrid = viewsettings.PropertyBoolean("bSnapToGrid", map.SnapToGrid);
                    map.Show2DGrid = viewsettings.PropertyBoolean("bShowGrid", map.Show2DGrid);
                    map.Show3DGrid = viewsettings.PropertyBoolean("bShow3DGrid", map.Show3DGrid);
                    map.GridSpacing = viewsettings.PropertyDecimal("nGridSpacing", map.GridSpacing);
                    map.IgnoreGrouping = viewsettings.PropertyBoolean("bIgnoreGrouping", map.IgnoreGrouping);
                    map.HideFaceMask = viewsettings.PropertyBoolean("bHideFaceMask", map.HideFaceMask);
                    map.HideNullTextures = viewsettings.PropertyBoolean("bHideNullTextures", map.HideNullTextures);
                    map.TextureLock = viewsettings.PropertyBoolean("bTextureLock", map.TextureLock);
                    map.TextureScalingLock = viewsettings.PropertyBoolean("bTextureScalingLock", map.TextureScalingLock);
                }

                return map;
            }
        }
Beispiel #20
0
        private static World ReadWorld(GenericStructure world, IDGenerator generator)
        {
            var ret = new World(GetObjectID(world, generator))
                          {
                              ClassName = "worldspawn",
                              EntityData = ReadEntityData(world)
                          };

            // Load groups
            var groups = new Dictionary<Group, long>();
            foreach (var group in world.GetChildren("group"))
            {
                var g = ReadGroup(group, generator);
                var editor = group.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                var gid = editor.PropertyLong("groupid");
                groups.Add(g, gid);
            }

            // Build group tree
            var assignedGroups = groups.Where(x => x.Value == 0).Select(x => x.Key).ToList();
            foreach (var ag in assignedGroups)
            {
                // Add the groups with no parent
                ag.SetParent(ret);
                groups.Remove(ag);
            }
            while (groups.Any())
            {
                var canAssign = groups.Where(x => assignedGroups.Any(y => y.ID == x.Value)).ToList();
                if (!canAssign.Any())
                {
                    break;
                }
                foreach (var kv in canAssign)
                {
                    // Add the group to the tree and the assigned list, remove it from the groups list
                    var parent = assignedGroups.First(y => y.ID == kv.Value);
                    kv.Key.SetParent(parent);
                    assignedGroups.Add(kv.Key);
                    groups.Remove(kv.Key);
                }
            }

            // Load visible solids
            foreach (var solid in world.GetChildren("solid"))
            {
                var s = ReadSolid(solid, generator);
                if (s == null) continue;

                var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                var gid = editor.PropertyLong("groupid");
                var parent = gid > 0 ? assignedGroups.FirstOrDefault(x => x.ID == gid) ?? (MapObject) ret : ret;
                s.SetParent(parent);
                parent.UpdateBoundingBox();
            }

            // Load hidden solids
            foreach (var hidden in world.GetChildren("hidden"))
            {
                foreach (var solid in hidden.GetChildren("solid"))
                {
                    var s = ReadSolid(solid, generator);
                    if (s == null) continue;

                    s.IsVisgroupHidden = true;

                    var editor = solid.GetChildren("editor").FirstOrDefault() ?? new GenericStructure("editor");
                    var gid = editor.PropertyLong("groupid");
                    var parent = gid > 0 ? assignedGroups.FirstOrDefault(x => x.ID == gid) ?? (MapObject)ret : ret;
                    s.SetParent(parent);
                    parent.UpdateBoundingBox();
                }
            }

            assignedGroups.ForEach(x => x.UpdateBoundingBox(false));

            return ret;
        }