public virtual void AddTrees(ref IMapHandler mh, BiomeType[,] biomes, ref Random rand, int X, int Z, int H) { int xo = (int)(X * mh.ChunkScale.X); int zo = (int)(Z * mh.ChunkScale.Z); List<Vector2i> PlantedTrees = new List<Vector2i>(); int DistanceReqd = 3; for (int t = 0; t < (int)((HumidityNoise.Noise((double)(xo) / BIOME_SCALE, (double)(zo) / BIOME_SCALE, 0) + HumidityOffset) * 5.0); t++) { Vector2i me = new Vector2i(rand.Next(0, 15),rand.Next(0, 15)); if (!Biome.NeedsTrees(biomes[me.X, me.Y])) continue; bool tooclose=false; foreach (Vector2i tree in PlantedTrees) { if (Vector2i.Distance(tree, me) < DistanceReqd) { tooclose = true; break; } } if (tooclose) continue; bool founddert = false; for (int y = (int)H - 10; y > 0; y--) { switch (mh.GetBlockAt(me.X+xo, y, me.Y+zo)) { case 0: // Air case 78: // Snow cover continue; // case 1: // ROCK case 2: // GRASS case 3: // DIRT //Utils.GrowTree(ref blocks, rand, (int)me.X, (int)y + 1, (int)me.Y); mh.SetBlockAt(me.X + xo, y + 1, me.Y + zo, 6); // Sapling mh.SetDataAt(me.X + xo, y + 1, me.Y + zo, 15); // Growth stage 15. /* Tree tree = new NormalTree(me.X + xo, y + 1, me.Y + zo, rand.Next(5, 8)); tree.MakeTrunk(ref mh); tree.MakeFoliage(ref mh); */ mh.SaveAll(); founddert = true; break; case 11: // SAND //Utils.GrowCactus(ref b, rand, me.X, y + 1, me.Y); break; default: founddert = true; break; } if (founddert) break; } PlantedTrees.Add(me); } }
public Vector2i GetChunkCoordsFromFile(string file,bool test) { Vector2i r = new Vector2i(0, 0); // cX.Y.dat // X.Y.dat string[] peices =Path.GetFileName(file.Substring(1)).Split('.'); long X; long Y; Radix.Decode(peices[0],36,out X); Radix.Decode(peices[1],36,out Y); r.X = (int)X; r.Y = (int)Y; if (test) { NbtFile f = new NbtFile(file); try { f.LoadFile(); } catch (Exception e) { if (CorruptChunk != null) CorruptChunk(X,Y,e.ToString(), file); return null; } NbtCompound Level = (NbtCompound)f.RootTag["Level"]; Vector2i t = new Vector2i(0, 0); t.X = (Level["xPos"] as NbtInt).Value; t.Y = (Level["zPos"] as NbtInt).Value; f.Dispose(); if (t != r) throw new Exception("Test failed."); } return r; }
/// <summary> /// Pythagorean distance. /// </summary> /// <param name="tree"></param> /// <param name="me"></param> /// <returns></returns> public static double Distance(Vector2i a, Vector2i b) { return(Math.Sqrt(((a.X - b.X) ^ 2) + ((a.Y - b.Y) ^ 2))); }
internal override Vector2i _GetChunkCoordsFromFile(string file) { Vector2i r = new Vector2i(0, 0); NbtFile f = new NbtFile(file); try { f.LoadFile(); } catch (Exception e) { if (CorruptChunk != null) CorruptChunk(0,0,e.ToString(), file); return null; } NbtCompound Level = (NbtCompound)f.RootTag["Level"]; Vector2i t = new Vector2i(0, 0); r.X = (Level["xPos"] as NbtInt).Value; r.Y = (Level["zPos"] as NbtInt).Value; f.Dispose(); return r; }
internal Vector2i GetChunkCoords(string file) { Vector2i pos=null; if (mFileCoords.ContainsKey(file)) { Vector3i coords = mFileCoords[file]; return new Vector2i((int)coords.X, (int)coords.Y); // Z is dimension } Console.WriteLine("[CACHE] Couldn't find file " + file + " in the coordinate cache. Trying database..."); try { rwLock.AcquireReaderLock(300); using (SQLiteCommand cmd = database.CreateCommand()) { cmd.CommandText = "SELECT cnkX,cnkZ FROM Chunks WHERE cnkFile='" + file + "';"; SQLiteDataReader rdr = cmd.ExecuteReader(); if (rdr.HasRows) { pos = new Vector2i( (int)((long)rdr["cnkX"]), (int)((long)rdr["cnkZ"]) ); } } } finally { if (rwLock.IsReaderLockHeld) rwLock.ReleaseReaderLock(); } return pos; }
public void UpdateCache() { TileEntities.Clear(); Entities.Clear(); SQLiteTransaction trans = database.BeginTransaction(); int numChunksChanged = 0; int numNewChunks = 0; int numChunksTotal = 0; //mThread = new Thread(delegate() //{ map.SetBusy("Updating cache..."); map.SetBusy(string.Format("Please wait, {0}...\r\n{1} new, {2} changed", (FirstCache) ? "creating cache (may take a while)" : "updating cache", numNewChunks, numChunksChanged)); Dimension[] dims = (Dimension[])map.GetDimensions(); Dimension dim = dims[map.Dimension]; map.ForEachChunkFile(dim.ID, delegate(IMapHandler _map, string file) { bool NeedsUpdate = false; bool NewChunk = false; using (SQLiteCommand cmd = database.CreateCommand()) { Vector2i pos; cmd.CommandText="SELECT cnkMD5,cnkX,cnkZ,dimID FROM Chunks WHERE cnkFile='" + file + "';"; SQLiteDataReader rdr = cmd.ExecuteReader(); if (!rdr.HasRows) { NewChunk = true; NeedsUpdate = true; pos = map.GetChunkCoordsFromFile(file,true); } else { pos=new Vector2i( (int)((long)rdr["cnkX"]), (int)((long)rdr["cnkZ"]) ); if(!mFileCoords.ContainsKey(file)) mFileCoords.Add(file,new Vector3i(pos.X,pos.Y, (int)((long)rdr["dimID"]))); } if (dim.MinimumChunk.X > pos.X) dim.MinimumChunk.X = pos.X; if (dim.MaximumChunk.X < pos.X) dim.MaximumChunk.X = pos.X; if (dim.MinimumChunk.Y > pos.Y) dim.MinimumChunk.Y = pos.Y; if (dim.MaximumChunk.Y < pos.Y) dim.MaximumChunk.Y = pos.Y; if (!NeedsUpdate) { if (rdr["cnkMD5"].ToString() != GetMD5HashFromFile(file)) NeedsUpdate = true; } if (NeedsUpdate) { if (NewChunk) numNewChunks++; else numChunksChanged++; if (pos == null) return; //Console.WriteLine(string.Format("Updating chunk {0} in {1} ({2})...", pos, dim.Name, file)); map.SetBusy(string.Format("Please wait, {0}...\r\n{1} new, {2} changed", (FirstCache) ? "creating cache (may take a while)" : "updating cache", numNewChunks, numChunksChanged)); Chunk c = map.GetChunk(pos.X, pos.Y); map.UnloadChunks(); } System.Windows.Forms.Application.DoEvents(); } // Dump it from RAM onto disk to prevent bloat. if (numChunksChanged + numNewChunks % 500 == 499) { Console.WriteLine("Saving cache..."); map.SetBusy(string.Format("Please wait, {0}...\r\n[Saving]", (FirstCache) ? "creating cache (may take a while)" : "updating cache")); trans.Commit(); trans = database.BeginTransaction(); map.UnloadChunks(); } }); UpdateDimension(dim); map.SetIdle(); trans.Commit(); // NOW save to disk. //}); //mThread.Start(); }
public IEnumerable<Vector2i> GetKnownChunks(int dimension) { List<Vector2i> known = new List<Vector2i>(); try { rwLock.AcquireReaderLock(300); using (SQLiteCommand cmd = database.CreateCommand()) { cmd.CommandText = "SELECT cnkX,cnkZ FROM Chunks WHERE dimID=" + dimension.ToString() + " ORDER BY cnkX,cnkZ;"; SQLiteDataReader rdr =cmd.ExecuteReader(); while (rdr.Read()) { Vector2i cpos = new Vector2i( (int)(long)rdr["cnkX"], (int)(long)rdr["cnkZ"] ); if (!known.Contains(cpos)) known.Add(cpos); } } } finally { if (rwLock.IsReaderLockHeld) rwLock.ReleaseReaderLock(); } return known; }