//PUBLIC METHODS /// <summary> /// Loads the specified heightmap from the a file. /// </summary> /// <param name="filename">filename to load</param> /// <param name="info">Heightmap info</param> /// <param name="heightmap">out heightmap</param> /// <returns>True/False heightmap can be loaded</returns> public static bool LoadFromFile(string filename, HeightMapInfo info, out HeightMap heightmap) { bool result = false; FileStream filestream = null; try { //OPEN THE FILESTREAM filestream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(filestream); //GENERATE NEW HEIGHTMAP HeightMap hmap = new HeightMap(); hmap.info = info; //GENERATE MAX X, Y VALUES int MaxX = hmap.info.size; int MaxY = hmap.info.size; //GENERATE X/Y ARRAY FOR hmap.HeightData = new float[MaxX, MaxY]; //READ ALL Z COORDS float size = (hmap.info.size / 2); for (int y = 0; y < MaxY; y++) { for (int x = 0; x < MaxX; x++) { hmap.HeightData[x, y] = (float)(((float)reader.ReadUInt16() - 32768) / 32768) * size; } } //CLOSE DOWN RESOURCES reader.Close(); filestream.Close(); heightmap = hmap; result = true; } catch (IOException) { //A IO-EXCEPTION OCCURED Trace.TraceError("Cannot load heightmap from file: {0}", filename); heightmap = null; result = false; } catch (OutOfMemoryException) { //WE RUN OUT OF MEMORY WHILE LOADING A MAP, THIS CAN BE THE RESULT OF CORRUPT MEMORY Trace.TraceError("Ran out of memory when attempting to load: {0}", filename); heightmap = null; result = false; } finally { //CLOSE DOWN RESOURCES if (filestream != null) filestream.Close(); } return result; }
protected void SetMembers(Zone zone, byte zoneid, HeightMap heightmap, ZoneType type, byte cathelaya_map, Point cathelaya_location, byte promise_map, Point promise_location, uint regioncode) { zone.Map = zoneid; zone.Regiontree = new Regiontree(); zone.Type = type; zone.Heightmap = heightmap; zone.CathelayaLocation = new WorldCoordinate(cathelaya_location, cathelaya_map); zone.ProsmiseLocation = new WorldCoordinate(promise_location, promise_map); zone.RegionCode = regioncode; }
//PUBLIC METHODS /// <summary> /// Loads the specified heightmap from the a file. /// </summary> /// <param name="filename">filename to load</param> /// <param name="info">Heightmap info</param> /// <param name="heightmap">out heightmap</param> /// <returns>True/False heightmap can be loaded</returns> public static bool LoadFromFile(string filename, HeightMapInfo info, out HeightMap heightmap) { bool result = false; FileStream filestream = null; try { //OPEN THE FILESTREAM filestream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(filestream); //GENERATE NEW HEIGHTMAP HeightMap hmap = new HeightMap(); hmap.info = info; //GENERATE MAX X, Y VALUES int MaxX = hmap.info.size; int MaxY = hmap.info.size; //GENERATE X/Y ARRAY FOR hmap.HeightData = new float[MaxX, MaxY]; //READ ALL Z COORDS float size = (hmap.info.size / 2); for (int y = 0; y < MaxY; y++) { for (int x = 0; x < MaxX; x++) { hmap.HeightData[x, y] = (float)(((float)reader.ReadUInt16() - 32768) / 32768) * size; } } //CLOSE DOWN RESOURCES reader.Close(); filestream.Close(); heightmap = hmap; result = true; } catch (IOException) { //A IO-EXCEPTION OCCURED Trace.TraceError("Cannot load heightmap from file: {0}", filename); heightmap = null; result = false; } catch (OutOfMemoryException) { //WE RUN OUT OF MEMORY WHILE LOADING A MAP, THIS CAN BE THE RESULT OF CORRUPT MEMORY Trace.TraceError("Ran out of memory when attempting to load: {0}", filename); heightmap = null; result = false; } finally { //CLOSE DOWN RESOURCES if (filestream != null) { filestream.Close(); } } return(result); }
/// <summary> /// Default included event that invokes a csv based stream. /// </summary> /// <param name="stream">Stream to read data from</param> /// <param name="ProgressReport">Class to report the state of reading</param> protected override void ParseAsCsvStream(Stream stream, FactoryBase.ReportProgress ProgressReport) { using (StreamReader c = new StreamReader(stream)) { c.ReadLine(); while (c.Peek() > 0) { //REPORT PROGRESS ProgressReport.Invoke(); String row = c.ReadLine(); String[] fields = row.Split(','); //LOAD A HEIGHTMAP HeightMap heightmap = new HeightMap(); HeightMap.HeightMapInfo info = new HeightMap.HeightMapInfo(); try { //FILL OUT HEIGHTMAP INFORMATION string filename = Path.Combine(Environment.CurrentDirectory, dirHeightmap); if (fields[10].Length > 0) { info.location[0] = float.Parse(fields[12], NumberFormatInfo.InvariantInfo); info.location[1] = float.Parse(fields[13], NumberFormatInfo.InvariantInfo); info.location[2] = float.Parse(fields[14], NumberFormatInfo.InvariantInfo); info.scale[0] = int.Parse(fields[15], NumberFormatInfo.InvariantInfo); info.scale[1] = int.Parse(fields[16], NumberFormatInfo.InvariantInfo); info.scale[2] = int.Parse(fields[17], NumberFormatInfo.InvariantInfo); info.size = int.Parse(fields[11], NumberFormatInfo.InvariantInfo); filename = Path.Combine(filename, fields[10]); //IF HEIGHTMAP IS NOT LOADED PROCEED HeightMap.LoadFromFile(filename, info, out heightmap); } float catheleyax = float.Parse(fields[2], NumberFormatInfo.InvariantInfo); float catheleyay = float.Parse(fields[3], NumberFormatInfo.InvariantInfo); float catheleyaz = float.Parse(fields[4], NumberFormatInfo.InvariantInfo); float promisex = float.Parse(fields[6], NumberFormatInfo.InvariantInfo); float promisey = float.Parse(fields[7], NumberFormatInfo.InvariantInfo); float promizez = float.Parse(fields[8], NumberFormatInfo.InvariantInfo); byte catheleyamap = byte.Parse(fields[5], NumberFormatInfo.InvariantInfo); byte promisemap = byte.Parse(fields[9], NumberFormatInfo.InvariantInfo); uint regioncode = uint.Parse(fields[19], NumberFormatInfo.InvariantInfo); uint zoneid = uint.Parse(fields[0], NumberFormatInfo.InvariantInfo); ZoneType zonetype = (ZoneType)Enum.Parse(typeof(ZoneType), fields[18], true); Zone zone; if (TryFindZoneString(fields[1], out zone)) { SetMembers(zone, (byte)zoneid, heightmap, zonetype, catheleyamap, new Point(catheleyax, catheleyay, catheleyaz), promisemap, new Point(promisex, promisey, promizez), regioncode); maps.Add(zoneid, zone); } } catch (Exception e) { HostContext.AddUnhandeldException(e); } } } }