aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/EstateManager.cs
blob: ac710ad692f446996d6945a465c27315f6665b87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the OpenSim Project nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* 
*/
using System;
using System.Collections.Generic;
using System.Text;
using libsecondlife;
using libsecondlife.Packets;
using OpenSim.Framework.Console;
using OpenSim.Framework.Interfaces;
using OpenSim.Framework.Types;
using OpenSim.Region.Environment.Scenes;


namespace OpenSim.Region.Environment
{
    /// <summary>
    /// Processes requests regarding estates. Refer to EstateSettings.cs in OpenSim.Framework. Types for all of the core settings
    /// </summary>
    public class EstateManager
    {
        private Scene m_scene;
        private RegionInfo m_regInfo;

        public EstateManager(Scene scene, RegionInfo reginfo)
        {
            m_scene = scene;
            m_regInfo = reginfo;
        }

        private bool convertParamStringToBool(byte[] field)
        {
            string s = Helpers.FieldToUTF8String(field);
            if (s == "1" || s.ToLower() == "y" || s.ToLower() == "yes" || s.ToLower() == "t" || s.ToLower() == "true")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Sets terrain texture heights for each of the four corners of the region - textures are distributed as a linear range between the two heights.
        /// </summary>
        /// <param name="corner">Which corner</param>
        /// <param name="lowValue">Minimum height that texture range should cover</param>
        /// <param name="highValue">Maximum height that texture range should cover</param>
        public void setEstateTextureRange(Int16 corner, float lowValue, float highValue)
        {
            switch (corner)
            {
                case 0:
                    m_regInfo.estateSettings.terrainStartHeight0 = lowValue;
                    m_regInfo.estateSettings.terrainHeightRange0 = highValue;
                    break;
                case 1:
                    m_regInfo.estateSettings.terrainStartHeight1 = lowValue;
                    m_regInfo.estateSettings.terrainHeightRange1 = highValue;
                    break;
                case 2:
                    m_regInfo.estateSettings.terrainStartHeight2 = lowValue;
                    m_regInfo.estateSettings.terrainHeightRange2 = highValue;
                    break;
                case 3:
                    m_regInfo.estateSettings.terrainStartHeight3 = lowValue;
                    m_regInfo.estateSettings.terrainHeightRange3 = highValue;
                    break;
            }
        }

        /// <summary>
        /// Sets the 'detail' terrain texture on each of the bands.
        /// </summary>
        /// <param name="band">Which texture band</param>
        /// <param name="textureUUID">The UUID of the texture</param>
        public void setTerrainTexture(Int16 band, LLUUID textureUUID)
        {
            switch (band)
            {
                case 0:
                    m_regInfo.estateSettings.terrainDetail0 = textureUUID;
                    break;
                case 1:
                    m_regInfo.estateSettings.terrainDetail1 = textureUUID;
                    break;
                case 2:
                    m_regInfo.estateSettings.terrainDetail2 = textureUUID;
                    break;
                case 3:
                    m_regInfo.estateSettings.terrainDetail3 = textureUUID;
                    break;
            }
        }

        /// <summary>
        /// Sets common region settings
        /// </summary>
        /// <param name="WaterHeight">Water height of the waterplane (may not nessecarily be one value)</param>
        /// <param name="TerrainRaiseLimit">Maximum amount terrain can be raised from previous baking</param>
        /// <param name="TerrainLowerLimit">Minimum amount terrain can be lowered from previous baking</param>
        /// <param name="UseFixedSun">Use a fixed time of day on the sun?</param>
        /// <param name="SunHour">The offset hour of the day</param>
        public void setRegionSettings(float WaterHeight, float TerrainRaiseLimit, float TerrainLowerLimit,
                                      bool UseFixedSun, float SunHour)
        {
            // Water Height
            m_regInfo.estateSettings.waterHeight = WaterHeight;
            m_scene.Terrain.watermap.Fill(WaterHeight);

            // Terraforming limits
            m_regInfo.estateSettings.terrainRaiseLimit = TerrainRaiseLimit;
            m_regInfo.estateSettings.terrainLowerLimit = TerrainLowerLimit;
            m_scene.Terrain.maxRaise = TerrainRaiseLimit;
            m_scene.Terrain.minLower = TerrainLowerLimit;

            // Time of day / fixed sun
            m_regInfo.estateSettings.useFixedSun = UseFixedSun;
            m_regInfo.estateSettings.sunHour = SunHour;
        }

        #region Packet Handlers

        public void handleEstateOwnerMessage(EstateOwnerMessagePacket packet, IClientAPI remote_client)
        {
            switch (Helpers.FieldToUTF8String(packet.MethodData.Method))
            {
                case "getinfo":
                    sendRegionInfoPacketToAll();
                    break;
                case "setregioninfo":
                    if (m_scene.PermissionsMngr.CanEditEstateTerrain(remote_client.AgentId))
                        estateSetRegionInfoHandler(packet);
                    break;
                case "texturebase":
                    if (m_scene.PermissionsMngr.CanEditEstateTerrain(remote_client.AgentId))
                        estateTextureBaseHandler(packet);
                    break;
                case "texturedetail":
                    if (m_scene.PermissionsMngr.CanEditEstateTerrain(remote_client.AgentId))
                        estateTextureDetailHandler(packet);
                    break;
                case "textureheights":
                    if (m_scene.PermissionsMngr.CanEditEstateTerrain(remote_client.AgentId))
                        estateTextureHeightsHandler(packet);
                    break;
                case "texturecommit":
                    sendRegionHandshakeToAll();
                    break;
                case "setregionterrain":
                    if (m_scene.PermissionsMngr.CanEditEstateTerrain(remote_client.AgentId))
                        estateSetRegionTerrainHandler(packet);
                    break;
                default:
                    MainLog.Instance.Error("EstateOwnerMessage: Unknown method requested\n" + packet.ToString());
                    break;
            }
        }

        private void estateSetRegionInfoHandler(EstateOwnerMessagePacket packet)
        {
            if (packet.ParamList.Length != 9)
            {
                MainLog.Instance.Error("EstateOwnerMessage: SetRegionInfo method has a ParamList of invalid length");
            }
            else
            {
                m_regInfo.estateSettings.regionFlags = Simulator.RegionFlags.None;

                if (convertParamStringToBool(packet.ParamList[0].Parameter))
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.BlockTerraform;
                }

                if (convertParamStringToBool(packet.ParamList[1].Parameter))
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.NoFly;
                }

                if (convertParamStringToBool(packet.ParamList[2].Parameter))
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.AllowDamage;
                }

