aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/ScenePresence.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Environment/Scenes/ScenePresence.cs597
1 files changed, 597 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Scenes/ScenePresence.cs b/OpenSim/Region/Environment/Scenes/ScenePresence.cs
new file mode 100644
index 0000000..e65ab7c
--- /dev/null
+++ b/OpenSim/Region/Environment/Scenes/ScenePresence.cs
@@ -0,0 +1,597 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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*/
28using System;
29using System.Collections.Generic;
30using System.IO;
31using Axiom.Math;
32using libsecondlife;
33using libsecondlife.Packets;
34using OpenSim.Framework.Console;
35using OpenSim.Framework.Interfaces;
36using OpenSim.Framework.Types;
37using OpenSim.Physics.Manager;
38
39namespace OpenSim.Region.Environment.Scenes
40{
41 public partial class ScenePresence : Entity
42 {
43 public static bool PhysicsEngineFlying = false;
44 public static AvatarAnimations Animations;
45 public static byte[] DefaultTexture;
46 public string firstname;
47 public string lastname;
48 public IClientAPI ControllingClient;
49 public LLUUID current_anim;
50 public int anim_seq;
51 private bool updateflag = false;
52 private byte movementflag = 0;
53 private List<NewForce> forcesList = new List<NewForce>();
54 private short _updateCount = 0;
55 private Quaternion bodyRot;
56 private LLObject.TextureEntry avatarAppearanceTexture = null;
57 private byte[] visualParams;
58 private AvatarWearable[] Wearables;
59 private LLVector3 positionLastFrame = new LLVector3(0, 0, 0);
60 private ulong m_regionHandle;
61 private bool childAgent = false;
62 private bool newForce = false;
63 private bool newAvatar = false;
64 private IScenePresenceBody m_body;
65
66 protected RegionInfo m_regionInfo;
67
68 private Vector3[] Dir_Vectors = new Vector3[6];
69 private enum Dir_ControlFlags
70 {
71 DIR_CONTROL_FLAG_FOWARD = MainAvatar.ControlFlags.AGENT_CONTROL_AT_POS,
72 DIR_CONTROL_FLAG_BACK = MainAvatar.ControlFlags.AGENT_CONTROL_AT_NEG,
73 DIR_CONTROL_FLAG_LEFT = MainAvatar.ControlFlags.AGENT_CONTROL_LEFT_POS,
74 DIR_CONTROL_FLAG_RIGHT = MainAvatar.ControlFlags.AGENT_CONTROL_LEFT_NEG,
75 DIR_CONTROL_FLAG_UP = MainAvatar.ControlFlags.AGENT_CONTROL_UP_POS,
76 DIR_CONTROL_FLAG_DOWN = MainAvatar.ControlFlags.AGENT_CONTROL_UP_NEG
77 }
78
79 #region Properties
80 /// <summary>
81 ///
82 /// </summary>
83 public PhysicsActor PhysActor
84 {
85 set
86 {
87 this._physActor = value;
88 }
89 get
90 {
91 return _physActor;
92 }
93 }
94
95 public ulong RegionHandle
96 {
97 get { return m_regionHandle; }
98 }
99
100 #endregion
101
102 #region Constructor(s)
103 /// <summary>
104 ///
105 /// </summary>
106 /// <param name="theClient"></param>
107 /// <param name="world"></param>
108 /// <param name="clientThreads"></param>
109 /// <param name="regionDat"></param>
110 public ScenePresence(IClientAPI theClient, Scene world, RegionInfo reginfo)
111 {
112
113 m_world = world;
114 this.uuid = theClient.AgentId;
115
116 m_regionInfo = reginfo;
117 m_regionHandle = reginfo.RegionHandle;
118 MainLog.Instance.Verbose("Avatar.cs ");
119 ControllingClient = theClient;
120 this.firstname = ControllingClient.FirstName;
121 this.lastname = ControllingClient.LastName;
122 m_localId = m_world.NextLocalId;
123 Pos = ControllingClient.StartPos;
124 visualParams = new byte[218];
125 for (int i = 0; i < 218; i++)
126 {
127 visualParams[i] = 100;
128 }
129
130 Wearables = AvatarWearable.DefaultWearables;
131 Animations = new ScenePresence.AvatarAnimations();
132 Animations.LoadAnims();
133
134 this.avatarAppearanceTexture = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
135
136 //register for events
137 ControllingClient.OnRequestWearables += this.SendOurAppearance;
138 //ControllingClient.OnSetAppearance += new SetAppearance(this.SetAppearance);
139 ControllingClient.OnCompleteMovementToRegion += this.CompleteMovement;
140 ControllingClient.OnCompleteMovementToRegion += this.SendInitialData;
141 ControllingClient.OnAgentUpdate += this.HandleAgentUpdate;
142 // ControllingClient.OnStartAnim += new StartAnim(this.SendAnimPack);
143 // ControllingClient.OnChildAgentStatus += new StatusChange(this.ChildStatusChange);
144 //ControllingClient.OnStopMovement += new GenericCall2(this.StopMovement);
145
146 Dir_Vectors[0] = new Vector3(1, 0, 0); //FOWARD
147 Dir_Vectors[1] = new Vector3(-1, 0, 0); //BACK
148 Dir_Vectors[2] = new Vector3(0, 1, 0); //LEFT
149 Dir_Vectors[3] = new Vector3(0, -1, 0); //RIGHT
150 Dir_Vectors[4] = new Vector3(0, 0, 1); //UP
151 Dir_Vectors[5] = new Vector3(0, 0, -1); //DOWN
152
153 }
154 #endregion
155
156 #region Status Methods
157 /// <summary>
158 /// Not Used, most likely can be deleted
159 /// </summary>
160 /// <param name="status"></param>
161 public void ChildStatusChange(bool status)
162 {
163 this.childAgent = status;
164
165 if (this.childAgent == true)
166 {
167 this.Velocity = new LLVector3(0, 0, 0);
168 this.Pos = new LLVector3(128, 128, 70);
169
170 }
171 }
172
173 /// <summary>
174 ///
175 /// </summary>
176 /// <param name="pos"></param>
177 public void MakeAvatar(LLVector3 pos)
178 {
179 //this.childAvatar = false;
180 this.Pos = pos;
181 this.newAvatar = true;
182 this.childAgent = false;
183 }
184
185 protected void MakeChildAgent()
186 {
187 this.Velocity = new LLVector3(0, 0, 0);
188 this.Pos = new LLVector3(128, 128, 70);
189 this.childAgent = true;
190 }
191
192 /// <summary>
193 ///
194 /// </summary>
195 /// <param name="pos"></param>
196 public void Teleport(LLVector3 pos)
197 {
198 this.Pos = pos;
199 this.SendTerseUpdateToALLClients();
200 }
201
202 /// <summary>
203 ///
204 /// </summary>
205 public void StopMovement()
206 {
207
208 }
209 #endregion
210
211 #region Event Handlers
212 /// <summary>
213 ///
214 /// </summary>
215 /// <param name="texture"></param>
216 /// <param name="visualParam"></param>
217 public void SetAppearance(byte[] texture, AgentSetAppearancePacket.VisualParamBlock[] visualParam)
218 {
219
220 }
221
222 /// <summary>
223 /// Complete Avatar's movement into the region
224 /// </summary>
225 public void CompleteMovement()
226 {
227 LLVector3 look = this.Velocity;
228 if ((look.X == 0) && (look.Y == 0) && (look.Z == 0))
229 {
230 look = new LLVector3(0.99f, 0.042f, 0);
231 }
232 this.ControllingClient.MoveAgentIntoRegion(m_regionInfo, Pos, look);
233 if (this.childAgent)
234 {
235 this.childAgent = false;
236 }
237 }
238
239 /// <summary>
240 ///
241 /// </summary>
242 /// <param name="pack"></param>
243 public void HandleAgentUpdate(IClientAPI remoteClient, uint flags, LLQuaternion bodyRotation)
244 {
245 int i = 0;
246 bool update_movementflag = false;
247 bool update_rotation = false;
248 bool DCFlagKeyPressed = false;
249 Vector3 agent_control_v3 = new Vector3(0, 0, 0);
250 Quaternion q = new Quaternion(bodyRotation.W, bodyRotation.X, bodyRotation.Y, bodyRotation.Z);
251
252 this.PhysActor.Flying = ((flags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_FLY) != 0);
253
254 if (q != this.bodyRot)
255 {
256 this.bodyRot = q;
257 update_rotation = true;
258 }
259 foreach (Dir_ControlFlags DCF in Enum.GetValues(typeof(Dir_ControlFlags)))
260 {
261 if ((flags & (uint)DCF) != 0)
262 {
263 DCFlagKeyPressed = true;
264 agent_control_v3 += Dir_Vectors[i];
265 if ((movementflag & (uint)DCF) == 0)
266 {
267 movementflag += (byte)(uint)DCF;
268 update_movementflag = true;
269 }
270 }
271 else
272 {
273 if ((movementflag & (uint)DCF) != 0)
274 {
275 movementflag -= (byte)(uint)DCF;
276 update_movementflag = true;
277 }
278 }
279 i++;
280 }
281 if ((update_movementflag) || (update_rotation && DCFlagKeyPressed))
282 {
283 this.AddNewMovement(agent_control_v3, q);
284 }
285 UpdateMovementAnimations(update_movementflag);
286 }
287
288 protected void UpdateMovementAnimations(bool update_movementflag)
289 {
290 if (update_movementflag)
291 {
292 if (movementflag != 0)
293 {
294 if (this._physActor.Flying)
295 {
296 this.SendAnimPack(Animations.AnimsLLUUID["FLY"], 1);
297 }
298 else
299 {
300 this.SendAnimPack(Animations.AnimsLLUUID["WALK"], 1);
301 }
302 }
303 else
304 {
305 this.SendAnimPack(Animations.AnimsLLUUID["STAND"], 1);
306 }
307 }
308
309 }
310
311
312 protected void AddNewMovement(Vector3 vec, Quaternion rotation)
313 {
314 NewForce newVelocity = new NewForce();
315 Vector3 direc = rotation * vec;
316 direc.Normalize();
317
318 direc = direc * ((0.03f) * 128f);
319 if (this._physActor.Flying)
320 direc *= 4;
321
322 newVelocity.X = direc.x;
323 newVelocity.Y = direc.y;
324 newVelocity.Z = direc.z;
325 this.forcesList.Add(newVelocity);
326 }
327
328 #endregion
329
330 #region Overridden Methods
331 /// <summary>
332 ///
333 /// </summary>
334 public override void LandRenegerated()
335 {
336
337 }
338
339 /// <summary>
340 ///
341 /// </summary>
342 public override void update()
343 {
344 if (this.childAgent == false)
345 {
346 if (this.newForce)
347 {
348 this.SendTerseUpdateToALLClients();
349 _updateCount = 0;
350 }
351 else if (movementflag != 0)
352 {
353 _updateCount++;
354 if (_updateCount > 3)
355 {
356 this.SendTerseUpdateToALLClients();
357 _updateCount = 0;
358 }
359 }
360
361 this.CheckForBorderCrossing();
362 }
363 }
364 #endregion
365
366 #region Update Client(s)
367 /// <summary>
368 ///
369 /// </summary>
370 /// <param name="RemoteClient"></param>
371 public void SendTerseUpdateToClient(IClientAPI RemoteClient)
372 {
373 LLVector3 pos = this.Pos;
374 LLVector3 vel = this.Velocity;
375 RemoteClient.SendAvatarTerseUpdate(this.m_regionHandle, 64096, this.LocalId, new LLVector3(pos.X, pos.Y, pos.Z), new LLVector3(vel.X, vel.Y, vel.Z));
376 }
377
378 /// <summary>
379 ///
380 /// </summary>
381 public void SendTerseUpdateToALLClients()
382 {
383 List<ScenePresence> avatars = this.m_world.RequestAvatarList();
384 for (int i = 0; i < avatars.Count; i++)
385 {
386 this.SendTerseUpdateToClient(avatars[i].ControllingClient);
387 }
388 }
389
390 /// <summary>
391 ///
392 /// </summary>
393 /// <param name="remoteAvatar"></param>
394 public void SendFullUpdateToOtherClient(ScenePresence remoteAvatar)
395 {
396 remoteAvatar.ControllingClient.SendAvatarData(m_regionInfo.RegionHandle, this.firstname, this.lastname, this.uuid, this.LocalId, this.Pos, DefaultTexture);
397 }
398
399 public void SendFullUpdateToALLClients()
400 {
401 List<ScenePresence> avatars = this.m_world.RequestAvatarList();
402 foreach (ScenePresence avatar in this.m_world.RequestAvatarList())
403 {
404 this.SendFullUpdateToOtherClient(avatar);
405 avatar.SendFullUpdateToOtherClient(this);
406 }
407 }
408
409 /// <summary>
410 ///
411 /// </summary>
412 public void SendInitialData()
413 {
414 this.ControllingClient.SendAvatarData(m_regionInfo.RegionHandle, this.firstname, this.lastname, this.uuid, this.LocalId, this.Pos, DefaultTexture);
415 if (this.newAvatar)
416 {
417 this.m_world.InformClientOfNeighbours(this.ControllingClient);
418 this.newAvatar = false;
419 }
420 }
421
422 /// <summary>
423 ///
424 /// </summary>
425 /// <param name="OurClient"></param>
426 public void SendOurAppearance(IClientAPI OurClient)
427 {
428 this.ControllingClient.SendWearables(this.Wearables);
429 this.SendFullUpdateToALLClients();
430 this.m_world.SendAllSceneObjectsToClient(this.ControllingClient);
431 }
432
433 /// <summary>
434 ///
435 /// </summary>
436 /// <param name="avatarInfo"></param>
437 public void SendAppearanceToOtherAgent(ScenePresence avatarInfo)
438 {
439
440 }
441
442 /// <summary>
443 ///
444 /// </summary>
445 /// <param name="animID"></param>
446 /// <param name="seq"></param>
447 public void SendAnimPack(LLUUID animID, int seq)
448 {
449 this.current_anim = animID;
450 this.anim_seq = seq;
451 List<ScenePresence> avatars = this.m_world.RequestAvatarList();
452 for (int i = 0; i < avatars.Count; i++)
453 {
454 avatars[i].ControllingClient.SendAnimation(animID, seq, this.ControllingClient.AgentId);
455 }
456 }
457
458 /// <summary>
459 ///
460 /// </summary>
461 public void SendAnimPack()
462 {
463 this.SendAnimPack(this.current_anim, this.anim_seq);
464 }
465 #endregion
466
467 #region Border Crossing Methods
468 /// <summary>
469 ///
470 /// </summary>
471 protected void CheckForBorderCrossing()
472 {
473 LLVector3 pos2 = this.Pos;
474 LLVector3 vel = this.Velocity;
475
476 float timeStep = 0.2f;
477 pos2.X = pos2.X + (vel.X * timeStep);
478 pos2.Y = pos2.Y + (vel.Y * timeStep);
479 pos2.Z = pos2.Z + (vel.Z * timeStep);
480
481 if ((pos2.X < 0) || (pos2.X > 256))
482 {
483 this.CrossToNewRegion();
484 }
485
486 if ((pos2.Y < 0) || (pos2.Y > 256))
487 {
488 this.CrossToNewRegion();
489 }
490 }
491
492 /// <summary>
493 ///
494 /// </summary>
495 protected void CrossToNewRegion()
496 {
497 LLVector3 pos = this.Pos;
498 LLVector3 newpos = new LLVector3(pos.X, pos.Y, pos.Z);
499 uint neighbourx = this.m_regionInfo.RegionLocX;
500 uint neighboury = this.m_regionInfo.RegionLocY;
501
502 if (pos.X < 2)
503 {
504 neighbourx -= 1;
505 newpos.X = 254;
506 }
507 if (pos.X > 253)
508 {
509 neighbourx += 1;
510 newpos.X = 1;
511 }
512 if (pos.Y < 2)
513 {
514 neighboury -= 1;
515 newpos.Y = 254;
516 }
517 if (pos.Y > 253)
518 {
519 neighboury += 1;
520 newpos.Y = 1;
521 }
522
523 LLVector3 vel = this.m_velocity;
524 ulong neighbourHandle = Helpers.UIntsToLong((uint)(neighbourx * 256), (uint)(neighboury * 256));
525 RegionInfo neighbourRegion = this.m_world.RequestNeighbouringRegionInfo(neighbourHandle);
526 if (neighbourRegion != null)
527 {
528 bool res = this.m_world.InformNeighbourOfCrossing(neighbourHandle, this.ControllingClient.AgentId, newpos);
529 if (res)
530 {
531 this.MakeChildAgent();
532 this.ControllingClient.CrossRegion(neighbourHandle, newpos, vel, neighbourRegion.InternalEndPoint);
533 }
534 }
535 }
536 #endregion
537
538 /// <summary>
539 ///
540 /// </summary>
541 public static void LoadAnims()
542 {
543
544 }
545
546 /// <summary>
547 ///
548 /// </summary>
549 public override void updateMovement()
550 {
551 newForce = false;
552 lock (this.forcesList)
553 {
554 if (this.forcesList.Count > 0)
555 {
556 for (int i = 0; i < this.forcesList.Count; i++)
557 {
558 NewForce force = this.forcesList[i];
559
560 this.updateflag = true;
561 this.Velocity = new LLVector3(force.X, force.Y, force.Z);
562 this.newForce = true;
563 }
564 for (int i = 0; i < this.forcesList.Count; i++)
565 {
566 this.forcesList.RemoveAt(0);
567 }
568 }
569 }
570 }
571
572 public static void LoadTextureFile(string name)
573 {
574 FileInfo fInfo = new FileInfo(name);
575 long numBytes = fInfo.Length;
576 FileStream fStream = new FileStream(name, FileMode.Open, FileAccess.Read);
577 BinaryReader br = new BinaryReader(fStream);
578 byte[] data1 = br.ReadBytes((int)numBytes);
579 br.Close();
580 fStream.Close();
581 DefaultTexture = data1;
582 }
583
584 public class NewForce
585 {
586 public float X;
587 public float Y;
588 public float Z;
589
590 public NewForce()
591 {
592
593 }
594 }
595 }
596
597}