private void cmdSnowAccum(IServerPlayer player, int groupId, CmdArgs args) { WeatherSystemServer wsys = api.ModLoader.GetModSystem <WeatherSystemServer>(); string cmd = args.PopWord(); if (cmd == "on") { wsys.snowSimSnowAccu.ProcessChunks = true; player.SendMessage(groupId, "Snow accum process chunks on", EnumChatType.CommandSuccess); return; } if (cmd == "off") { wsys.snowSimSnowAccu.ProcessChunks = false; player.SendMessage(groupId, "Snow accum process chunks off", EnumChatType.CommandSuccess); return; } if (cmd == "processhere") { BlockPos plrPos = player.Entity.Pos.AsBlockPos; int chunksize = api.World.BlockAccessor.ChunkSize; Vec2i chunkPos = new Vec2i(plrPos.X / chunksize, plrPos.Z / chunksize); wsys.snowSimSnowAccu.AddToCheckQueue(chunkPos); player.SendMessage(groupId, "Ok, added to check queue", EnumChatType.CommandSuccess); return; } if (cmd == "info") { BlockPos plrPos = player.Entity.Pos.AsBlockPos; int chunksize = api.World.BlockAccessor.ChunkSize; Vec2i chunkPos = new Vec2i(plrPos.X / chunksize, plrPos.Z / chunksize); IServerMapChunk mc = sapi.WorldManager.GetMapChunk(chunkPos.X, chunkPos.Y); byte[] data = mc.GetData("lastSnowAccumUpdateTotalHours"); double lastSnowAccumUpdateTotalHours = data == null ? 0 : SerializerUtil.Deserialize <double>(data); player.SendMessage(groupId, "lastSnowAccumUpdateTotalHours: " + lastSnowAccumUpdateTotalHours, EnumChatType.CommandSuccess); int regionX = (int)player.Entity.Pos.X / sapi.World.BlockAccessor.RegionSize; int regionZ = (int)player.Entity.Pos.Z / sapi.World.BlockAccessor.RegionSize; WeatherSystemServer wsysServer = sapi.ModLoader.GetModSystem <WeatherSystemServer>(); long index2d = wsysServer.MapRegionIndex2D(regionX, regionZ); WeatherSimulationRegion simregion; wsysServer.weatherSimByMapRegion.TryGetValue(index2d, out simregion); int reso = WeatherSimulationRegion.snowAccumResolution; SnowAccumSnapshot sumsnapshot = new SnowAccumSnapshot() { //SumTemperatureByRegionCorner = new API.FloatDataMap3D(reso, reso, reso), SnowAccumulationByRegionCorner = new API.FloatDataMap3D(reso, reso, reso) }; float[] sumdata = sumsnapshot.SnowAccumulationByRegionCorner.Data; // Can't grow bigger than one full snow block float max = 3 + 0.5f; int len = simregion.SnowAccumSnapshots.Length; int i = simregion.SnowAccumSnapshots.Start; // This code here causes wacky snow patterns // The lerp itself is fine!!! while (len-- > 0) { SnowAccumSnapshot hoursnapshot = simregion.SnowAccumSnapshots[i]; i = (i + 1) % simregion.SnowAccumSnapshots.Length; float[] snowaccumdata = hoursnapshot.SnowAccumulationByRegionCorner.Data; for (int j = 0; j < snowaccumdata.Length; j++) { sumdata[j] = GameMath.Clamp(sumdata[j] + snowaccumdata[j], -max, max); } lastSnowAccumUpdateTotalHours = Math.Max(lastSnowAccumUpdateTotalHours, hoursnapshot.TotalHours); } for (int j = 0; j < sumdata.Length; j++) { player.SendMessage(groupId, j + ": " + sumdata[j], EnumChatType.CommandSuccess); } return; } if (cmd == "here") { float amount = (float)args.PopFloat(0); BlockPos plrPos = player.Entity.Pos.AsBlockPos; int chunksize = api.World.BlockAccessor.ChunkSize; Vec2i chunkPos = new Vec2i(plrPos.X / chunksize, plrPos.Z / chunksize); IServerMapChunk mc = sapi.WorldManager.GetMapChunk(chunkPos.X, chunkPos.Y); int reso = WeatherSimulationRegion.snowAccumResolution; SnowAccumSnapshot sumsnapshot = new SnowAccumSnapshot() { SumTemperatureByRegionCorner = new API.FloatDataMap3D(reso, reso, reso), SnowAccumulationByRegionCorner = new API.FloatDataMap3D(reso, reso, reso) }; sumsnapshot.SnowAccumulationByRegionCorner.Data.Fill(amount); var updatepacket = wsys.snowSimSnowAccu.UpdateSnowLayer(sumsnapshot, true, mc, chunkPos); wsys.snowSimSnowAccu.accum = 1f; var ba = sapi.World.GetBlockAccessorBulkMinimalUpdate(true, false); ba.UpdateSnowAccumMap = false; wsys.snowSimSnowAccu.processBlockUpdates(chunkPos, updatepacket, ba); ba.Commit(); player.SendMessage(groupId, "Ok, test snow accum gen complete", EnumChatType.CommandSuccess); return; } }
public void UpdateSnowLayerOffThread(WeatherSimulationRegion simregion, IServerMapChunk mc, Vec2i chunkPos) { #region Tyrons brain cloud // Trick 1: Each x/z coordinate gets a "snow accum" threshold by using a locational random (murmurhash3). Once that threshold is reached, spawn snow. If its doubled, spawn 2nd layer of snow. => Patchy "fade in" of snow \o/ // Trick 2: We store a region wide snow accum value for the ground level and the map ceiling level. We can now interpolate between those values for each Y-Coordinate \o/ // Trick 3: We loop through each x/z block in a separate thread, then hand over "place snow" tasks to the main thread // Trick 4: Lets pre-gen 50 random shuffles for every x/z coordinate of a chunk. Loop through the region chunks, check which one is loaded and select one random shuffle from the list, then iterate over every x/z coord // Trick 5: Snowed over blocks: // - New VSMC util: "Automatically Try to add a snow cover to all horizontal faces" // - New Block property: SnowCoverableShape. // - Block.OnJsonTesselation adds snow adds cover shape to the sourceMesh!! // Trick 6: Turn Cloud Patterns into a "dumb slave system". They are visual information only, so lets make them follow internal mechanisms. // - Create a precipitation perlin noise generator. If the precipitation value goes above or below a certain value, we force the cloud pattern system to adapt to a fitting pattern // => We gain easy to probe, deterministic precipitation values!! // => We gain the ability to do unloaded chunk snow accumulation and unloaded chunk farmland rain-wetness accum // Trick 6 v2.0: // Rain clouds are simply overlaid onto the normal clouds. // Questions: // - Q1: When should it hail now? // - Q2: How is particle size determined? // - Q3: When should there be thunder? // - Q4: How to control the precipitation by command? // A1/A3: What if we read the slope of precipitation change. If there is a drastic increase of rain fall launch a // a. wind + thunder event // b. thunder event // c. rarely a hail event // d. extra rarely thunder + hail event // A2: Particle size is determiend by precipitation intensity // Trick 7 v2.0 // - Hail and Thunder are also triggered by a perlin noise generator. That way I don't need to care about event range. // A4: /weather setprecip [auto or 0..1] // - Q5: How do we overlay rain clouds onto the normal clouds? // Q5a: Will they be hardcoded? Or configurable? // Q5b: How does the overlay work? Lerp? // Q5c: Rain cloud intensity should relate to precip level. // How? Lerp from zero to max rain clouds? Multiple cloud configs and lerp between them? // - A5a: Configurable // A5b: Lerp. // A5c: Single max rain cloud config seems sufficient // TODO: // 1. Rain cloud overlay // 2. Snow accum // 3. Hail, Thunder perlin noise // 4. Done? // Idea 8: // - F**K the region based weather sim. // - Generate clouds patterns like you generate terrain from landforms // - Which is grid based indices, neatly abstracted with LerpedIndex2DMap and nicely shaped with domain warping // - Give it enough padding to ensure domain warping does not go out of bounds // - Every 2-3 minutes regenerate this map in a seperate thread, cloud renderer lerps between old and new map. // - Since the basic indices input is grid based, we can cycle those individually through time // for a future version // Hm. Maybe one noise generator for cloud coverage? // => Gain the ability to affect local temperature based on cloud coverage // Hm. Or maybe one noise generator for each cloud pattern? // => Gain the abillity for small scale and very large scale cloud patterns // Maybe even completely ditch per-region simulation? // => Gain the ability for migrating weather patterns // but then what will determine the cloud pattern? // Region-less Concept: // Take an LCGRandom. Use xpos and zpos+((int)totalDays) / 5 for coords // Iterate over every player // - iterate over a 20x20 chunk area around it (or max view dist + 5 chunks) // - domain warp x/z coords. use those coords to init position seed on lcgrand. get random value // - store in an LerpedWeightedIndex2DMap // Iterate over every cloud tile // - read cloud pattern data from the map // Snow accum needs to take the existing world information into account, i.e. current snow level // We should probably // - Store snow accumulation as a float value in mapchunkdata as Dictionary<BlockPos, float> // - Every 3 seconds or so, "commit" that snow accum into actual snow layer blocks, i.e. if accum >= 1 then add one snow layer and do accum-=1 #endregion UpdateSnowLayerChunk ch = new UpdateSnowLayerChunk() { Coords = chunkPos }; // Lets wait until we're done with the current job for this chunk if (updateSnowLayerQueue.Contains(ch)) { return; } double nowTotalHours = ws.api.World.Calendar.TotalHours; if (nowTotalHours - simregion.LastUpdateTotalHours > 1) // Lets wait until WeatherSimulationRegion is done updating { return; } byte[] data = mc.GetData("lastSnowAccumUpdateTotalHours"); double lastSnowAccumUpdateTotalHours = data == null ? 0 : SerializerUtil.Deserialize <double>(data); double startTotalHours = lastSnowAccumUpdateTotalHours; int reso = WeatherSimulationRegion.snowAccumResolution; SnowAccumSnapshot sumsnapshot = new SnowAccumSnapshot() { //SumTemperatureByRegionCorner = new API.FloatDataMap3D(reso, reso, reso), SnowAccumulationByRegionCorner = new API.FloatDataMap3D(reso, reso, reso) }; float[] sumdata = sumsnapshot.SnowAccumulationByRegionCorner.Data; if (simregion == null) { return; } // Can't grow bigger than one full snow block float max = ws.GeneralConfig.SnowLayerBlocks.Count + 0.5f; int len = simregion.SnowAccumSnapshots.Length; int i = simregion.SnowAccumSnapshots.Start; int newCount = 0; lock (WeatherSimulationRegion.lockTest) { while (len-- > 0) { SnowAccumSnapshot hoursnapshot = simregion.SnowAccumSnapshots[i]; i = (i + 1) % simregion.SnowAccumSnapshots.Length; if (hoursnapshot == null || lastSnowAccumUpdateTotalHours >= hoursnapshot.TotalHours) { continue; } float[] snowaccumdata = hoursnapshot.SnowAccumulationByRegionCorner.Data; for (int j = 0; j < snowaccumdata.Length; j++) { sumdata[j] = GameMath.Clamp(sumdata[j] + snowaccumdata[j], -max, max); } lastSnowAccumUpdateTotalHours = Math.Max(lastSnowAccumUpdateTotalHours, hoursnapshot.TotalHours); newCount++; } } if (newCount == 0) { return; } bool ignoreOldAccum = false; if (lastSnowAccumUpdateTotalHours - startTotalHours >= sapi.World.Calendar.DaysPerYear * sapi.World.Calendar.HoursPerDay) { ignoreOldAccum = true; } ch = UpdateSnowLayer(sumsnapshot, ignoreOldAccum, mc, chunkPos); if (ch != null) { //Console.WriteLine("{0} snaps used for {1}/{2}", newCount, chunkPos.X, chunkPos.Y); ch.LastSnowAccumUpdateTotalHours = lastSnowAccumUpdateTotalHours; ch.Coords = chunkPos.Copy(); lock (updateSnowLayerQueueLock) { updateSnowLayerQueue.Enqueue(ch); } } }