Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. public string BasePath = @"C:\Users\reiv3_000\AppData\Roaming\SpaceEngineers\Saves\76561197993733345\Dark Horizons";
  2. public string BaseFileName = "Sandbox_Base.sbs";
  3.  
  4. void Main()
  5. {
  6.     var sectorCount = 27;
  7.     var sectorSize = 16000;
  8.     var subSectorCount = 4;
  9.     var maxMapSize = new Vector3(50000, 50000, 50000);
  10.     var beacons = new List<Beacon>();
  11.     XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
  12.     var doc = XDocument.Load(Path.Combine(BasePath, BaseFileName));
  13.     var sectorObjects = doc
  14.         .Root
  15.         //.Element("MyObjectBuilder_Sector");
  16.         .Element("SectorObjects");
  17.    
  18.    
  19.     var beaconObject = sectorObjects
  20.         .Elements("MyObjectBuilder_EntityBase")
  21.         .Where(node => node.Attribute(ns + "type").Value.Equals("MyObjectBuilder_CubeGrid"))
  22.         .First();
  23.     //beaconObject.Dump();
  24.    
  25.     var max = new Vector3(maxMapSize.X / 2, maxMapSize.Y / 2, maxMapSize.Z / 2);
  26.     var offset = new Vector3(maxMapSize.X % sectorSize / 2, maxMapSize.Y % sectorSize / 2, maxMapSize.Z % sectorSize / 2);
  27.     var random = new Random();
  28.     for(var x = maxMapSize.X / 2f * -1f + offset.X;x + sectorSize < max.X;x += sectorSize) {
  29.         for(var y = maxMapSize.Y / 2f * -1f + offset.Y;y + sectorSize < max.Y;y += sectorSize) {
  30.             for(var z = maxMapSize.Z / 2f * -1f + offset.Z;z + sectorSize < max.Z;z += sectorSize) {
  31.            
  32.                 var subSectors =  new List<Beacon>();
  33.                 var beaconsThisSector = random.Next(subSectorCount / 2, subSectorCount);
  34.                 var sectorName = "ShitBaskets";
  35.                 while(subSectors.Count < beaconsThisSector) {
  36.                     var beacon = new Vector3((float)Math.Round((random.NextDouble() * sectorSize + x), 0),
  37.                         (float)Math.Round((random.NextDouble() * sectorSize) + y, 0),
  38.                         (float)Math.Round((random.NextDouble() * sectorSize) + z, 0));
  39.                        
  40.                     if(subSectors.Count == 0
  41.                         || subSectors.Any(b => beacon.DistanceFrom(b.Position.X, b.Position.Y, b.Position.Z) < 10000
  42.                             && beacon.DistanceFrom(b.Position.X, b.Position.Y, b.Position.Z) > 5000)) {
  43.                         subSectors.Add(new Beacon(beacon, sectorName + " - " + subSectors.Count));
  44.                     }
  45.                 }
  46.                
  47.                 beacons.AddRange(subSectors);
  48.             }          
  49.         }
  50.     }
  51.    
  52.     // Create the new save
  53.    
  54.     foreach(var beacon in beacons) {
  55.         var position = beaconObject.Element("PositionAndOrientation").Element("Position");
  56.         position.Attribute("x").Value = beacon.Position.X.ToString();
  57.         position.Attribute("y").Value = beacon.Position.Y.ToString();
  58.         position.Attribute("z").Value = beacon.Position.Z.ToString();
  59.         beaconObject.Element("EntityId").Value = random.Next(int.MaxValue).ToString();
  60.         foreach(var entityId in beaconObject.Element("CubeBlocks").Elements("MyObjectBuilder_CubeBlock").Select(element => element.Element("EntityId"))) {
  61.             if(entityId != null)
  62.                 entityId.Value = random.Next(int.MaxValue).ToString();
  63.         }
  64.         beaconObject.Element("CubeBlocks")
  65.             .Elements("MyObjectBuilder_CubeBlock")
  66.             .Where(element => element.Attribute(ns + "type").Value.Equals("MyObjectBuilder_Beacon"))
  67.             .First()
  68.             .SetElementValue("CustomName", beacon.Name);
  69.         sectorObjects.Add(beaconObject);
  70.         string.Format("Beacon Created: ({0},{1},{2})", beacon.Position.X, beacon.Position.Y, beacon.Position.Z).Dump();
  71.     }
  72.     doc.Save(Path.Combine(BasePath, "Sandbox_0_0_0_New.sbs"));
  73. }
  74.  
  75. // Define other methods and classes here
  76. public struct Vector3 {
  77.     public float X;
  78.     public float Y;
  79.     public float Z;
  80.    
  81.     public Vector3(float x, float y, float z) {
  82.         X = x;
  83.         Y = y;
  84.         Z = z;
  85.     }
  86.    
  87.     public Vector3(int x, int y, int z) {
  88.         X = (float)x;
  89.         Y = (float)y;
  90.         Z = (float)z;
  91.     }
  92.    
  93.     public float DistanceFrom(float x, float y, float z) {
  94.         return (float)Math.Sqrt(Math.Pow((x - X), 2f) + Math.Pow((y - Y), 2f) + Math.Pow((z - Z), 2f));
  95.     }
  96. }
  97.  
  98. public class Beacon {
  99.     public string Name;
  100.     public Vector3 Position;
  101.     public string Sector;
  102.    
  103.     public Beacon(Vector3 position, string name) {
  104.         Name = name;
  105.         Position = position;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement