AvatarUpdate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using libsecondlife;
  5. using libsecondlife.Packets;
  6. namespace OpenSim.world
  7. {
  8. partial class Avatar
  9. {
  10. public override void update()
  11. {
  12. if (this._physActor == null)
  13. {
  14. //HACKHACK: Note to work out why this entity does not have a physics actor
  15. // and prehaps create one.
  16. return;
  17. }
  18. libsecondlife.LLVector3 pos2 = new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z);
  19. if (this.updateflag)
  20. {
  21. //need to send movement info
  22. //so create the improvedterseobjectupdate packet
  23. //use CreateTerseBlock()
  24. ImprovedTerseObjectUpdatePacket.ObjectDataBlock terseBlock = CreateTerseBlock();
  25. ImprovedTerseObjectUpdatePacket terse = new ImprovedTerseObjectUpdatePacket();
  26. terse.RegionData.RegionHandle = m_regionHandle; // FIXME
  27. terse.RegionData.TimeDilation = 64096;
  28. terse.ObjectData = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock[1];
  29. terse.ObjectData[0] = terseBlock;
  30. foreach (SimClient client in m_clientThreads.Values)
  31. {
  32. client.OutPacket(terse);
  33. }
  34. updateflag = false;
  35. //this._updateCount = 0;
  36. }
  37. else
  38. {
  39. if ((pos2 != this.positionLastFrame) || (this.movementflag == 16))
  40. {
  41. _updateCount++;
  42. if (((!PhysicsEngineFlying) && (_updateCount > 3)) || (PhysicsEngineFlying) && (_updateCount > 0))
  43. {
  44. //It has been a while since last update was sent so lets send one.
  45. ImprovedTerseObjectUpdatePacket.ObjectDataBlock terseBlock = CreateTerseBlock();
  46. ImprovedTerseObjectUpdatePacket terse = new ImprovedTerseObjectUpdatePacket();
  47. terse.RegionData.RegionHandle = m_regionHandle; // FIXME
  48. terse.RegionData.TimeDilation = 64096;
  49. terse.ObjectData = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock[1];
  50. terse.ObjectData[0] = terseBlock;
  51. foreach (SimClient client in m_clientThreads.Values)
  52. {
  53. client.OutPacket(terse);
  54. }
  55. _updateCount = 0;
  56. }
  57. if (this.movementflag == 16)
  58. {
  59. movementflag = 0;
  60. }
  61. }
  62. }
  63. this.positionLastFrame = pos2;
  64. if (!this.ControllingClient.m_sandboxMode)
  65. {
  66. if (pos2.X < 0)
  67. {
  68. ControllingClient.CrossSimBorder(new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z));
  69. }
  70. if (pos2.Y < 0)
  71. {
  72. ControllingClient.CrossSimBorder(new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z));
  73. }
  74. if (pos2.X > 255)
  75. {
  76. ControllingClient.CrossSimBorder(new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z));
  77. }
  78. if (pos2.Y > 255)
  79. {
  80. ControllingClient.CrossSimBorder(new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z));
  81. }
  82. }
  83. }
  84. public ObjectUpdatePacket CreateUpdatePacket()
  85. {
  86. System.Text.Encoding _enc = System.Text.Encoding.ASCII;
  87. //send a objectupdate packet with information about the clients avatar
  88. ObjectUpdatePacket objupdate = new ObjectUpdatePacket();
  89. objupdate.RegionData.RegionHandle = m_regionHandle;
  90. objupdate.RegionData.TimeDilation = 64096;
  91. objupdate.ObjectData = new libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock[1];
  92. objupdate.ObjectData[0] = AvatarTemplate;
  93. //give this avatar object a local id and assign the user a name
  94. objupdate.ObjectData[0].ID = this.localid;
  95. objupdate.ObjectData[0].FullID = ControllingClient.AgentID;
  96. objupdate.ObjectData[0].NameValue = _enc.GetBytes("FirstName STRING RW SV " + firstname + "\nLastName STRING RW SV " + lastname + " \0");
  97. libsecondlife.LLVector3 pos2 = new LLVector3((float)this._physActor.Position.X, (float)this._physActor.Position.Y, (float)this._physActor.Position.Z);
  98. byte[] pb = pos2.GetBytes();
  99. Array.Copy(pb, 0, objupdate.ObjectData[0].ObjectData, 16, pb.Length);
  100. return objupdate;
  101. }
  102. public void SendInitialPosition()
  103. {
  104. System.Text.Encoding _enc = System.Text.Encoding.ASCII;
  105. //send a objectupdate packet with information about the clients avatar
  106. ObjectUpdatePacket objupdate = new ObjectUpdatePacket();
  107. objupdate.RegionData.RegionHandle = m_regionHandle;
  108. objupdate.RegionData.TimeDilation = 64096;
  109. objupdate.ObjectData = new libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock[1];
  110. objupdate.ObjectData[0] = AvatarTemplate;
  111. //give this avatar object a local id and assign the user a name
  112. objupdate.ObjectData[0].ID = this.localid;
  113. this.uuid = objupdate.ObjectData[0].FullID = ControllingClient.AgentID;
  114. objupdate.ObjectData[0].NameValue = _enc.GetBytes("FirstName STRING RW SV " + firstname + "\nLastName STRING RW SV " + lastname + " \0");
  115. libsecondlife.LLVector3 pos2 = new LLVector3((float)this.Pos.X, (float)this.Pos.Y, (float)this.Pos.Z);
  116. byte[] pb = pos2.GetBytes();
  117. Array.Copy(pb, 0, objupdate.ObjectData[0].ObjectData, 16, pb.Length);
  118. m_world._localNumber++;
  119. foreach (SimClient client in m_clientThreads.Values)
  120. {
  121. client.OutPacket(objupdate);
  122. if (client.AgentID != ControllingClient.AgentID)
  123. {
  124. //the below line is already in Simclient.cs at line number 245 , directly below the call to this method
  125. //if there is a problem/bug with that , then lets fix it there rather than duplicating it here
  126. //client.ClientAvatar.SendAppearanceToOtherAgent(this.ControllingClient);
  127. SendAppearanceToOtherAgent(client);
  128. }
  129. }
  130. }
  131. public void SendInitialAppearance()
  132. {
  133. AgentWearablesUpdatePacket aw = new AgentWearablesUpdatePacket();
  134. aw.AgentData.AgentID = this.ControllingClient.AgentID;
  135. aw.AgentData.SerialNum = 0;
  136. aw.AgentData.SessionID = ControllingClient.SessionID;
  137. aw.WearableData = new AgentWearablesUpdatePacket.WearableDataBlock[13];
  138. AgentWearablesUpdatePacket.WearableDataBlock awb;
  139. for (int i = 0; i < 13; i++)
  140. {
  141. awb = new AgentWearablesUpdatePacket.WearableDataBlock();
  142. awb.WearableType = (byte)i;
  143. awb.AssetID = this.Wearables[i].AssetID;
  144. awb.ItemID = this.Wearables[i].ItemID;
  145. aw.WearableData[i] = awb;
  146. }
  147. ControllingClient.OutPacket(aw);
  148. }
  149. public void SendAppearanceToOtherAgent(SimClient userInfo)
  150. {
  151. AvatarAppearancePacket avp = new AvatarAppearancePacket();
  152. avp.VisualParam = new AvatarAppearancePacket.VisualParamBlock[218];
  153. avp.ObjectData.TextureEntry = this.avatarAppearanceTexture.ToBytes();
  154. //a wearable update packets should only be sent about the viewers/agents own avatar not for other avatars
  155. //but it seems that the following code only created the packets and never actually sent them anyway
  156. /*AgentWearablesUpdatePacket aw = new AgentWearablesUpdatePacket();
  157. aw.AgentData.AgentID = this.ControllingClient.AgentID;
  158. aw.AgentData.SessionID = userInfo.SessionID;
  159. aw.AgentData.SerialNum = 0; //removed the use of a random number as a random number could be less than the last number, should have a counter variable for this
  160. aw.WearableData = new AgentWearablesUpdatePacket.WearableDataBlock[13];
  161. AgentWearablesUpdatePacket.WearableDataBlock awb;
  162. for (int i = 0; i < 13; i++)
  163. {
  164. awb = new AgentWearablesUpdatePacket.WearableDataBlock();
  165. awb.WearableType = (byte)i;
  166. awb.AssetID = this.Wearables[i].AssetID;
  167. awb.ItemID = this.Wearables[i].ItemID;
  168. aw.WearableData[i] = awb;
  169. }*/
  170. AvatarAppearancePacket.VisualParamBlock avblock = null;
  171. for (int i = 0; i < 218; i++)
  172. {
  173. avblock = new AvatarAppearancePacket.VisualParamBlock();
  174. avblock.ParamValue = visualParams[i];
  175. avp.VisualParam[i] = avblock;
  176. }
  177. avp.Sender.IsTrial = false;
  178. avp.Sender.ID = ControllingClient.AgentID;
  179. userInfo.OutPacket(avp);
  180. }
  181. public void SetAppearance(AgentSetAppearancePacket appear)
  182. {
  183. LLObject.TextureEntry tex = new LLObject.TextureEntry(appear.ObjectData.TextureEntry, 0, appear.ObjectData.TextureEntry.Length);
  184. this.avatarAppearanceTexture = tex;
  185. for (int i = 0; i < appear.VisualParam.Length; i++)
  186. {
  187. this.visualParams[i] = appear.VisualParam[i].ParamValue;
  188. }
  189. foreach (SimClient client in m_clientThreads.Values)
  190. {
  191. if (client.AgentID != ControllingClient.AgentID)
  192. {
  193. SendAppearanceToOtherAgent(client);
  194. }
  195. }
  196. }
  197. public ImprovedTerseObjectUpdatePacket.ObjectDataBlock CreateTerseBlock()
  198. {
  199. byte[] bytes = new byte[60];
  200. int i = 0;
  201. ImprovedTerseObjectUpdatePacket.ObjectDataBlock dat = new ImprovedTerseObjectUpdatePacket.ObjectDataBlock();
  202. dat.TextureEntry = new byte[0];// AvatarTemplate.TextureEntry;
  203. libsecondlife.LLVector3 pos2 = new LLVector3(0, 0, 0);
  204. lock (m_world.LockPhysicsEngine)
  205. {
  206. pos2 = new LLVector3(this._physActor.Position.X, this._physActor.Position.Y, this._physActor.Position.Z);
  207. }
  208. uint ID = this.localid;
  209. bytes[i++] = (byte)(ID % 256);
  210. bytes[i++] = (byte)((ID >> 8) % 256);
  211. bytes[i++] = (byte)((ID >> 16) % 256);
  212. bytes[i++] = (byte)((ID >> 24) % 256);
  213. bytes[i++] = 0;
  214. bytes[i++] = 1;
  215. i += 14;
  216. bytes[i++] = 128;
  217. bytes[i++] = 63;
  218. byte[] pb = pos2.GetBytes();
  219. Array.Copy(pb, 0, bytes, i, pb.Length);
  220. i += 12;
  221. ushort InternVelocityX;
  222. ushort InternVelocityY;
  223. ushort InternVelocityZ;
  224. Axiom.MathLib.Vector3 internDirec = new Axiom.MathLib.Vector3(0, 0, 0);
  225. lock (m_world.LockPhysicsEngine)
  226. {
  227. internDirec = new Axiom.MathLib.Vector3(this._physActor.Velocity.X, this._physActor.Velocity.Y, this._physActor.Velocity.Z);
  228. }
  229. internDirec = internDirec / 128.0f;
  230. internDirec.x += 1;
  231. internDirec.y += 1;
  232. internDirec.z += 1;
  233. InternVelocityX = (ushort)(32768 * internDirec.x);
  234. InternVelocityY = (ushort)(32768 * internDirec.y);
  235. InternVelocityZ = (ushort)(32768 * internDirec.z);
  236. ushort ac = 32767;
  237. bytes[i++] = (byte)(InternVelocityX % 256);
  238. bytes[i++] = (byte)((InternVelocityX >> 8) % 256);
  239. bytes[i++] = (byte)(InternVelocityY % 256);
  240. bytes[i++] = (byte)((InternVelocityY >> 8) % 256);
  241. bytes[i++] = (byte)(InternVelocityZ % 256);
  242. bytes[i++] = (byte)((InternVelocityZ >> 8) % 256);
  243. //accel
  244. bytes[i++] = (byte)(ac % 256);
  245. bytes[i++] = (byte)((ac >> 8) % 256);
  246. bytes[i++] = (byte)(ac % 256);
  247. bytes[i++] = (byte)((ac >> 8) % 256);
  248. bytes[i++] = (byte)(ac % 256);
  249. bytes[i++] = (byte)((ac >> 8) % 256);
  250. //rot
  251. bytes[i++] = (byte)(ac % 256);
  252. bytes[i++] = (byte)((ac >> 8) % 256);
  253. bytes[i++] = (byte)(ac % 256);
  254. bytes[i++] = (byte)((ac >> 8) % 256);
  255. bytes[i++] = (byte)(ac % 256);
  256. bytes[i++] = (byte)((ac >> 8) % 256);
  257. bytes[i++] = (byte)(ac % 256);
  258. bytes[i++] = (byte)((ac >> 8) % 256);
  259. //rotation vel
  260. bytes[i++] = (byte)(ac % 256);
  261. bytes[i++] = (byte)((ac >> 8) % 256);
  262. bytes[i++] = (byte)(ac % 256);
  263. bytes[i++] = (byte)((ac >> 8) % 256);
  264. bytes[i++] = (byte)(ac % 256);
  265. bytes[i++] = (byte)((ac >> 8) % 256);
  266. dat.Data = bytes;
  267. return (dat);
  268. }
  269. // Sends animation update
  270. public void SendAnimPack()
  271. {
  272. AvatarAnimationPacket ani = new AvatarAnimationPacket();
  273. ani.AnimationSourceList = new AvatarAnimationPacket.AnimationSourceListBlock[1];
  274. ani.AnimationSourceList[0] = new AvatarAnimationPacket.AnimationSourceListBlock();
  275. ani.AnimationSourceList[0].ObjectID = ControllingClient.AgentID;
  276. ani.Sender = new AvatarAnimationPacket.SenderBlock();
  277. ani.Sender.ID = ControllingClient.AgentID;
  278. ani.AnimationList = new AvatarAnimationPacket.AnimationListBlock[1];
  279. ani.AnimationList[0] = new AvatarAnimationPacket.AnimationListBlock();
  280. ani.AnimationList[0].AnimID = this.current_anim;
  281. ani.AnimationList[0].AnimSequenceID = this.anim_seq;
  282. //ControllingClient.OutPacket(ani);
  283. foreach (SimClient client in m_clientThreads.Values)
  284. {
  285. client.OutPacket(ani);
  286. }
  287. }
  288. }
  289. }