public void EnsureMapFullyLoaded() { int chunksize = api.World.BlockAccessor.ChunkSize; nowVisible.Clear(); nowHidden.Clear(); Cuboidi chunkviewBounds = CurrentBlockViewBounds.ToCuboidi(); chunkviewBounds.Div(chunksize); BlockPos cur = new BlockPos().Set(chunkviewBounds.X1, 0, chunkviewBounds.Z1); bool beforeBoundsEmpty = chunkViewBoundsBefore.SizeX == 0 && chunkViewBoundsBefore.SizeZ == 0; while (cur.X <= chunkviewBounds.X2) { cur.Z = chunkviewBounds.Z1; while (cur.Z <= chunkviewBounds.Z2) { if (beforeBoundsEmpty || !chunkViewBoundsBefore.ContainsOrTouches(cur)) { nowVisible.Add(new Vec2i(cur.X, cur.Z)); } cur.Z++; } cur.X++; } cur.Set(chunkViewBoundsBefore.X1, 0, chunkViewBoundsBefore.Z1); while (cur.X <= chunkViewBoundsBefore.X2) { cur.Z = chunkViewBoundsBefore.Z1; while (cur.Z <= chunkViewBoundsBefore.Z2) { if (!chunkviewBounds.ContainsOrTouches(cur)) { nowHidden.Add(new Vec2i(cur.X, cur.Z)); } cur.Z++; } cur.X++; } chunkViewBoundsBefore = chunkviewBounds.Clone(); if (nowHidden.Count > 0 || nowVisible.Count > 0) { viewChanged(nowVisible, nowHidden); } }
public void EnsureMapFullyLoaded() { int chunksize = api.World.BlockAccessor.ChunkSize; nowVisible.Clear(); nowHidden.Clear(); Cuboidi worldBounds = CurrentViewBounds.ToCuboidi(); worldBounds.Div(chunksize); //worldBounds.GrowBy(1); BlockPos cur = new BlockPos().Set(worldBounds.X1, 0, worldBounds.Z1); // StringBuilder debug = new StringBuilder(); while (cur.X <= worldBounds.X2) { cur.Z = worldBounds.Z1; while (cur.Z <= worldBounds.Z2) { if (!worldBoundsBefore.ContainsOrTouches(cur)) { nowVisible.Add(new Vec2i(cur.X, cur.Z)); // debug.Append(string.Format("{0}/{1}, ", cur.X, cur.Z)); } cur.Z++; } cur.X++; } // if (nowVisible.Count > 0) Console.WriteLine("{0} chunks now visible: {1}", nowVisible.Count, debug.ToString()); cur.Set(worldBoundsBefore.X1, 0, worldBoundsBefore.Z1); while (cur.X <= worldBoundsBefore.X2) { cur.Z = worldBoundsBefore.Z1; while (cur.Z <= worldBoundsBefore.Z2) { if (!worldBounds.ContainsOrTouches(cur)) { nowHidden.Add(new Vec2i(cur.X, cur.Z)); } cur.Z++; } cur.X++; } worldBoundsBefore = worldBounds.Clone(); viewChanged(nowVisible, nowHidden); }
public bool TryGenerate(IBlockAccessor blockAccessor, IWorldAccessor worldForCollectibleResolve, BlockPos pos, int climateUpLeft, int climateUpRight, int climateBotLeft, int climateBotRight, DidGenerate didGenerateStructure) { this.climateUpLeft = climateUpLeft; this.climateUpRight = climateUpRight; this.climateBotLeft = climateBotLeft; this.climateBotRight = climateBotRight; float cnt = QuantityStructures.nextFloat(); int minQuantity = (int)cnt; BlockPos schemPos = pos.Copy(); Cuboidi location = new Cuboidi(); rand.InitPositionSeed(pos.X, pos.Z); List <GeneratableStructure> generatables = new List <GeneratableStructure>(); while (cnt-- > 0) { if (cnt < 1 && rand.NextDouble() > cnt) { break; } int tries = 30; while (tries-- > 0) { schemPos.Set(pos); schemPos.Add(rand.NextInt(50) - 25, 0, rand.NextInt(50) - 25); schemPos.Y = blockAccessor.GetTerrainMapheightAt(schemPos); double rndVal = rand.NextDouble() * totalWeight; int i = 0; VillageSchematic schem = null; while (rndVal > 0) { schem = Schematics[i++]; rndVal -= schem.Weight; } BlockSchematicStructure struc = GetGeneratableStructure(schem, blockAccessor, worldForCollectibleResolve, schemPos); if (struc != null) { location.Set(schemPos.X, schemPos.Y, schemPos.Z, schemPos.X + struc.SizeX, schemPos.Y + struc.SizeY, schemPos.Z + struc.SizeZ); bool intersect = false; for (int k = 0; k < generatables.Count; k++) { if (location.IntersectsOrTouches(generatables[k].location)) { intersect = true; break; } } if (!intersect) { generatables.Add(new GeneratableStructure() { struc = struc, pos = schemPos.Copy(), location = location.Clone() }); } break; } } } if (generatables.Count >= minQuantity) { foreach (var val in generatables) { val.struc.PlaceRespectingBlockLayers(blockAccessor, worldForCollectibleResolve, val.pos, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight, replaceblockids); didGenerateStructure(val.location, val.struc); } } return(true); }
private void DoGenStructures(IMapRegion region, int chunkX, int chunkZ, bool postPass, ITreeAttribute chunkGenParams = null) { BlockPos pos = new BlockPos(); ITreeAttribute chanceModTree = null; ITreeAttribute maxQuantityModTree = null; if (chunkGenParams?["structureChanceModifier"] != null) { chanceModTree = chunkGenParams["structureChanceModifier"] as TreeAttribute; } if (chunkGenParams?["structureMaxCount"] != null) { maxQuantityModTree = chunkGenParams["structureMaxCount"] as TreeAttribute; } strucRand.InitPositionSeed(chunkX, chunkZ); scfg.Structures.Shuffle(strucRand); for (int i = 0; i < scfg.Structures.Length; i++) { WorldGenStructure struc = scfg.Structures[i]; if (struc.PostPass != postPass) { continue; } float chance = struc.Chance * scfg.ChanceMultiplier; int toGenerate = 9999; if (chanceModTree != null) { chance *= chanceModTree.GetFloat(struc.Code, 0); } if (maxQuantityModTree != null) { toGenerate = maxQuantityModTree.GetInt(struc.Code, 9999); } while (chance-- > strucRand.NextDouble() && toGenerate > 0) { int dx = strucRand.NextInt(chunksize); int dz = strucRand.NextInt(chunksize); int ySurface = heightmap[dz * chunksize + dx]; if (ySurface <= 0 || ySurface >= worldheight - 15) { continue; } if (struc.Placement == EnumStructurePlacement.Underground) { if (struc.Depth != null) { pos.Set(chunkX * chunksize + dx, ySurface - (int)struc.Depth.nextFloat(1, strucRand), chunkZ * chunksize + dz); } else { pos.Set(chunkX * chunksize + dx, 8 + strucRand.NextInt(ySurface - 8 - 5), chunkZ * chunksize + dz); } } else { pos.Set(chunkX * chunksize + dx, ySurface, chunkZ * chunksize + dz); } if (struc.TryGenerate(worldgenBlockAccessor, api.World, pos, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight)) { Cuboidi loc = struc.LastPlacedSchematicLocation; string code = struc.Code + (struc.LastPlacedSchematic == null ? "" : "/" + struc.LastPlacedSchematic.FromFileName); region.GeneratedStructures.Add(new GeneratedStructure() { Code = code, Group = struc.Group, Location = loc.Clone() }); region.DirtyForSaving = true; if (struc.BuildProtected) { api.World.Claims.Add(new LandClaim() { Areas = new List <Cuboidi>() { loc.Clone() }, Description = struc.BuildProtectionDesc, ProtectionLevel = 10, LastKnownOwnerName = struc.BuildProtectionName, AllowUse = true }); } toGenerate--; } } } }