public MyPlanetEnvironmentMapping(PlanetEnvironmentItemMapping map) { Rule = map.Rule; Items = new MyMaterialEnvironmentItem[map.Items.Length]; if (Items.Length <= 0) { CumulativeIntervals = null; TotalFrequency = 0; return; } TotalFrequency = 0; for (int i = 0; i < map.Items.Length; i++) { var item = map.Items[i]; MyObjectBuilderType type; if (item.TypeId != null && MyObjectBuilderType.TryParse(item.TypeId, out type)) { if (!typeof(MyObjectBuilder_BotDefinition).IsAssignableFrom((Type)type) && !typeof(MyObjectBuilder_VoxelMapStorageDefinition).IsAssignableFrom((Type)type) && !typeof(MyObjectBuilder_EnvironmentItems).IsAssignableFrom((Type)type)) { MyLog.Default.WriteLine(String.Format("Object builder type {0} is not supported for environment items.", item.TypeId)); Items[i].Frequency = 0; // This should disable this item } else { Items[i] = new MyMaterialEnvironmentItem() { Definition = new MyDefinitionId(type, item.SubtypeId), Frequency = map.Items[i].Density, IsDetail = map.Items[i].IsDetail, IsBot = typeof(MyObjectBuilder_BotDefinition).IsAssignableFrom((Type)type), IsVoxel = typeof(MyObjectBuilder_VoxelMapStorageDefinition).IsAssignableFrom((Type)type), IsEnvironemntItem = typeof(MyObjectBuilder_EnvironmentItems).IsAssignableFrom((Type)type), BaseColor = map.Items[i].BaseColor, ColorSpread = map.Items[i].ColorSpread, MaxRoll = (float)Math.Cos(MathHelper.ToDegrees(map.Items[i].MaxRoll)), Offset = map.Items[i].Offset, GroupId = map.Items[i].GroupId, GroupIndex = map.Items[i].GroupIndex, ModifierId = map.Items[i].ModifierId, ModifierIndex = map.Items[i].ModifierIndex }; } } else { MyLog.Default.WriteLine(String.Format("Object builder type {0} does not exist.", item.TypeId)); Items[i].Frequency = 0; // This should disable this item } } ComputeDistribution(); }
public MyPlanetEnvironmentMapping(PlanetEnvironmentItemMapping map) { this.Rule = map.Rule; this.Items = new MyMaterialEnvironmentItem[map.Items.Length]; if (this.Items.Length == 0) { this.CumulativeIntervals = null; this.TotalFrequency = 0f; } else { this.TotalFrequency = 0f; for (int i = 0; i < map.Items.Length; i++) { MyObjectBuilderType type; MyPlanetEnvironmentItemDef def = map.Items[i]; if ((def.TypeId == null) || !MyObjectBuilderType.TryParse(def.TypeId, out type)) { MyLog.Default.WriteLine($"Object builder type {def.TypeId} does not exist."); this.Items[i].Frequency = 0f; } else if ((!typeof(MyObjectBuilder_BotDefinition).IsAssignableFrom((Type)type) && !typeof(MyObjectBuilder_VoxelMapStorageDefinition).IsAssignableFrom((Type)type)) && !typeof(MyObjectBuilder_EnvironmentItems).IsAssignableFrom((Type)type)) { MyLog.Default.WriteLine($"Object builder type {def.TypeId} is not supported for environment items."); this.Items[i].Frequency = 0f; } else { MyMaterialEnvironmentItem item1 = new MyMaterialEnvironmentItem(); item1.Definition = new MyDefinitionId(type, def.SubtypeId); item1.Frequency = map.Items[i].Density; item1.IsDetail = map.Items[i].IsDetail; item1.IsBot = typeof(MyObjectBuilder_BotDefinition).IsAssignableFrom((Type)type); item1.IsVoxel = typeof(MyObjectBuilder_VoxelMapStorageDefinition).IsAssignableFrom((Type)type); item1.IsEnvironemntItem = typeof(MyObjectBuilder_EnvironmentItems).IsAssignableFrom((Type)type); item1.BaseColor = map.Items[i].BaseColor; item1.ColorSpread = map.Items[i].ColorSpread; item1.MaxRoll = (float)Math.Cos((double)MathHelper.ToDegrees(map.Items[i].MaxRoll)); item1.Offset = map.Items[i].Offset; item1.GroupId = map.Items[i].GroupId; item1.GroupIndex = map.Items[i].GroupIndex; item1.ModifierId = map.Items[i].ModifierId; item1.ModifierIndex = map.Items[i].ModifierIndex; this.Items[i] = item1; } } this.ComputeDistribution(); } }
// Prepare an environment definition from legacy planet definitions. public static MyWorldEnvironmentDefinition FromLegacyPlanet(MyObjectBuilder_PlanetGeneratorDefinition pgdef, MyModContext context) { var envOb = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_ProceduralWorldEnvironment>(pgdef.Id.SubtypeId);; envOb.Id = new SerializableDefinitionId(envOb.TypeId, envOb.SubtypeName); // storage var staticModule = new SerializableDefinitionId(typeof(MyObjectBuilder_ProceduralEnvironmentModuleDefinition), "Static"); var memoryModule = new SerializableDefinitionId(typeof(MyObjectBuilder_ProceduralEnvironmentModuleDefinition), "Memory"); // proxies var breakable = new SerializableDefinitionId(typeof(MyObjectBuilder_EnvironmentModuleProxyDefinition), "Breakable"); var voxelmap = new SerializableDefinitionId(typeof(MyObjectBuilder_EnvironmentModuleProxyDefinition), "VoxelMap"); var botSpawner = new SerializableDefinitionId(typeof(MyObjectBuilder_EnvironmentModuleProxyDefinition), "BotSpawner"); // TODO: Implement environmental particles. var environmentalParticleMarker = new SerializableDefinitionId(typeof(MyObjectBuilder_EnvironmentModuleProxyDefinition), "EnvironmentalParticles"); envOb.ItemTypes = new[] { new MyEnvironmentItemTypeDefinition() { LodFrom = -1, Name = "Tree", Provider = staticModule, Proxies = new [] { breakable } }, new MyEnvironmentItemTypeDefinition() { LodFrom = 0, Name = "Bush", Provider = staticModule, Proxies = new [] { breakable } }, new MyEnvironmentItemTypeDefinition() { LodFrom = 0, Name = "VoxelMap", Provider = memoryModule, Proxies = new [] { voxelmap } }, new MyEnvironmentItemTypeDefinition() { LodFrom = 0, Name = "Bot", Provider = null, Proxies = new [] { botSpawner } }, }; envOb.ScanningMethod = MyProceduralScanningMethod.Random; envOb.ItemsPerSqMeter = 0.0017; envOb.MaxSyncLod = 0; envOb.SectorSize = 384; List <MyProceduralEnvironmentMapping> mappings = new List <MyProceduralEnvironmentMapping>(); List <MyEnvironmentItemInfo> items = new List <MyEnvironmentItemInfo>(); var defaultRule = new MyPlanetSurfaceRule(); if (pgdef.EnvironmentItems != null) { foreach (var matmap in pgdef.EnvironmentItems) { var map = new MyProceduralEnvironmentMapping(); map.Biomes = matmap.Biomes; map.Materials = matmap.Materials; var rule = matmap.Rule ?? defaultRule; map.Height = rule.Height; map.Latitude = rule.Latitude; map.Longitude = rule.Longitude; map.Slope = rule.Slope; items.Clear(); foreach (var item in matmap.Items) { var it = new MyEnvironmentItemInfo { Density = item.Density, Subtype = MyStringHash.GetOrCompute(item.SubtypeId) }; switch (item.TypeId) { case "MyObjectBuilder_DestroyableItems": it.Type = "Bush"; break; case "MyObjectBuilder_Trees": it.Type = "Tree"; break; case "MyObjectBuilder_VoxelMapStorageDefinition": it.Type = "VoxelMap"; if (item.SubtypeId == null) { var subtype = MyStringHash.GetOrCompute(string.Format("G({0})M({1})", item.GroupId, item.ModifierId)); var vcolDef = MyDefinitionManager.Static.GetDefinition <MyVoxelMapCollectionDefinition>(subtype); if (vcolDef == null) { var vdefOb = MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_VoxelMapCollectionDefinition>(subtype.ToString()); vdefOb.Id = new SerializableDefinitionId(vdefOb.TypeId, vdefOb.SubtypeName); vdefOb.StorageDefs = new MyObjectBuilder_VoxelMapCollectionDefinition.VoxelMapStorage[1] { new MyObjectBuilder_VoxelMapCollectionDefinition.VoxelMapStorage() { Storage = item.GroupId } }; vdefOb.Modifier = item.ModifierId; vcolDef = new MyVoxelMapCollectionDefinition(); vcolDef.Init(vdefOb, context); MyDefinitionManager.Static.Definitions.AddDefinition(vcolDef); } it.Subtype = subtype; } break; default: MyLog.Default.Error("Planet Generator {0}: Invalid Item Type: {1}", pgdef.SubtypeName, item.SubtypeId); continue; break; } if (it.Subtype == null) { MyLog.Default.Error("Planet Generator {0}: Missing subtype for item of type {1}", pgdef.SubtypeName, it.Type); continue; } items.Add(it); } map.Items = items.ToArray(); mappings.Add(map); } } mappings.Capacity = mappings.Count; envOb.EnvironmentMappings = mappings.GetInternalArray(); var def = new MyProceduralEnvironmentDefinition(); def.Context = context; def.Init(envOb); return(def); }