clockwerk-opensim-stable – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 OpenSim.Framework; |
||
29 | using OpenSim.Region.Framework.Interfaces; |
||
30 | using System; |
||
31 | using System.Text; |
||
32 | using System.Xml; |
||
33 | using System.IO; |
||
34 | using System.Xml.Serialization; |
||
35 | |||
36 | namespace OpenSim.Region.Framework.Scenes |
||
37 | { |
||
38 | /// <summary> |
||
39 | /// A new version of the old Channel class, simplified |
||
40 | /// </summary> |
||
41 | public class TerrainChannel : ITerrainChannel |
||
42 | { |
||
43 | private readonly bool[,] taint; |
||
44 | private double[,] map; |
||
45 | |||
46 | public TerrainChannel() |
||
47 | { |
||
48 | map = new double[Constants.RegionSize, Constants.RegionSize]; |
||
49 | taint = new bool[Constants.RegionSize / 16, Constants.RegionSize / 16]; |
||
50 | |||
51 | PinHeadIsland(); |
||
52 | } |
||
53 | |||
54 | public TerrainChannel(String type) |
||
55 | { |
||
56 | map = new double[Constants.RegionSize, Constants.RegionSize]; |
||
57 | taint = new bool[Constants.RegionSize / 16, Constants.RegionSize / 16]; |
||
58 | |||
59 | if (type.Equals("flat")) |
||
60 | FlatLand(); |
||
61 | else |
||
62 | PinHeadIsland(); |
||
63 | } |
||
64 | |||
65 | public TerrainChannel(double[,] import) |
||
66 | { |
||
67 | map = import; |
||
68 | taint = new bool[import.GetLength(0),import.GetLength(1)]; |
||
69 | } |
||
70 | |||
71 | public TerrainChannel(bool createMap) |
||
72 | { |
||
73 | if (createMap) |
||
74 | { |
||
75 | map = new double[Constants.RegionSize,Constants.RegionSize]; |
||
76 | taint = new bool[Constants.RegionSize / 16,Constants.RegionSize / 16]; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | public TerrainChannel(int w, int h) |
||
81 | { |
||
82 | map = new double[w,h]; |
||
83 | taint = new bool[w / 16,h / 16]; |
||
84 | } |
||
85 | |||
86 | #region ITerrainChannel Members |
||
87 | |||
88 | public int Width |
||
89 | { |
||
90 | get { return map.GetLength(0); } |
||
91 | } |
||
92 | |||
93 | public int Height |
||
94 | { |
||
95 | get { return map.GetLength(1); } |
||
96 | } |
||
97 | |||
98 | public ITerrainChannel MakeCopy() |
||
99 | { |
||
100 | TerrainChannel copy = new TerrainChannel(false); |
||
101 | copy.map = (double[,]) map.Clone(); |
||
102 | |||
103 | return copy; |
||
104 | } |
||
105 | |||
106 | public float[] GetFloatsSerialised() |
||
107 | { |
||
108 | // Move the member variables into local variables, calling |
||
109 | // member variables 256*256 times gets expensive |
||
110 | int w = Width; |
||
111 | int h = Height; |
||
112 | float[] heights = new float[w * h]; |
||
113 | |||
114 | int i, j; // map coordinates |
||
115 | int idx = 0; // index into serialized array |
||
116 | for (i = 0; i < h; i++) |
||
117 | { |
||
118 | for (j = 0; j < w; j++) |
||
119 | { |
||
120 | heights[idx++] = (float)map[j, i]; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return heights; |
||
125 | } |
||
126 | |||
127 | public double[,] GetDoubles() |
||
128 | { |
||
129 | return map; |
||
130 | } |
||
131 | |||
132 | public double this[int x, int y] |
||
133 | { |
||
134 | get { return map[x, y]; } |
||
135 | set |
||
136 | { |
||
137 | // Will "fix" terrain hole problems. Although not fantastically. |
||
138 | if (Double.IsNaN(value) || Double.IsInfinity(value)) |
||
139 | return; |
||
140 | |||
141 | if (map[x, y] != value) |
||
142 | { |
||
143 | taint[x / 16, y / 16] = true; |
||
144 | map[x, y] = value; |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | public bool Tainted(int x, int y) |
||
150 | { |
||
151 | if (taint[x / 16, y / 16]) |
||
152 | { |
||
153 | taint[x / 16, y / 16] = false; |
||
154 | return true; |
||
155 | } |
||
156 | return false; |
||
157 | } |
||
158 | |||
159 | #endregion |
||
160 | |||
161 | public TerrainChannel Copy() |
||
162 | { |
||
163 | TerrainChannel copy = new TerrainChannel(false); |
||
164 | copy.map = (double[,]) map.Clone(); |
||
165 | |||
166 | return copy; |
||
167 | } |
||
168 | |||
169 | public string SaveToXmlString() |
||
170 | { |
||
171 | XmlWriterSettings settings = new XmlWriterSettings(); |
||
172 | settings.Encoding = Util.UTF8; |
||
173 | using (StringWriter sw = new StringWriter()) |
||
174 | { |
||
175 | using (XmlWriter writer = XmlWriter.Create(sw, settings)) |
||
176 | { |
||
177 | WriteXml(writer); |
||
178 | } |
||
179 | string output = sw.ToString(); |
||
180 | return output; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private void WriteXml(XmlWriter writer) |
||
185 | { |
||
186 | writer.WriteStartElement(String.Empty, "TerrainMap", String.Empty); |
||
187 | ToXml(writer); |
||
188 | writer.WriteEndElement(); |
||
189 | } |
||
190 | |||
191 | public void LoadFromXmlString(string data) |
||
192 | { |
||
193 | StringReader sr = new StringReader(data); |
||
194 | XmlTextReader reader = new XmlTextReader(sr); |
||
195 | reader.Read(); |
||
196 | |||
197 | ReadXml(reader); |
||
198 | reader.Close(); |
||
199 | sr.Close(); |
||
200 | } |
||
201 | |||
202 | private void ReadXml(XmlReader reader) |
||
203 | { |
||
204 | reader.ReadStartElement("TerrainMap"); |
||
205 | FromXml(reader); |
||
206 | } |
||
207 | |||
208 | private void ToXml(XmlWriter xmlWriter) |
||
209 | { |
||
210 | float[] mapData = GetFloatsSerialised(); |
||
211 | byte[] buffer = new byte[mapData.Length * 4]; |
||
212 | for (int i = 0; i < mapData.Length; i++) |
||
213 | { |
||
214 | byte[] value = BitConverter.GetBytes(mapData[i]); |
||
215 | Array.Copy(value, 0, buffer, (i * 4), 4); |
||
216 | } |
||
217 | XmlSerializer serializer = new XmlSerializer(typeof(byte[])); |
||
218 | serializer.Serialize(xmlWriter, buffer); |
||
219 | } |
||
220 | |||
221 | private void FromXml(XmlReader xmlReader) |
||
222 | { |
||
223 | XmlSerializer serializer = new XmlSerializer(typeof(byte[])); |
||
224 | byte[] dataArray = (byte[])serializer.Deserialize(xmlReader); |
||
225 | int index = 0; |
||
226 | |||
227 | for (int y = 0; y < Height; y++) |
||
228 | { |
||
229 | for (int x = 0; x < Width; x++) |
||
230 | { |
||
231 | float value; |
||
232 | value = BitConverter.ToSingle(dataArray, index); |
||
233 | index += 4; |
||
234 | this[x, y] = (double)value; |
||
235 | } |
||
236 | } |
||
237 | } |
||
238 | |||
239 | private void PinHeadIsland() |
||
240 | { |
||
241 | int x; |
||
242 | for (x = 0; x < Constants.RegionSize; x++) |
||
243 | { |
||
244 | int y; |
||
245 | for (y = 0; y < Constants.RegionSize; y++) |
||
246 | { |
||
247 | map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 2, 0.125) * 10; |
||
248 | double spherFacA = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2.0, Constants.RegionSize / 2.0, 50) * 0.01; |
||
249 | double spherFacB = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2.0, Constants.RegionSize / 2.0, 100) * 0.001; |
||
250 | if (map[x, y] < spherFacA) |
||
251 | map[x, y] = spherFacA; |
||
252 | if (map[x, y] < spherFacB) |
||
253 | map[x, y] = spherFacB; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | private void FlatLand() |
||
259 | { |
||
260 | int x; |
||
261 | for (x = 0; x < Constants.RegionSize; x++) |
||
262 | { |
||
263 | int y; |
||
264 | for (y = 0; y < Constants.RegionSize; y++) |
||
265 | map[x, y] = 21; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | } |
||
270 | } |