aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/Primitive(Old).cs
blob: 64976a873793f9399b64263c4fb66662c76ff7fc (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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using Axiom.Math;
using libsecondlife;
using libsecondlife.Packets;
using OpenSim.Framework.Interfaces;
using OpenSim.Framework.Inventory;
using OpenSim.Framework.Types;

using InventoryItem = OpenSim.Framework.Inventory.InventoryItem;

namespace OpenSim.Region.Environment.Scenes
{
    public delegate void PrimCountTaintedDelegate();

    public class Primitive : EntityBase
    {
        private const uint FULL_MASK_PERMISSIONS = 2147483647;

        private LLVector3 m_positionLastFrame = new LLVector3(0, 0, 0);
        private ulong m_regionHandle;
        private byte m_updateFlag;
        private uint m_flags = 32 + 65536 + 131072 + 256 + 4 + 8 + 2048 + 524288 + 268435456 + 128;

        private Dictionary<LLUUID, InventoryItem> m_inventoryItems;

        private string m_description = "";

        public LLUUID CreatorID;
        public LLUUID OwnerID;
        public LLUUID LastOwnerID;

        public Int32 CreationDate;

        public uint ParentID = 0;

        public uint OwnerMask = FULL_MASK_PERMISSIONS;
        public uint NextOwnerMask = FULL_MASK_PERMISSIONS;
        public uint GroupMask = 0;// FULL_MASK_PERMISSIONS;
        public uint EveryoneMask = 0;//FULL_MASK_PERMISSIONS;
        public uint BaseMask = 0;//FULL_MASK_PERMISSIONS;

        private PrimitiveBaseShape m_shape;
        private byte[] m_particleSystem = new byte[0];

        public SceneObjectOLD m_RootParent;
        public bool m_isRootPrim;
        public EntityBase m_Parent;

        public event PrimCountTaintedDelegate OnPrimCountTainted;

        #region Properties

        /// <summary>
        /// If rootprim, will return world position
        /// otherwise will return local offset from rootprim
        /// </summary>
        public override LLVector3 Pos
        {
            get
            {
                if (m_isRootPrim)
                {
                    //if we are rootprim then our offset should be zero
                    return m_pos + m_Parent.Pos;
                }
                else
                {
                    return m_pos;
                }
            }
            set
            {
                if (m_isRootPrim)
                {
                    m_Parent.Pos = value;
                }
                m_pos = value - m_Parent.Pos;
            }
        }

        public PrimitiveBaseShape Shape
        {
            get { return m_shape; }
        }

        public LLVector3 WorldPos
        {
            get
            {
                if (!m_isRootPrim)
                {
                    Primitive parentPrim = (Primitive)m_Parent;
                    Vector3 offsetPos = new Vector3(m_pos.X, m_pos.Y, m_pos.Z);
                    offsetPos = parentPrim.Rotation * offsetPos;
                    return parentPrim.WorldPos + new LLVector3(offsetPos.x, offsetPos.y, offsetPos.z);
                }
                else
                {
                    return Pos;
                }
            }
        }

        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        public LLVector3 Scale
        {
            set { m_shape.Scale = value; }
            get { return m_shape.Scale; }
        }

        private string m_sitName = "";
        public string SitName
        {
            get { return m_sitName; }
        }

        private string m_touchName = "";
        public string TouchName
        {
            get { return m_touchName; }
        }

        private string m_text = "";
        public string Text
        {
            get { return m_text; }
            set
            {
                m_text = value;
                ScheduleFullUpdate();
            }
        }

        #endregion

        #region Constructors

        public Primitive(ulong regionHandle, Scene scene, LLUUID ownerID, uint localID, bool isRoot, EntityBase parent,
                         SceneObjectOLD rootObject, PrimitiveBaseShape shape, LLVector3 pos)
        {
            m_regionHandle = regionHandle;
            m_scene = scene;
            m_inventoryItems = new Dictionary<LLUUID, InventoryItem>();
            m_Parent = parent;
            m_isRootPrim = isRoot;
            m_RootParent = rootObject;
            ClearUpdateSchedule();
            CreateFromShape(ownerID, localID, pos, shape);

            Rotation = Quaternion.Identity;

            m_scene.AcknowledgeNewPrim(this);

            OnPrimCountTainted();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>Empty constructor for duplication</remarks>
        public Primitive()
        {
        }

        #endregion

        #region Destructors

        ~Primitive()
        {
            if (OnPrimCountTainted != null)
                OnPrimCountTainted();
        }

        #endregion

        #region Duplication

        public Primitive Copy(EntityBase parent, SceneObjectOLD rootParent)
        {
            Primitive dupe = (Primitive)MemberwiseClone();

            dupe.m_Parent = parent;
            dupe.m_RootParent = rootParent;

            // TODO: Copy this properly.

            dupe.m_inventoryItems = m_inventoryItems;
            dupe.m_children = new List<EntityBase>();
            dupe.m_shape = m_shape.Copy();
            dupe.m_regionHandle = m_regionHandle;
            dupe.m_scene = m_scene;


            uint newLocalID = m_scene.PrimIDAllocate();
            dupe.m_uuid = LLUUID.Random();
            dupe.LocalId = newLocalID;

            if (parent is SceneObjectGroup)
            {
                dupe.m_isRootPrim = true;
                dupe.ParentID = 0;
            }
            else
            {
                dupe.m_isRootPrim = false;
                dupe.ParentID = ((Primitive)parent).LocalId;
            }

            dupe.Scale = new LLVector3(Scale.X, Scale.Y, Scale.Z);
            dupe.Rotation = new Quaternion(Rotation.w, Rotation.x, Rotation.y, Rotation.z);
            dupe.m_pos = new LLVector3(m_pos.X, m_pos.Y, m_pos.Z);

            rootParent.AddChildToList(dupe);
            m_scene.AcknowledgeNewPrim(dupe);
            dupe.TriggerOnPrimCountTainted();


            foreach (Primitive prim in m_children)
            {
                Primitive primClone = prim.Copy(dupe, rootParent);

                dupe.m_children.Add(primClone);
            }

            return dupe;
        }

        #endregion

        #region Override from EntityBase

        /// <summary>
        /// 
        /// </summary>
        public override void Update()
        {
            if (m_updateFlag == 1) //some change has been made so update the clients
            {
                SendTerseUpdateToALLClients();
                ClearUpdateSchedule();
            }
            else
            {
                if (m_updateFlag == 2) // is a new prim just been created/reloaded or has major changes
                {
                    SendFullUpdateToAllClients();
                    ClearUpdateSchedule();
                }
            }

            foreach (EntityBase child in m_children)
            {
                child.Update();
            }
        }

        private void ClearUpdateSchedule()
        {
            m_updateFlag = 0;
        }

        #endregion

        #region Setup

        /// <summary>
        /// 
        /// </summary>
        /// <param name="addPacket"></param>
        /// <param name="ownerID"></param>
        /// <param name="localID"></param>
        public void CreateFromShape(LLUUID ownerID, uint localID, LLVector3 pos, PrimitiveBaseShape shape)
        {
            CreationDate = (Int32)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
            OwnerID = ownerID;
            CreatorID = OwnerID;
            LastOwnerID = LLUUID.Zero;
            Pos = pos;
            m_uuid = LLUUID.Random();
            m_localId = (uint)(localID);

            m_shape = shape;

            ScheduleFullUpdate();
        }

        private void ScheduleFullUpdate()
        {
            m_updateFlag = 2;
        }

        private void ScheduleTerseUpdate()
        {
            if (m_updateFlag < 1)
            {
                m_updateFlag = 1;
            }
        }

        #endregion

        #region Linking / unlinking

        /// <summary>
        /// 
        /// </summary>
        /// <param name="linkObject"></param>
        public void AddNewChildren(SceneObjectOLD linkObject)
        {
            // Console.WriteLine("linking new prims " + linkObject.rootLocalID + " to me (" + this.LocalId + ")");
            //TODO check permissions

            m_children.Add(linkObject.rootPrimitive);
           linkObject.rootPrimitive.SetNewParent(this, m_RootParent);

            m_scene.DeleteEntity(linkObject.rootUUID);
            linkObject.DeleteAllChildren();

            OnPrimCountTainted();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newParent"></param>
        /// <param name="rootParent"></param>
        public void SetNewParent(Primitive newParent, SceneObjectOLD rootParent)
        {
            LLVector3 oldPos = new LLVector3(Pos.X, Pos.Y, Pos.Z);
            m_isRootPrim = false;
            m_Parent = newParent;
            ParentID = newParent.LocalId;
            m_RootParent = rootParent;
            m_RootParent.AddChildToList(this);
            Pos = oldPos;
            Vector3 axPos = new Vector3(m_pos.X, m_pos.Y, m_pos.Z);
            axPos = m_Parent.Rotation.Inverse() * axPos;
            m_pos = new LLVector3(axPos.x, axPos.y, axPos.z);
            Quaternion oldRot = new Quaternion(Rotation.w, Rotation.x, Rotation.y, Rotation.z);
            Rotation = m_Parent.Rotation.Inverse() * Rotation;
            ScheduleFullUpdate();


            foreach (Primitive child in m_children)
            {
                child.SetRootParent(rootParent, newParent, oldPos, oldRot);
            }

            m_children.Clear();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newRoot"></param>
        public void SetRootParent(SceneObjectOLD newRoot, Primitive newParent, LLVector3 oldParentPosition,
                                  Quaternion oldParentRotation)
        {
            LLVector3 oldPos = new LLVector3(Pos.X, Pos.Y, Pos.Z);
            Vector3 axOldPos = new Vector3(oldPos.X, oldPos.Y, oldPos.Z);
            axOldPos = oldParentRotation * axOldPos;
            oldPos = new LLVector3(axOldPos.x, axOldPos.y, axOldPos.z);
            oldPos += oldParentPosition;
            Quaternion oldRot = new Quaternion(Rotation.w, Rotation.x, Rotation.y, Rotation.z);
            m_isRootPrim = false;
            m_Parent = newParent;
            ParentID = newParent.LocalId;
            newParent.AddToChildrenList(this);

            m_RootParent = newRoot;
            m_RootParent.AddChildToList(this);
            Pos = oldPos;
            Vector3 axPos = new Vector3(m_pos.X, m_pos.Y, m_pos.Z);
            axPos = m_Parent.Rotation.Inverse() * axPos;
            m_pos = new LLVector3(axPos.x, axPos.y, axPos.z);
            Rotation = oldParentRotation * Rotation;
            Rotation = m_Parent.Rotation.Inverse() * Rotation;
            ScheduleFullUpdate();
            foreach (Primitive child in m_children)
            {
                child.SetRootParent(newRoot, newParent, oldPos, oldRot);
            }

            m_children.Clear();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        public void AddOffsetToChildren(LLVector3 offset)
        {
            foreach (Primitive prim in m_children)
            {
                prim.m_pos += offset;
                prim.ScheduleTerseUpdate();
            }
            OnPrimCountTainted();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="prim"></param>
        public void AddToChildrenList(Primitive prim)
        {
            m_children.Add(prim);
        }

        #endregion

        #region Resizing/Scale

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scale"></param>
        public void ResizeGoup(LLVector3 scale)
        {
            m_shape.Scale = scale;

            ScheduleFullUpdate();
        }

        #endregion

        #region Position

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pos"></param>
        public void UpdateGroupPosition(LLVector3 pos)
        {
            LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);

            Pos = newPos;
            ScheduleTerseUpdate();

            OnPrimCountTainted();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pos"></param>
        public void UpdateSinglePosition(LLVector3 pos)
        {
            // Console.WriteLine("updating single prim position");
            if (m_isRootPrim)
            {
                LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
                LLVector3 oldPos = new LLVector3(Pos.X, Pos.Y, Pos.Z);
                LLVector3 diff = oldPos - newPos;
                Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
                axDiff = Rotation.Inverse() * axDiff;
                diff.X = axDiff.x;
                diff.Y = axDiff.y;
                diff.Z = axDiff.z;
                Pos = newPos;

                foreach (Primitive prim in m_children)
                {
                    prim.m_pos += diff;
                    prim.ScheduleTerseUpdate();
                }
                ScheduleTerseUpdate();
            }
            else
            {
                LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
                m_pos = newPos;
                ScheduleTerseUpdate();
            }
        }

        #endregion

        #region Rotation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rot"></param>
        public void UpdateGroupRotation(LLQuaternion rot)
        {
            Rotation = new Quaternion(rot.W, rot.X, rot.Y, rot.Z);
            ScheduleTerseUpdate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pos"></param>
        /// <param name="rot"></param>
        public void UpdateGroupMouseRotation(LLVector3 pos, LLQuaternion rot)
        {
            Rotation = new Quaternion(rot.W, rot.X, rot.Y, rot.Z);
            Pos = pos;
            ScheduleTerseUpdate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rot"></param>
        public void UpdateSingleRotation(LLQuaternion rot)
        {
            //Console.WriteLine("updating single prim rotation");

            Quaternion axRot = new Quaternion(rot.W, rot.X, rot.Y, rot.Z);
            Quaternion oldParentRot = new Quaternion(Rotation.w, Rotation.x, Rotation.y, Rotation.z);
            Rotation = axRot;
            foreach (Primitive prim in m_children)
            {
                Vector3 axPos = new Vector3(prim.m_pos.X, prim.m_pos.Y, prim.m_pos.Z);
                axPos = oldParentRot * axPos;
                axPos = axRot.Inverse() * axPos;
                prim.m_pos = new LLVector3(axPos.x, axPos.y, axPos.z);
                prim.Rotation = oldParentRot * prim.Rotation;
                prim.Rotation = axRot.Inverse() * prim.Rotation;
                prim.ScheduleTerseUpdate();
            }
            ScheduleTerseUpdate();
        }

        #endregion

        #region Shape

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shapeBlock"></param>
        public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock)
        {
            m_shape.PathBegin = shapeBlock.PathBegin;
            m_shape.PathEnd = shapeBlock.PathEnd;
            m_shape.PathScaleX = shapeBlock.PathScaleX;
            m_shape.PathScaleY = shapeBlock.PathScaleY;
            m_shape.PathShearX = shapeBlock.PathShearX;
            m_shape.PathShearY = shapeBlock.PathShearY;
            m_shape.PathSkew = shapeBlock.PathSkew;
            m_shape.ProfileBegin = shapeBlock.ProfileBegin;
            m_shape.ProfileEnd = shapeBlock.ProfileEnd;
            m_shape.PathCurve = shapeBlock.PathCurve;
            m_shape.ProfileCurve = shapeBlock.ProfileCurve;
            m_shape.ProfileHollow = shapeBlock.ProfileHollow;
            m_shape.PathRadiusOffset = shapeBlock.PathRadiusOffset;
            m_shape.PathRevolutions = shapeBlock.PathRevolutions;
            m_shape.PathTaperX = shapeBlock.PathTaperX;
            m_shape.PathTaperY = shapeBlock.PathTaperY;
            m_shape.PathTwist = shapeBlock.PathTwist;
            m_shape.PathTwistBegin = shapeBlock.PathTwistBegin;
            ScheduleFullUpdate();
        }

        #endregion

        #region Inventory
        public void GetInventory(IClientAPI client, uint localID)
        {
            if (localID == this.m_localId)
            {
                client.SendTaskInventory(this.m_uuid, 0, new byte[0]);
            }
        }
        #endregion

        public void UpdateExtraParam(ushort type, bool inUse, byte[] data)
        {
            this.m_shape.ExtraParams = new byte[data.Length + 7];
            int i =0;
            uint length = (uint) data.Length;
            this.m_shape.ExtraParams[i++] = 1;
            this.m_shape.ExtraParams[i++] = (byte)(type % 256);
            this.m_shape.ExtraParams[i++] = (byte)((type >> 8) % 256);
               
            this.m_shape.ExtraParams[i++] = (byte)(length % 256);
            this.m_shape.ExtraParams[i++] = (byte)((length >> 8) % 256);
            this.m_shape.ExtraParams[i++] = (byte)((length >> 16) % 256);
            this.m_shape.ExtraParams[i++] = (byte)((length >> 24) % 256);
            Array.Copy(data, 0, this.m_shape.ExtraParams, i, data.Length);

            this.ScheduleFullUpdate();
        }

        #region Texture

        /// <summary>
        /// 
        /// </summary>
        /// <param name="textureEntry"></param>
        public void UpdateTextureEntry(byte[] textureEntry)
        {
            m_shape.TextureEntry = textureEntry;
            ScheduleFullUpdate();
        }

        #endregion

        public void AddNewParticleSystem(libsecondlife.Primitive.ParticleSystem pSystem)
        {
            this.m_particleSystem = pSystem.GetBytes();
            ScheduleFullUpdate();
        }

        #region Client Update Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="remoteClient"></param>
        public void SendFullUpdateForAllChildren(IClientAPI remoteClient)
        {

            SendFullUpdateToClient(remoteClient);
            for (int i = 0; i < m_children.Count; i++)

            {

                if (m_children[i] is Primitive)
                {
                    ((Primitive)m_children[i]).SendFullUpdateForAllChildren(remoteClient);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="remoteClient"></param>
        public void SendFullUpdateToClient(IClientAPI remoteClient)
        {
            LLVector3 lPos;
            lPos = Pos;
            LLQuaternion lRot;
            lRot = new LLQuaternion(Rotation.x, Rotation.y, Rotation.z, Rotation.w);

            remoteClient.SendPrimitiveToClient(m_regionHandle, 64096, LocalId, m_shape, lPos, m_flags, m_uuid, OwnerID,
                                               m_text, ParentID, this.m_particleSystem, lRot);
        }

        /// <summary>
        /// 
        /// </summary>
        public void SendFullUpdateToAllClients()
        {
            List<ScenePresence> avatars = m_scene.RequestAvatarList();
            for (int i = 0; i < avatars.Count; i++)
            {
                SendFullUpdateToClient(avatars[i].ControllingClient);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="remoteClient"></param>
        public void SendTerseUpdateForAllChildren(IClientAPI remoteClient)
        {

            SendTerseUpdateToClient(remoteClient);
            for (int i = 0; i < m_children.Count; i++)
            {
                if (m_children[i] is Primitive)
                {
                    ((Primitive)m_children[i]).SendTerseUpdateForAllChildren(remoteClient);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="RemoteClient"></param>
        public void SendTerseUpdateToClient(IClientAPI RemoteClient)
        {
            LLVector3 lPos;
            Quaternion lRot;

            lPos = Pos;
            lRot = Rotation;

            LLQuaternion mRot = new LLQuaternion(lRot.x, lRot.y, lRot.z, lRot.w);
            RemoteClient.SendPrimTerseUpdate(m_regionHandle, 64096, LocalId, lPos, mRot);
        }

        /// <summary>
        /// 
        /// </summary>
        public void SendTerseUpdateToALLClients()
        {
            List<ScenePresence> avatars = m_scene.RequestAvatarList();
            for (int i = 0; i < avatars.Count; i++)
            {
                SendTerseUpdateToClient(avatars[i].ControllingClient);
            }
        }

        #endregion

        public void TriggerOnPrimCountTainted()
        {
            OnPrimCountTainted();
        }
    }
}