diff options
author | Robert Adams | 2013-10-02 16:53:12 -0700 |
---|---|---|
committer | Robert Adams | 2013-10-07 13:57:16 -0700 |
commit | 9b150194f698f1b450458b50f8d51b6e5d010bcc (patch) | |
tree | 66f46b3af6cbb5d0796737cb794977ac2a9b10f8 /OpenSim/Framework/TerrainData.cs | |
parent | Merge branch 'varregion' of git://opensimulator.org/git/opensim into varregion (diff) | |
download | opensim-SC_OLD-9b150194f698f1b450458b50f8d51b6e5d010bcc.zip opensim-SC_OLD-9b150194f698f1b450458b50f8d51b6e5d010bcc.tar.gz opensim-SC_OLD-9b150194f698f1b450458b50f8d51b6e5d010bcc.tar.bz2 opensim-SC_OLD-9b150194f698f1b450458b50f8d51b6e5d010bcc.tar.xz |
varregion: add new TerrainData and TerrainCompressor routines. TerrainCompressor needed to replace the one in libopenmetaverse that doesn't know about the larger terrain packets.
Diffstat (limited to 'OpenSim/Framework/TerrainData.cs')
-rw-r--r-- | OpenSim/Framework/TerrainData.cs | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/OpenSim/Framework/TerrainData.cs b/OpenSim/Framework/TerrainData.cs new file mode 100644 index 0000000..8cb1aef --- /dev/null +++ b/OpenSim/Framework/TerrainData.cs | |||
@@ -0,0 +1,152 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | |||
32 | using OpenMetaverse; | ||
33 | |||
34 | namespace OpenSim.Framework | ||
35 | { | ||
36 | public abstract class TerrainData | ||
37 | { | ||
38 | // Terrain always is a square | ||
39 | public int SizeX { get; protected set; } | ||
40 | public int SizeY { get; protected set; } | ||
41 | public int SizeZ { get; protected set; } | ||
42 | |||
43 | public abstract float this[int x, int y] { get; set; } | ||
44 | // Someday terrain will have caves | ||
45 | public abstract float this[int x, int y, int z] { get; set; } | ||
46 | |||
47 | // Return a representation of this terrain for storing as a blob in the database. | ||
48 | // Returns 'true' to say blob was stored in the 'out' locations. | ||
49 | public abstract bool GetDatabaseBlob(out int DBFormatRevisionCode, out Array blob); | ||
50 | } | ||
51 | |||
52 | // The terrain is stored as a blob in the database with a 'revision' field. | ||
53 | // Some implementations of terrain storage would fill the revision field with | ||
54 | // the time the terrain was stored. When real revisions were added and this | ||
55 | // feature removed, that left some old entries with the time in the revision | ||
56 | // field. | ||
57 | // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is | ||
58 | // left over and it is presumed to be 'Legacy256'. | ||
59 | // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation. | ||
60 | // If a revision does not match any of these, it is assumed to be Legacy256. | ||
61 | public enum DBTerrainRevision | ||
62 | { | ||
63 | // Terrain is 'double[256,256]' | ||
64 | Legacy256 = 11, | ||
65 | // Terrain is 'int32, int32, float[,]' where the shorts are X and Y dimensions | ||
66 | // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256. | ||
67 | Variable2D = 22, | ||
68 | // A revision that is not listed above or any revision greater than this value is 'Legacy256'. | ||
69 | RevisionHigh = 1234 | ||
70 | } | ||
71 | |||
72 | // Version of terrain that is a heightmap. | ||
73 | // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge | ||
74 | // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer. | ||
75 | public class HeightmapTerrainData : TerrainData | ||
76 | { | ||
77 | // TerrainData.this[x, y] | ||
78 | public override float this[int x, int y] | ||
79 | { | ||
80 | get { return m_heightmap[x * SizeX + y]; } | ||
81 | set { m_heightmap[x * SizeX + y] = value; } | ||
82 | } | ||
83 | |||
84 | // TerrainData.this[x, y, z] | ||
85 | public override float this[int x, int y, int z] | ||
86 | { | ||
87 | get { return this[x, y]; } | ||
88 | set { this[x, y] = value; } | ||
89 | } | ||
90 | |||
91 | // TerrainData.GetDatabaseBlob | ||
92 | // The user wants something to store in the database. | ||
93 | public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob) | ||
94 | { | ||
95 | DBRevisionCode = (int)DBTerrainRevision.Legacy256; | ||
96 | blob = LegacyTerrainSerialization(); | ||
97 | return false; | ||
98 | } | ||
99 | private float[] m_heightmap; | ||
100 | |||
101 | // To keep with the legacy theme, this can be created with the way terrain | ||
102 | // used to passed around as. | ||
103 | public HeightmapTerrainData(double[,] pTerrain) | ||
104 | { | ||
105 | SizeX = pTerrain.GetLength(0); | ||
106 | SizeY = pTerrain.GetLength(1); | ||
107 | SizeZ = (int)Constants.RegionHeight; | ||
108 | |||
109 | int idx = 0; | ||
110 | m_heightmap = new float[SizeX * SizeY]; | ||
111 | for (int ii = 0; ii < SizeX; ii++) | ||
112 | { | ||
113 | for (int jj = 0; jj < SizeY; jj++) | ||
114 | { | ||
115 | m_heightmap[idx++] = (float)pTerrain[ii, jj]; | ||
116 | |||
117 | } | ||
118 | } | ||
119 | } | ||
120 | |||
121 | public HeightmapTerrainData(float[] pHeightmap, int pX, int pY, int pZ) | ||
122 | { | ||
123 | m_heightmap = pHeightmap; | ||
124 | SizeX = pX; | ||
125 | SizeY = pY; | ||
126 | SizeZ = pZ; | ||
127 | } | ||
128 | |||
129 | // Just create an array of doubles. Presumes the caller implicitly knows the size. | ||
130 | public Array LegacyTerrainSerialization() | ||
131 | { | ||
132 | Array ret = null; | ||
133 | using (MemoryStream str = new MemoryStream(SizeX * SizeY * sizeof(double))) | ||
134 | { | ||
135 | using (BinaryWriter bw = new BinaryWriter(str)) | ||
136 | { | ||
137 | // TODO: COMPATIBILITY - Add byte-order conversions | ||
138 | for (int ii = 0; ii < m_heightmap.Length; ii++) | ||
139 | { | ||
140 | double height = (double)m_heightmap[ii]; | ||
141 | if (height == 0.0) | ||
142 | height = double.Epsilon; | ||
143 | |||
144 | bw.Write(height); | ||
145 | } | ||
146 | } | ||
147 | ret = str.ToArray(); | ||
148 | } | ||
149 | return ret; | ||
150 | } | ||
151 | } | ||
152 | } | ||