                if (convertParamStringToBool(packet.ParamList[3].Parameter) == false)
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.BlockLandResell;
                }


                int tempMaxAgents =
                    Convert.ToInt16(Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[4].Parameter)));
                m_regInfo.estateSettings.maxAgents = (byte) tempMaxAgents;

                float tempObjectBonusFactor =
                    (float) Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[5].Parameter));
                m_regInfo.estateSettings.objectBonusFactor = tempObjectBonusFactor;

                int tempMatureLevel = Convert.ToInt16(Helpers.FieldToUTF8String(packet.ParamList[6].Parameter));
                m_regInfo.estateSettings.simAccess = (Simulator.SimAccess) tempMatureLevel;


                if (convertParamStringToBool(packet.ParamList[7].Parameter))
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.RestrictPushObject;
                }

                if (convertParamStringToBool(packet.ParamList[8].Parameter))
                {
                    m_regInfo.estateSettings.regionFlags = m_regInfo.estateSettings.regionFlags |
                                                           Simulator.RegionFlags.AllowParcelChanges;
                }

                sendRegionInfoPacketToAll();
            }
        }

        private void estateSetRegionTerrainHandler(EstateOwnerMessagePacket packet)
        {
            if (packet.ParamList.Length != 9)
            {
                MainLog.Instance.Error("EstateOwnerMessage: SetRegionTerrain method has a ParamList of invalid length");
            }
            else
            {
                float WaterHeight = (float) Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[0].Parameter));
                float TerrainRaiseLimit =
                    (float) Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[1].Parameter));
                float TerrainLowerLimit =
                    (float) Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[2].Parameter));
                bool UseFixedSun = convertParamStringToBool(packet.ParamList[4].Parameter);
                float SunHour = (float) Convert.ToDecimal(Helpers.FieldToUTF8String(packet.ParamList[5].Parameter));

                setRegionSettings(WaterHeight, TerrainRaiseLimit, TerrainLowerLimit, UseFixedSun, SunHour);

                sendRegionInfoPacketToAll();
            }
        }

        private void estateTextureHeightsHandler(EstateOwnerMessagePacket packet)
        {
            foreach (EstateOwnerMessagePacket.ParamListBlock block in packet.ParamList)
            {
                string s = Helpers.FieldToUTF8String(block.Parameter);
                string[] splitField = s.Split(' ');
                if (splitField.Length == 3)
                {
                    Int16 corner = Convert.ToInt16(splitField[0]);
                    float lowValue = (float) Convert.ToDecimal(splitField[1]);
                    float highValue = (float) Convert.ToDecimal(splitField[2]);

                    setEstateTextureRange(corner, lowValue, highValue);
                }
            }
        }

        private void estateTextureDetailHandler(EstateOwnerMessagePacket packet)
        {
            foreach (EstateOwnerMessagePacket.ParamListBlock block in packet.ParamList)
            {
                string s = Helpers.FieldToUTF8String(block.Parameter);
                string[] splitField = s.Split(' ');
                if (splitField.Length == 2)
                {
                    Int16 corner = Convert.ToInt16(splitField[0]);
                    LLUUID textureUUID = new LLUUID(splitField[1]);

                    setTerrainTexture(corner, textureUUID);
                }
            }
        }

        private void estateTextureBaseHandler(EstateOwnerMessagePacket packet)
        {
            foreach (EstateOwnerMessagePacket.ParamListBlock block in packet.ParamList)
            {
                string s = Helpers.FieldToUTF8String(block.Parameter);
                string[] splitField = s.Split(' ');
                if (splitField.Length == 2)
                {
                    LLUUID tempUUID = new LLUUID(splitField[1]);
                    switch (Convert.ToInt16(splitField[0]))
                    {
                        case 0:
                            m_regInfo.estateSettings.terrainBase0 = tempUUID;
                            break;
                        case 1:
                            m_regInfo.estateSettings.terrainBase1 = tempUUID;
                            break;
                        case 2:
                            m_regInfo.estateSettings.terrainBase2 = tempUUID;
                            break;
                        case 3:
                            m_regInfo.estateSettings.terrainBase3 = tempUUID;
                            break;
                    }
                }
            }
        }

        #endregion

        #region Outgoing Packets

        public void sendRegionInfoPacketToAll()
        {
            List<ScenePresence> avatars = m_scene.GetAvatars();

            for (int i = 0; i < avatars.Count; i++)
            {
                sendRegionInfoPacket(avatars[i].ControllingClient);
            }
        }

        public void sendRegionHandshakeToAll()
        {
            m_scene.ForEachScenePresence( delegate( ScenePresence scenePresence )
            {
                sendRegionHandshake(scenePresence.ControllingClient);
            });
        }

        public void sendRegionInfoPacket(IClientAPI remote_client)
        {
            Encoding _enc = Encoding.ASCII;

            AgentCircuitData circuitData = remote_client.RequestClientInfo();

            RegionInfoPacket regionInfoPacket = new RegionInfoPacket();
            regionInfoPacket.AgentData.AgentID = circuitData.AgentID;
            regionInfoPacket.AgentData.SessionID = circuitData.SessionID;
            regionInfoPacket.RegionInfo.BillableFactor = m_regInfo.estateSettings.billableFactor;
            regionInfoPacket.RegionInfo.EstateID = m_regInfo.estateSettings.estateID;
            regionInfoPacket.RegionInfo.MaxAgents = m_regInfo.estateSettings.maxAgents;
            regionInfoPacket.RegionInfo.ObjectBonusFactor = m_regInfo.estateSettings.objectBonusFactor;
            regionInfoPacket.RegionInfo.ParentEstateID = m_regInfo.estateSettings.parentEstateID;
            regionInfoPacket.RegionInfo.PricePerMeter = m_regInfo.estateSettings.pricePerMeter;
            regionInfoPacket.RegionInfo.RedirectGridX = m_regInfo.estateSettings.redirectGridX;
            regionInfoPacket.RegionInfo.RedirectGridY = m_regInfo.estateSettings.redirectGridY;
            regionInfoPacket.RegionInfo.RegionFlags = (uint) m_regInfo.estateSettings.regionFlags;
            regionInfoPacket.RegionInfo.SimAccess = (byte) m_regInfo.estateSettings.simAccess;
            regionInfoPacket.RegionInfo.SimName = _enc.GetBytes(m_regInfo.RegionName);
            regionInfoPacket.RegionInfo.SunHour = m_regInfo.estateSettings.sunHour;
            regionInfoPacket.RegionInfo.TerrainLowerLimit = m_regInfo.estateSettings.terrainLowerLimit;
            regionInfoPacket.RegionInfo.TerrainRaiseLimit = m_regInfo.estateSettings.terrainRaiseLimit;
            regionInfoPacket.RegionInfo.UseEstateSun = !m_regInfo.estateSettings.useFixedSun;
            regionInfoPacket.RegionInfo.WaterHeight = m_regInfo.estateSettings.waterHeight;

            remote_client.OutPacket(regionInfoPacket);
        }

        public void sendRegionHandshake(IClientAPI remoteClient)
        {
            remoteClient.SendRegionHandshake(m_regInfo);
        }

        #endregion
    }
}