ScenePresence.cs 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811
  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 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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.Serialization;
  31. using System.Security.Permissions;
  32. using Axiom.Math;
  33. using libsecondlife;
  34. using libsecondlife.Packets;
  35. using log4net;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Environment.Types;
  38. using OpenSim.Region.Physics.Manager;
  39. namespace OpenSim.Region.Environment.Scenes
  40. {
  41. enum ScriptControlled : int
  42. {
  43. CONTROL_ZERO = 0,
  44. CONTROL_FWD = 1,
  45. CONTROL_BACK = 2,
  46. CONTROL_LEFT = 4,
  47. CONTROL_RIGHT = 8,
  48. CONTROL_UP = 16,
  49. CONTROL_DOWN = 32,
  50. CONTROL_ROT_LEFT = 256,
  51. CONTROL_ROT_RIGHT = 512,
  52. CONTROL_LBUTTON = 268435456,
  53. CONTROL_ML_LBUTTON = 1073741824
  54. }
  55. struct ScriptControllers
  56. {
  57. public LLUUID itemID;
  58. public uint objID;
  59. public ScriptControlled ignoreControls;
  60. public ScriptControlled eventControls;
  61. }
  62. [Serializable]
  63. public class ScenePresence : EntityBase, ISerializable
  64. {
  65. // ~ScenePresence()
  66. // {
  67. // System.Console.WriteLine("[ScenePresence] Destructor called");
  68. // }
  69. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  70. public static byte[] DefaultTexture;
  71. public LLUUID currentParcelUUID = LLUUID.Zero;
  72. private AnimationSet m_animations = new AnimationSet();
  73. private Dictionary<LLUUID, ScriptControllers> scriptedcontrols = new Dictionary<LLUUID, ScriptControllers>();
  74. private ScriptControlled IgnoredControls = ScriptControlled.CONTROL_ZERO;
  75. private ScriptControlled LastCommands = ScriptControlled.CONTROL_ZERO;
  76. private SceneObjectGroup proxyObjectGroup = null;
  77. //private SceneObjectPart proxyObjectPart = null;
  78. public Vector3 lastKnownAllowedPosition = new Vector3();
  79. public bool sentMessageAboutRestrictedParcelFlyingDown = false;
  80. private bool m_updateflag = false;
  81. private byte m_movementflag = 0;
  82. private readonly List<NewForce> m_forcesList = new List<NewForce>();
  83. private short m_updateCount = 0;
  84. private uint m_requestedSitTargetID = 0;
  85. private LLUUID m_requestedSitTargetUUID = LLUUID.Zero;
  86. private LLVector3 m_requestedSitOffset = new LLVector3();
  87. private LLVector3 m_LastFinitePos = new LLVector3();
  88. private float m_sitAvatarHeight = 2.0f;
  89. // experimentally determined "fudge factor" to make sit-target positions
  90. // the same as in SecondLife. Fudge factor was tested for 36 different
  91. // test cases including prims of type box, sphere, cylinder, and torus,
  92. // with varying parameters for sit target location, prim size, prim
  93. // rotation, prim cut, prim twist, prim taper, and prim shear. See mantis
  94. // issue #1716
  95. private static readonly LLVector3 m_sitTargetCorrectionOffset = new LLVector3(0.1f, 0.0f, 0.3f);
  96. private float m_godlevel = 0;
  97. private bool m_attachmentsTransported = false;
  98. private bool m_invulnerable = true;
  99. private LLVector3 m_LastChildAgentUpdatePosition = new LLVector3();
  100. private int m_perfMonMS = 0;
  101. private bool m_setAlwaysRun = false;
  102. private Quaternion m_bodyRot;
  103. public bool IsRestrictedToRegion = false;
  104. public string JID = string.Empty;
  105. // Agent moves with a PID controller causing a force to be exerted.
  106. private bool m_newForce = false;
  107. private bool m_newCoarseLocations = true;
  108. private bool m_gotAllObjectsInScene = false;
  109. private float m_health = 100f;
  110. private LLVector3 m_lastVelocity = LLVector3.Zero;
  111. // Default AV Height
  112. private float m_avHeight = 127.0f;
  113. protected RegionInfo m_regionInfo;
  114. protected ulong crossingFromRegion = 0;
  115. private readonly Vector3[] Dir_Vectors = new Vector3[6];
  116. private LLVector3 lastPhysPos = new LLVector3();
  117. // Position of agent's camera in world (region cordinates)
  118. protected Vector3 m_CameraCenter = new Vector3(0, 0, 0);
  119. // Use these three vectors to figure out what the agent is looking at
  120. // Convert it to a Matrix and/or Quaternion
  121. protected Vector3 m_CameraAtAxis = new Vector3(0, 0, 0);
  122. protected Vector3 m_CameraLeftAxis = new Vector3(0, 0, 0);
  123. protected Vector3 m_CameraUpAxis = new Vector3(0, 0, 0);
  124. private uint m_AgentControlFlags = (uint) 0;
  125. private LLQuaternion m_headrotation = new LLQuaternion();
  126. private byte m_state = (byte) 0;
  127. //Reuse the LLVector3 instead of creating a new one on the UpdateMovement method
  128. private LLVector3 movementvector = new LLVector3();
  129. private bool m_autopilotMoving = false;
  130. private LLVector3 m_autoPilotTarget = LLVector3.Zero;
  131. private bool m_sitAtAutoTarget = false;
  132. private List<LLUUID> m_knownPrimUUID = new List<LLUUID>();
  133. // Agent's Draw distance.
  134. protected float m_DrawDistance = 0f;
  135. protected AvatarAppearance m_appearance;
  136. protected List<SceneObjectGroup> m_attachments = new List<SceneObjectGroup>();
  137. //neighbouring regions we have enabled a child agent in
  138. private readonly List<ulong> m_knownChildRegions = new List<ulong>();
  139. /// <summary>
  140. /// Implemented Control Flags
  141. /// </summary>
  142. private enum Dir_ControlFlags
  143. {
  144. DIR_CONTROL_FLAG_FORWARD = AgentManager.ControlFlags.AGENT_CONTROL_AT_POS,
  145. DIR_CONTROL_FLAG_BACK = AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG,
  146. DIR_CONTROL_FLAG_LEFT = AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS,
  147. DIR_CONTROL_FLAG_RIGHT = AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG,
  148. DIR_CONTROL_FLAG_UP = AgentManager.ControlFlags.AGENT_CONTROL_UP_POS,
  149. DIR_CONTROL_FLAG_DOWN = AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG,
  150. DIR_CONTROL_FLAG_DOWN_NUDGE = AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG
  151. }
  152. /// <summary>
  153. /// Position at which a significant movement was made
  154. /// </summary>
  155. private LLVector3 posLastSignificantMove = new LLVector3();
  156. private UpdateQueue m_partsUpdateQueue = new UpdateQueue();
  157. private Dictionary<LLUUID, ScenePartUpdate> m_updateTimes = new Dictionary<LLUUID, ScenePartUpdate>();
  158. #region Properties
  159. /// <summary>
  160. /// Physical scene representation of this Avatar.
  161. /// </summary>
  162. public PhysicsActor PhysicsActor
  163. {
  164. set { m_physicsActor = value; }
  165. get { return m_physicsActor; }
  166. }
  167. public byte MovementFlag
  168. {
  169. set { m_movementflag = value; }
  170. get { return m_movementflag; }
  171. }
  172. public bool KnownPrim(LLUUID primID)
  173. {
  174. if (m_knownPrimUUID.Contains(primID))
  175. {
  176. return true;
  177. }
  178. m_knownPrimUUID.Add(primID);
  179. return false;
  180. }
  181. public bool Updated
  182. {
  183. set { m_updateflag = value; }
  184. get { return m_updateflag; }
  185. }
  186. public bool Invulnerable
  187. {
  188. set { m_invulnerable = value; }
  189. get { return m_invulnerable; }
  190. }
  191. public float GodLevel
  192. {
  193. get { return m_godlevel; }
  194. }
  195. private readonly ulong m_regionHandle;
  196. public ulong RegionHandle
  197. {
  198. get { return m_regionHandle; }
  199. }
  200. public Vector3 CameraPosition
  201. {
  202. get { return m_CameraCenter; }
  203. }
  204. private readonly string m_firstname;
  205. public string Firstname
  206. {
  207. get { return m_firstname; }
  208. }
  209. private readonly string m_lastname;
  210. public string Lastname
  211. {
  212. get { return m_lastname; }
  213. }
  214. public float DrawDistance
  215. {
  216. get { return m_DrawDistance; }
  217. }
  218. protected bool m_allowMovement = true;
  219. public bool AllowMovement
  220. {
  221. get { return m_allowMovement; }
  222. set { m_allowMovement = value; }
  223. }
  224. /// <summary>
  225. /// This works out to be the ClientView object associated with this avatar, or it's client connection manager
  226. /// </summary>
  227. private IClientAPI m_controllingClient;
  228. protected PhysicsActor m_physicsActor;
  229. public IClientAPI ControllingClient
  230. {
  231. get { return m_controllingClient; }
  232. set { m_controllingClient = value; }
  233. }
  234. protected LLVector3 m_parentPosition = new LLVector3();
  235. /// <summary>
  236. /// Absolute position of this avatar in 'region cordinates'
  237. /// </summary>
  238. public override LLVector3 AbsolutePosition
  239. {
  240. get
  241. {
  242. if (m_physicsActor != null)
  243. {
  244. m_pos.X = m_physicsActor.Position.X;
  245. m_pos.Y = m_physicsActor.Position.Y;
  246. m_pos.Z = m_physicsActor.Position.Z;
  247. }
  248. return m_parentPosition + m_pos;
  249. }
  250. set
  251. {
  252. if (m_physicsActor != null)
  253. {
  254. try
  255. {
  256. lock (m_scene.SyncRoot)
  257. {
  258. m_physicsActor.Position = new PhysicsVector(value.X, value.Y, value.Z);
  259. }
  260. }
  261. catch (Exception e)
  262. {
  263. Console.WriteLine(e.Message);
  264. }
  265. }
  266. m_pos = value;
  267. m_parentPosition=new LLVector3(0, 0, 0);
  268. }
  269. }
  270. /// <summary>
  271. /// Current Velocity of the avatar.
  272. /// </summary>
  273. public override LLVector3 Velocity
  274. {
  275. get
  276. {
  277. if (m_physicsActor != null)
  278. {
  279. m_velocity.X = m_physicsActor.Velocity.X;
  280. m_velocity.Y = m_physicsActor.Velocity.Y;
  281. m_velocity.Z = m_physicsActor.Velocity.Z;
  282. }
  283. return m_velocity;
  284. }
  285. set
  286. {
  287. if (m_physicsActor != null)
  288. {
  289. try
  290. {
  291. lock (m_scene.SyncRoot)
  292. {
  293. m_physicsActor.Velocity = new PhysicsVector(value.X, value.Y, value.Z);
  294. }
  295. }
  296. catch (Exception e)
  297. {
  298. Console.WriteLine(e.Message);
  299. }
  300. }
  301. m_velocity = value;
  302. }
  303. }
  304. /// <summary>
  305. /// If this is true, agent doesn't have a representation in this scene.
  306. /// this is an agent 'looking into' this scene from a nearby scene(region)
  307. ///
  308. /// if False, this agent has a representation in this scene
  309. /// </summary>
  310. private bool m_isChildAgent = true;
  311. public bool IsChildAgent
  312. {
  313. get { return m_isChildAgent; }
  314. set { m_isChildAgent = value; }
  315. }
  316. private uint m_parentID = 0;
  317. public uint ParentID
  318. {
  319. get { return m_parentID; }
  320. set { m_parentID = value; }
  321. }
  322. public float Health
  323. {
  324. get { return m_health; }
  325. set { m_health = value; }
  326. }
  327. /// <summary>
  328. /// These are the region handles known by the avatar.
  329. /// </summary>
  330. public List<ulong> KnownChildRegions
  331. {
  332. get { return m_knownChildRegions; }
  333. }
  334. #endregion
  335. #region Constructor(s)
  336. private ScenePresence(IClientAPI client, Scene world, RegionInfo reginfo)
  337. {
  338. m_regionHandle = reginfo.RegionHandle;
  339. m_controllingClient = client;
  340. m_firstname = m_controllingClient.FirstName;
  341. m_lastname = m_controllingClient.LastName;
  342. m_scene = world;
  343. m_uuid = client.AgentId;
  344. m_regionInfo = reginfo;
  345. m_localId = m_scene.NextLocalId;
  346. AbsolutePosition = m_controllingClient.StartPos;
  347. TrySetMovementAnimation("STAND");
  348. RegisterToEvents();
  349. SetDirectionVectors();
  350. }
  351. public ScenePresence(IClientAPI client, Scene world, RegionInfo reginfo, byte[] visualParams,
  352. AvatarWearable[] wearables)
  353. : this(client, world, reginfo)
  354. {
  355. m_appearance = new AvatarAppearance(m_uuid, wearables, visualParams);
  356. }
  357. public ScenePresence(IClientAPI client, Scene world, RegionInfo reginfo, AvatarAppearance appearance)
  358. : this(client, world, reginfo)
  359. {
  360. m_appearance = appearance;
  361. }
  362. public void RegisterToEvents()
  363. {
  364. m_controllingClient.OnRequestWearables += SendOwnAppearance;
  365. m_controllingClient.OnSetAppearance += SetAppearance;
  366. m_controllingClient.OnCompleteMovementToRegion += CompleteMovement;
  367. m_controllingClient.OnCompleteMovementToRegion += SendInitialData;
  368. m_controllingClient.OnAgentUpdate += HandleAgentUpdate;
  369. m_controllingClient.OnAgentRequestSit += HandleAgentRequestSit;
  370. m_controllingClient.OnAgentSit += HandleAgentSit;
  371. m_controllingClient.OnSetAlwaysRun += HandleSetAlwaysRun;
  372. m_controllingClient.OnStartAnim += HandleStartAnim;
  373. m_controllingClient.OnStopAnim += HandleStopAnim;
  374. m_controllingClient.OnForceReleaseControls += HandleForceReleaseControls;
  375. m_controllingClient.OnAutoPilotGo += DoAutoPilot;
  376. // ControllingClient.OnChildAgentStatus += new StatusChange(this.ChildStatusChange);
  377. // ControllingClient.OnStopMovement += new GenericCall2(this.StopMovement);
  378. }
  379. private void SetDirectionVectors()
  380. {
  381. Dir_Vectors[0] = new Vector3(1, 0, 0); //FORWARD
  382. Dir_Vectors[1] = new Vector3(-1, 0, 0); //BACK
  383. Dir_Vectors[2] = new Vector3(0, 1, 0); //LEFT
  384. Dir_Vectors[3] = new Vector3(0, -1, 0); //RIGHT
  385. Dir_Vectors[4] = new Vector3(0, 0, 1); //UP
  386. Dir_Vectors[5] = new Vector3(0, 0, -1); //DOWN
  387. Dir_Vectors[5] = new Vector3(0, 0, -0.5f); //DOWN_Nudge
  388. }
  389. #endregion
  390. /// <summary>
  391. /// Add the part to the queue of parts for which we need to send an update to the client
  392. /// </summary>
  393. /// <param name="part"></param>
  394. public void QueuePartForUpdate(SceneObjectPart part)
  395. {
  396. //if (InterestList.Contains(part.ParentGroup))
  397. //{
  398. lock (m_partsUpdateQueue)
  399. {
  400. m_partsUpdateQueue.Enqueue(part);
  401. }
  402. // }
  403. }
  404. public uint GenerateClientFlags(LLUUID ObjectID)
  405. {
  406. return m_scene.ExternalChecks.ExternalChecksGenerateClientFlags(m_uuid, ObjectID);
  407. }
  408. /// <summary>
  409. /// Send updates to the client about prims which have been placed on the update queue. We don't
  410. /// necessarily send updates for all the parts on the queue, e.g. if an updates with a more recent
  411. /// timestamp has already been sent.
  412. /// </summary>
  413. public void SendPrimUpdates()
  414. {
  415. // if (m_scene.QuadTree.GetNodeID(this.AbsolutePosition.X, this.AbsolutePosition.Y) != m_currentQuadNode)
  416. //{
  417. // this.UpdateQuadTreeNode();
  418. //this.RefreshQuadObject();
  419. //}
  420. m_perfMonMS = System.Environment.TickCount;
  421. if (!m_gotAllObjectsInScene)
  422. {
  423. if (!m_isChildAgent || m_scene.m_seeIntoRegionFromNeighbor)
  424. {
  425. m_scene.SendAllSceneObjectsToClient(this);
  426. m_gotAllObjectsInScene = true;
  427. }
  428. }
  429. if (m_partsUpdateQueue.Count > 0)
  430. {
  431. bool runUpdate = true;
  432. int updateCount = 0;
  433. while (runUpdate)
  434. {
  435. SceneObjectPart part = m_partsUpdateQueue.Dequeue();
  436. if (m_updateTimes.ContainsKey(part.UUID))
  437. {
  438. ScenePartUpdate update = m_updateTimes[part.UUID];
  439. // We deal with the possibility that two updates occur at the same unix time
  440. // at the update point itself.
  441. if (update.LastFullUpdateTime < part.TimeStampFull)
  442. {
  443. // m_log.DebugFormat(
  444. // "[SCENE PRESENCE]: Fully updating prim {0}, {1} - part timestamp {2}",
  445. // part.Name, part.UUID, part.TimeStampFull);
  446. part.SendFullUpdate(ControllingClient, GenerateClientFlags(part.UUID));
  447. // We'll update to the part's timestamp rather than the current time to
  448. // avoid the race condition whereby the next tick occurs while we are
  449. // doing this update. If this happened, then subsequent updates which occurred
  450. // on the same tick or the next tick of the last update would be ignored.
  451. update.LastFullUpdateTime = part.TimeStampFull;
  452. updateCount++;
  453. }
  454. else if (update.LastTerseUpdateTime <= part.TimeStampTerse)
  455. {
  456. // m_log.DebugFormat(
  457. // "[SCENE PRESENCE]: Tersely updating prim {0}, {1} - part timestamp {2}",
  458. // part.Name, part.UUID, part.TimeStampTerse);
  459. part.SendTerseUpdate(ControllingClient);
  460. update.LastTerseUpdateTime = part.TimeStampTerse;
  461. updateCount++;
  462. }
  463. }
  464. else
  465. {
  466. //never been sent to client before so do full update
  467. part.SendFullUpdate(ControllingClient, GenerateClientFlags(part.UUID));
  468. ScenePartUpdate update = new ScenePartUpdate();
  469. update.FullID = part.UUID;
  470. update.LastFullUpdateTime = part.TimeStampFull;
  471. m_updateTimes.Add(part.UUID, update);
  472. updateCount++;
  473. }
  474. if (m_partsUpdateQueue.Count < 1 || updateCount > 60)
  475. {
  476. runUpdate = false;
  477. }
  478. }
  479. }
  480. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  481. }
  482. public void forceAvatarMovement(Vector3 position, Quaternion rotation)
  483. {
  484. AddNewMovement(position, rotation);
  485. }
  486. #region Status Methods
  487. /// <summary>
  488. /// This turns a child agent, into a root agent
  489. /// This is called when an agent teleports into a region, or if an
  490. /// agent crosses into this region from a neighbor over the border
  491. /// </summary>
  492. public void MakeRootAgent(LLVector3 pos, bool isFlying)
  493. {
  494. // m_log.DebugFormat(
  495. // "[SCENE PRESENCE]: Upgrading child agent {0}, {1} to a root agent in {2} at pos {3}",
  496. // Name, UUID, m_scene.RegionInfo.RegionName, pos);
  497. if (pos.X < 0 || pos.X > Constants.RegionSize || pos.Y < 0 || pos.Y > Constants.RegionSize || pos.Z < 0)
  498. {
  499. LLVector3 emergencyPos = new LLVector3(128, 128, 128);
  500. m_log.WarnFormat(
  501. "[SCENE PRESENCE]: MakeRootAgent() was given an illegal position of {0} for avatar {1}, {2}. Substituting {3}",
  502. pos, Name, UUID, emergencyPos);
  503. pos = emergencyPos;
  504. }
  505. m_isChildAgent = false;
  506. float localAVHeight = 1.56f;
  507. if (m_avHeight != 127.0f)
  508. {
  509. localAVHeight = m_avHeight;
  510. }
  511. float posZLimit = (float)m_scene.GetLandHeight((int)pos.X, (int)pos.Y);
  512. float newPosZ = posZLimit + localAVHeight;
  513. if (posZLimit >= (pos.Z - (localAVHeight / 2)) && !(Single.IsInfinity(newPosZ) || Single.IsNaN(newPosZ)))
  514. {
  515. pos.Z = newPosZ;
  516. }
  517. AbsolutePosition = pos;
  518. AddToPhysicalScene();
  519. m_physicsActor.Flying = isFlying;
  520. SendAnimPack();
  521. m_scene.SwapRootAgentCount(false);
  522. m_scene.CommsManager.UserProfileCacheService.RequestInventoryForUser(m_uuid);
  523. m_scene.AddCapsHandler(m_uuid);
  524. //if (!m_gotAllObjectsInScene)
  525. //{
  526. m_scene.SendAllSceneObjectsToClient(this);
  527. m_scene.EventManager.TriggerOnMakeRootAgent(this);
  528. m_scene.CommsManager.UserService.UpdateUserCurrentRegion(UUID, m_scene.RegionInfo.RegionID, m_scene.RegionInfo.RegionHandle);
  529. //m_gotAllObjectsInScene = true;
  530. //}
  531. }
  532. /// <summary>
  533. /// This turns a root agent into a child agent
  534. /// when an agent departs this region for a neighbor, this gets called.
  535. ///
  536. /// It doesn't get called for a teleport. Reason being, an agent that
  537. /// teleports out may not be anywhere near this region
  538. /// </summary>
  539. public void MakeChildAgent()
  540. {
  541. m_animations.Clear();
  542. // m_log.DebugFormat(
  543. // "[SCENEPRESENCE]: Downgrading child agent {0}, {1} to a root agent in {2}",
  544. // Name, UUID, m_scene.RegionInfo.RegionName);
  545. Velocity = new LLVector3(0, 0, 0);
  546. m_isChildAgent = true;
  547. m_scene.SwapRootAgentCount(true);
  548. RemoveFromPhysicalScene();
  549. m_scene.EventManager.TriggerOnMakeChildAgent(this);
  550. //this.Pos = new LLVector3(128, 128, 70);
  551. }
  552. /// <summary>
  553. /// Removes physics plugin scene representation of this agent if it exists.
  554. /// </summary>
  555. private void RemoveFromPhysicalScene()
  556. {
  557. if (PhysicsActor != null)
  558. {
  559. m_scene.PhysicsScene.RemoveAvatar(PhysicsActor);
  560. m_physicsActor.OnRequestTerseUpdate -= SendTerseUpdateToAllClients;
  561. m_physicsActor.UnSubscribeEvents();
  562. m_physicsActor.OnCollisionUpdate -= PhysicsCollisionUpdate;
  563. PhysicsActor = null;
  564. }
  565. }
  566. /// <summary>
  567. ///
  568. /// </summary>
  569. /// <param name="pos"></param>
  570. public void Teleport(LLVector3 pos)
  571. {
  572. RemoveFromPhysicalScene();
  573. Velocity = new LLVector3(0, 0, 0);
  574. AbsolutePosition = pos;
  575. AddToPhysicalScene();
  576. SendTerseUpdateToAllClients();
  577. }
  578. /// <summary>
  579. ///
  580. /// </summary>
  581. public void StopMovement()
  582. {
  583. }
  584. public void StopFlying()
  585. {
  586. // It turns out to get the agent to stop flying, you have to feed it stop flying velocities
  587. // and send a full object update.
  588. // There's no message to send the client to tell it to stop flying
  589. // Add 1/6 the avatar's height to it's position so it doesn't shoot into the air
  590. // when the avatar stands up
  591. if (m_avHeight != 127.0f)
  592. {
  593. AbsolutePosition = AbsolutePosition + new LLVector3(0, 0, (m_avHeight / 6));
  594. }
  595. else
  596. {
  597. AbsolutePosition = AbsolutePosition + new LLVector3(0, 0, (1.56f / 6));
  598. }
  599. TrySetMovementAnimation("LAND");
  600. SendFullUpdateToAllClients();
  601. }
  602. public void AddNeighbourRegion(ulong regionHandle)
  603. {
  604. if (!m_knownChildRegions.Contains(regionHandle))
  605. {
  606. m_knownChildRegions.Add(regionHandle);
  607. }
  608. }
  609. public void RemoveNeighbourRegion(ulong regionHandle)
  610. {
  611. if (!m_knownChildRegions.Contains(regionHandle))
  612. {
  613. m_knownChildRegions.Remove(regionHandle);
  614. }
  615. }
  616. public List<ulong> GetKnownRegionList()
  617. {
  618. return m_knownChildRegions;
  619. }
  620. #endregion
  621. #region Event Handlers
  622. /// <summary>
  623. /// Sets avatar height in the phyiscs plugin
  624. /// </summary>
  625. internal void SetHeight(float height)
  626. {
  627. m_avHeight = height;
  628. if (PhysicsActor != null)
  629. {
  630. PhysicsVector SetSize = new PhysicsVector(0.45f, 0.6f, m_avHeight);
  631. PhysicsActor.Size = SetSize;
  632. }
  633. }
  634. /// <summary>
  635. /// Complete Avatar's movement into the region
  636. /// </summary>
  637. public void CompleteMovement()
  638. {
  639. LLVector3 look = Velocity;
  640. if ((look.X == 0) && (look.Y == 0) && (look.Z == 0))
  641. {
  642. look = new LLVector3(0.99f, 0.042f, 0);
  643. }
  644. m_controllingClient.MoveAgentIntoRegion(m_regionInfo, AbsolutePosition, look);
  645. if (m_isChildAgent)
  646. {
  647. m_isChildAgent = false;
  648. //this.m_scene.SendAllSceneObjectsToClient(this.ControllingClient);
  649. MakeRootAgent(AbsolutePosition, false);
  650. }
  651. }
  652. /// <summary>
  653. /// This is the event handler for client movement. If a client is moving, this event is triggering.
  654. /// </summary>
  655. public void HandleAgentUpdate(IClientAPI remoteClient, AgentUpdateArgs agentData)
  656. {
  657. //if (m_isChildAgent)
  658. //{
  659. // // Console.WriteLine("DEBUG: HandleAgentUpdate: child agent");
  660. // return;
  661. //}
  662. // Must check for standing up even when PhysicsActor is null,
  663. // since sitting currently removes avatar from physical scene
  664. //m_log.Debug("agentPos:" + AbsolutePosition.ToString());
  665. // This is irritating. Really.
  666. if (!AbsolutePosition.IsFinite())
  667. {
  668. RemoveFromPhysicalScene();
  669. m_log.Error("[AVATAR]: NonFinite Avatar position detected... Reset Position. Mantis this please. Error# 9999902");
  670. m_pos = m_LastFinitePos;
  671. if (!m_pos.IsFinite())
  672. {
  673. m_pos.X = 127f;
  674. m_pos.Y = 127f;
  675. m_pos.Z = 127f;
  676. m_log.Error("[AVATAR]: NonFinite Avatar position detected... Reset Position. Mantis this please. Error# 9999903");
  677. }
  678. AddToPhysicalScene();
  679. }
  680. else
  681. {
  682. m_LastFinitePos = m_pos;
  683. }
  684. //m_physicsActor.AddForce(new PhysicsVector(999999999, 99999999, 999999999999999), true);
  685. //ILandObject land = LandChannel.GetLandObject(agent.startpos.X, agent.startpos.Y);
  686. //if (land != null)
  687. //{
  688. //if (land.landData.landingType == (byte)1 && land.landData.userLocation != LLVector3.Zero)
  689. //{
  690. // agent.startpos = land.landData.userLocation;
  691. //}
  692. //}
  693. m_perfMonMS = System.Environment.TickCount;
  694. uint flags = agentData.ControlFlags;
  695. LLQuaternion bodyRotation = agentData.BodyRotation;
  696. // Camera location in world. We'll need to raytrace
  697. // from this location from time to time.
  698. m_CameraCenter.x = agentData.CameraCenter.X;
  699. m_CameraCenter.y = agentData.CameraCenter.Y;
  700. m_CameraCenter.z = agentData.CameraCenter.Z;
  701. // Use these three vectors to figure out what the agent is looking at
  702. // Convert it to a Matrix and/or Quaternion
  703. m_CameraAtAxis.x = agentData.CameraAtAxis.X;
  704. m_CameraAtAxis.y = agentData.CameraAtAxis.Y;
  705. m_CameraAtAxis.z = agentData.CameraAtAxis.Z;
  706. m_CameraLeftAxis.x = agentData.CameraLeftAxis.X;
  707. m_CameraLeftAxis.y = agentData.CameraLeftAxis.Y;
  708. m_CameraLeftAxis.z = agentData.CameraLeftAxis.Z;
  709. m_CameraUpAxis.x = agentData.CameraUpAxis.X;
  710. m_CameraUpAxis.y = agentData.CameraUpAxis.Y;
  711. m_CameraUpAxis.z = agentData.CameraUpAxis.Z;
  712. // The Agent's Draw distance setting
  713. m_DrawDistance = agentData.Far;
  714. if ((flags & (uint) AgentManager.ControlFlags.AGENT_CONTROL_STAND_UP) != 0)
  715. {
  716. StandUp();
  717. }
  718. lock (scriptedcontrols)
  719. {
  720. if (scriptedcontrols.Count > 0)
  721. {
  722. SendControlToScripts(flags, LastCommands);
  723. flags = this.RemoveIgnoredControls(flags, IgnoredControls);
  724. }
  725. }
  726. if (PhysicsActor == null)
  727. {
  728. return;
  729. }
  730. if (m_autopilotMoving)
  731. CheckAtSitTarget();
  732. if ((flags & (uint) AgentManager.ControlFlags.AGENT_CONTROL_SIT_ON_GROUND) != 0)
  733. {
  734. // TODO: This doesn't enable the "stand up" button on the viewer yet (probably a parent ID problem)
  735. // m_parentID = ???
  736. TrySetMovementAnimation("SIT_GROUND");
  737. }
  738. // In the future, these values might need to go global.
  739. // Here's where you get them.
  740. // m_AgentControlFlags = flags;
  741. // m_headrotation = agentData.AgentData.HeadRotation;
  742. // m_state = agentData.AgentData.State;
  743. if (m_allowMovement)
  744. {
  745. int i = 0;
  746. bool update_movementflag = false;
  747. bool update_rotation = false;
  748. bool DCFlagKeyPressed = false;
  749. Vector3 agent_control_v3 = new Vector3(0, 0, 0);
  750. Quaternion q = new Quaternion(bodyRotation.W, bodyRotation.X, bodyRotation.Y, bodyRotation.Z);
  751. if (PhysicsActor != null)
  752. {
  753. bool oldflying = PhysicsActor.Flying;
  754. PhysicsActor.Flying = ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_FLY) != 0);
  755. if (PhysicsActor.Flying != oldflying)
  756. {
  757. update_movementflag = true;
  758. }
  759. }
  760. if (q != m_bodyRot)
  761. {
  762. m_bodyRot = q;
  763. update_rotation = true;
  764. }
  765. if (m_parentID == 0)
  766. {
  767. foreach (Dir_ControlFlags DCF in Enum.GetValues(typeof (Dir_ControlFlags)))
  768. {
  769. if ((flags & (uint) DCF) != 0)
  770. {
  771. DCFlagKeyPressed = true;
  772. try
  773. {
  774. agent_control_v3 += Dir_Vectors[i];
  775. }
  776. catch (IndexOutOfRangeException)
  777. {
  778. // Why did I get this?
  779. }
  780. if ((m_movementflag & (uint) DCF) == 0)
  781. {
  782. m_movementflag += (byte) (uint) DCF;
  783. update_movementflag = true;
  784. }
  785. }
  786. else
  787. {
  788. if ((m_movementflag & (uint) DCF) != 0)
  789. {
  790. m_movementflag -= (byte) (uint) DCF;
  791. update_movementflag = true;
  792. }
  793. }
  794. i++;
  795. }
  796. }
  797. // Cause the avatar to stop flying if it's colliding
  798. // with something with the down arrow pressed.
  799. // Only do this if we're flying
  800. if (m_physicsActor != null && m_physicsActor.Flying)
  801. {
  802. // Are the landing controls requirements filled?
  803. bool controlland = (((flags & (uint) AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG) != 0) ||
  804. ((flags & (uint) AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG) != 0));
  805. // Are the collision requirements fulfilled?
  806. bool colliding = (m_physicsActor.IsColliding == true);
  807. if (m_physicsActor.Flying && colliding && controlland)
  808. {
  809. StopFlying();
  810. }
  811. }
  812. if (update_movementflag || (update_rotation && DCFlagKeyPressed))
  813. {
  814. AddNewMovement(agent_control_v3, q);
  815. if (update_movementflag)
  816. UpdateMovementAnimations();
  817. }
  818. }
  819. m_scene.EventManager.TriggerOnClientMovement(this);
  820. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  821. }
  822. public void DoAutoPilot(uint not_used, LLVector3 Pos, IClientAPI remote_client)
  823. {
  824. m_autopilotMoving = true;
  825. m_autoPilotTarget = Pos;
  826. m_sitAtAutoTarget = false;
  827. PrimitiveBaseShape proxy = PrimitiveBaseShape.Default;
  828. //proxy.PCode = (byte)PCode.ParticleSystem;
  829. uint nextUUID = m_scene.NextLocalId;
  830. proxyObjectGroup = new SceneObjectGroup(m_scene, m_scene.RegionInfo.RegionHandle, UUID, nextUUID, Pos, new LLQuaternion(Rotation.x, Rotation.y, Rotation.z, Rotation.w), proxy);
  831. if (proxyObjectGroup != null)
  832. {
  833. proxyObjectGroup.SendGroupFullUpdate();
  834. remote_client.SendSitResponse(proxyObjectGroup.UUID, LLVector3.Zero, LLQuaternion.Identity, true, LLVector3.Zero, LLVector3.Zero, false);
  835. m_scene.DeleteSceneObject(proxyObjectGroup);
  836. }
  837. else
  838. {
  839. m_autopilotMoving = false;
  840. m_autoPilotTarget = LLVector3.Zero;
  841. ControllingClient.SendAlertMessage("Autopilot cancelled");
  842. }
  843. }
  844. private void CheckAtSitTarget()
  845. {
  846. //m_log.Debug("[AUTOPILOT]: " + Util.GetDistanceTo(AbsolutePosition, m_autoPilotTarget).ToString());
  847. if (Util.GetDistanceTo(AbsolutePosition, m_autoPilotTarget) <= 1.5)
  848. {
  849. if (m_sitAtAutoTarget)
  850. {
  851. SceneObjectPart part = m_scene.GetSceneObjectPart(m_requestedSitTargetUUID);
  852. if (part != null)
  853. {
  854. AbsolutePosition = part.AbsolutePosition;
  855. Velocity = new LLVector3(0, 0, 0);
  856. SendFullUpdateToAllClients();
  857. //HandleAgentSit(ControllingClient, m_requestedSitTargetUUID);
  858. }
  859. //ControllingClient.SendSitResponse(m_requestedSitTargetID, m_requestedSitOffset, LLQuaternion.Identity, false, LLVector3.Zero, LLVector3.Zero, false);
  860. m_requestedSitTargetUUID = LLUUID.Zero;
  861. }
  862. else
  863. {
  864. //ControllingClient.SendAlertMessage("Autopilot cancelled");
  865. //SendTerseUpdateToAllClients();
  866. //PrimitiveBaseShape proxy = PrimitiveBaseShape.Default;
  867. //proxy.PCode = (byte)PCode.ParticleSystem;
  868. ////uint nextUUID = m_scene.NextLocalId;
  869. //proxyObjectGroup = new SceneObjectGroup(m_scene, m_scene.RegionInfo.RegionHandle, UUID, nextUUID, m_autoPilotTarget, LLQuaternion.Identity, proxy);
  870. //if (proxyObjectGroup != null)
  871. //{
  872. //proxyObjectGroup.SendGroupFullUpdate();
  873. //ControllingClient.SendSitResponse(LLUUID.Zero, m_autoPilotTarget, LLQuaternion.Identity, true, LLVector3.Zero, LLVector3.Zero, false);
  874. //m_scene.DeleteSceneObject(proxyObjectGroup);
  875. //}
  876. }
  877. m_autoPilotTarget = LLVector3.Zero;
  878. m_autopilotMoving = false;
  879. }
  880. }
  881. /// <summary>
  882. /// Perform the logic necessary to stand the client up. This method also executes
  883. /// the stand animation.
  884. /// </summary>
  885. public void StandUp()
  886. {
  887. if (m_parentID != 0)
  888. {
  889. SceneObjectPart part = m_scene.GetSceneObjectPart(m_parentID);
  890. if (part != null)
  891. {
  892. // Reset sit target.
  893. if (part.GetAvatarOnSitTarget() == UUID)
  894. part.SetAvatarOnSitTarget(LLUUID.Zero);
  895. m_parentPosition = part.GetWorldPosition();
  896. }
  897. if (m_physicsActor == null)
  898. {
  899. AddToPhysicalScene();
  900. }
  901. m_pos += m_parentPosition + new LLVector3(0.0f, 0.0f, 2.0f*m_sitAvatarHeight);
  902. m_parentPosition = new LLVector3();
  903. m_parentID = 0;
  904. SendFullUpdateToAllClients();
  905. m_requestedSitTargetID = 0;
  906. if (m_physicsActor != null)
  907. {
  908. SetHeight(m_avHeight);
  909. }
  910. }
  911. TrySetMovementAnimation("STAND");
  912. }
  913. private SceneObjectPart FindNextAvailableSitTarget(LLUUID targetID)
  914. {
  915. SceneObjectPart targetPart = m_scene.GetSceneObjectPart(targetID);
  916. if (targetPart == null)
  917. return null;
  918. // If the primitive the player clicked on has a sit target and that sit target is not full, that sit target is used.
  919. // If the primitive the player clicked on has no sit target, and one or more other linked objects have sit targets that are not full, the sit target of the object with the lowest link number will be used.
  920. // Get our own copy of the part array, and sort into the order we want to test
  921. SceneObjectPart[] partArray = targetPart.ParentGroup.GetParts();
  922. Array.Sort(partArray, delegate(SceneObjectPart p1, SceneObjectPart p2) {
  923. // we want the originally selected part first, then the rest in link order -- so make the selected part link num (-1)
  924. int linkNum1 = p1==targetPart ? -1 : p1.LinkNum;
  925. int linkNum2 = p2==targetPart ? -1 : p2.LinkNum;
  926. return linkNum1 - linkNum2;
  927. }
  928. );
  929. //look for prims with explicit sit targets that are available
  930. foreach (SceneObjectPart part in partArray)
  931. {
  932. // Is a sit target available?
  933. Vector3 avSitOffSet = part.SitTargetPosition;
  934. Quaternion avSitOrientation = part.SitTargetOrientation;
  935. LLUUID avOnTargetAlready = part.GetAvatarOnSitTarget();
  936. bool SitTargetUnOccupied = (!(avOnTargetAlready != LLUUID.Zero));
  937. bool SitTargetisSet =
  938. (!(avSitOffSet.x == 0 && avSitOffSet.y == 0 && avSitOffSet.z == 0 && avSitOrientation.w == 0 &&
  939. avSitOrientation.x == 0 && avSitOrientation.y == 0 && avSitOrientation.z == 1));
  940. if (SitTargetisSet && SitTargetUnOccupied)
  941. {
  942. //switch the target to this prim
  943. return part;
  944. }
  945. }
  946. // no explicit sit target found - use original target
  947. return targetPart;
  948. }
  949. private void SendSitResponse(IClientAPI remoteClient, LLUUID targetID, LLVector3 offset)
  950. {
  951. bool autopilot = true;
  952. LLVector3 pos = new LLVector3();
  953. LLQuaternion sitOrientation = new LLQuaternion(0, 0, 0, 1);
  954. //SceneObjectPart part = m_scene.GetSceneObjectPart(targetID);
  955. SceneObjectPart part = FindNextAvailableSitTarget(targetID);
  956. if (part != null)
  957. {
  958. // TODO: determine position to sit at based on scene geometry; don't trust offset from client
  959. // see http://wiki.secondlife.com/wiki/User:Andrew_Linden/Office_Hours/2007_11_06 for details on how LL does it
  960. // Is a sit target available?
  961. Vector3 avSitOffSet = part.SitTargetPosition;
  962. Quaternion avSitOrientation = part.SitTargetOrientation;
  963. LLUUID avOnTargetAlready = part.GetAvatarOnSitTarget();
  964. bool SitTargetUnOccupied = (!(avOnTargetAlready != LLUUID.Zero));
  965. bool SitTargetisSet =
  966. (!(avSitOffSet.x == 0 && avSitOffSet.y == 0 && avSitOffSet.z == 0 && avSitOrientation.w == 0 &&
  967. avSitOrientation.x == 0 && avSitOrientation.y == 0 && avSitOrientation.z == 1));
  968. if (SitTargetisSet && SitTargetUnOccupied)
  969. {
  970. part.SetAvatarOnSitTarget(UUID);
  971. offset = new LLVector3(avSitOffSet.x, avSitOffSet.y, avSitOffSet.z);
  972. sitOrientation =
  973. new LLQuaternion(avSitOrientation.w, avSitOrientation.x, avSitOrientation.y, avSitOrientation.z);
  974. autopilot = false;
  975. }
  976. pos = part.AbsolutePosition + offset;
  977. //if (Math.Abs(part.AbsolutePosition.Z - AbsolutePosition.Z) > 1)
  978. //{
  979. // offset = pos;
  980. //autopilot = false;
  981. //}
  982. if (m_physicsActor != null)
  983. {
  984. // If we're not using the client autopilot, we're immediately warping the avatar to the location
  985. // We can remove the physicsActor until they stand up.
  986. m_sitAvatarHeight = m_physicsActor.Size.Z;
  987. if (autopilot)
  988. {
  989. if (Util.GetDistanceTo(AbsolutePosition, pos) < 4.5)
  990. {
  991. autopilot = false;
  992. RemoveFromPhysicalScene();
  993. AbsolutePosition = pos + new LLVector3(0.0f, 0.0f, m_sitAvatarHeight);
  994. }
  995. }
  996. else
  997. {
  998. RemoveFromPhysicalScene();
  999. }
  1000. }
  1001. }
  1002. ControllingClient.SendSitResponse(targetID, offset, sitOrientation, autopilot, LLVector3.Zero, LLVector3.Zero, false);
  1003. m_requestedSitTargetUUID = targetID;
  1004. // This calls HandleAgentSit twice, once from here, and the client calls
  1005. // HandleAgentSit itself after it gets to the location
  1006. // It doesn't get to the location until we've moved them there though
  1007. // which happens in HandleAgentSit :P
  1008. m_autopilotMoving = autopilot;
  1009. m_autoPilotTarget = pos;
  1010. m_sitAtAutoTarget = autopilot;
  1011. if (!autopilot)
  1012. HandleAgentSit(remoteClient, UUID);
  1013. }
  1014. public void HandleAgentRequestSit(IClientAPI remoteClient, LLUUID agentID, LLUUID targetID, LLVector3 offset)
  1015. {
  1016. if (m_parentID != 0)
  1017. {
  1018. StandUp();
  1019. }
  1020. //SceneObjectPart part = m_scene.GetSceneObjectPart(targetID);
  1021. SceneObjectPart part = FindNextAvailableSitTarget(targetID);
  1022. if (part != null)
  1023. {
  1024. m_requestedSitTargetID = part.LocalId;
  1025. m_requestedSitOffset = offset;
  1026. }
  1027. else
  1028. {
  1029. m_log.Warn("Sit requested on unknown object: " + targetID.ToString());
  1030. }
  1031. SendSitResponse(remoteClient, targetID, offset);
  1032. }
  1033. public void HandleAgentSit(IClientAPI remoteClient, LLUUID agentID)
  1034. {
  1035. SceneObjectPart part = m_scene.GetSceneObjectPart(m_requestedSitTargetID);
  1036. if (m_sitAtAutoTarget || !m_autopilotMoving)
  1037. {
  1038. if (part != null)
  1039. {
  1040. if (part.GetAvatarOnSitTarget() == UUID)
  1041. {
  1042. Vector3 sitTargetPos = part.SitTargetPosition;
  1043. Quaternion sitTargetOrient = part.SitTargetOrientation;
  1044. //Quaternion vq = new Quaternion(sitTargetPos.x, sitTargetPos.y+0.2f, sitTargetPos.z+0.2f, 0);
  1045. //Quaternion nq = new Quaternion(sitTargetOrient.w, -sitTargetOrient.x, -sitTargetOrient.y, -sitTargetOrient.z);
  1046. //Quaternion result = (sitTargetOrient * vq) * nq;
  1047. m_pos = new LLVector3(sitTargetPos.x, sitTargetPos.y, sitTargetPos.z);
  1048. m_pos += m_sitTargetCorrectionOffset;
  1049. m_bodyRot = sitTargetOrient;
  1050. //Rotation = sitTargetOrient;
  1051. m_parentPosition = part.AbsolutePosition;
  1052. //SendTerseUpdateToAllClients();
  1053. }
  1054. else
  1055. {
  1056. m_pos -= part.AbsolutePosition;
  1057. m_parentPosition = part.AbsolutePosition;
  1058. }
  1059. }
  1060. else
  1061. {
  1062. return;
  1063. }
  1064. }
  1065. m_parentID = m_requestedSitTargetID;
  1066. Velocity = new LLVector3(0, 0, 0);
  1067. RemoveFromPhysicalScene();
  1068. TrySetMovementAnimation("SIT");
  1069. SendFullUpdateToAllClients();
  1070. // This may seem stupid, but Our Full updates don't send avatar rotation :P
  1071. // So we're also sending a terse update (which has avatar rotation)
  1072. SendTerseUpdateToAllClients();
  1073. }
  1074. /// <summary>
  1075. /// Event handler for the 'Always run' setting on the client
  1076. /// Tells the physics plugin to increase speed of movement.
  1077. /// </summary>
  1078. public void HandleSetAlwaysRun(IClientAPI remoteClient, bool SetAlwaysRun)
  1079. {
  1080. m_setAlwaysRun = SetAlwaysRun;
  1081. if (PhysicsActor != null)
  1082. {
  1083. PhysicsActor.SetAlwaysRun = SetAlwaysRun;
  1084. }
  1085. }
  1086. public void AddAnimation(LLUUID animID)
  1087. {
  1088. if (m_isChildAgent)
  1089. return;
  1090. if (m_animations.Add(animID, m_controllingClient.NextAnimationSequenceNumber))
  1091. {
  1092. SendAnimPack();
  1093. }
  1094. }
  1095. public void AddAnimation(string name)
  1096. {
  1097. if (m_isChildAgent)
  1098. return;
  1099. LLUUID animID = m_controllingClient.GetDefaultAnimation(name);
  1100. if (animID == LLUUID.Zero)
  1101. return;
  1102. AddAnimation(animID);
  1103. }
  1104. public void RemoveAnimation(LLUUID animID)
  1105. {
  1106. if (m_isChildAgent)
  1107. return;
  1108. if (m_animations.Remove(animID))
  1109. {
  1110. SendAnimPack();
  1111. }
  1112. }
  1113. public void RemoveAnimation(string name)
  1114. {
  1115. if (m_isChildAgent)
  1116. return;
  1117. LLUUID animID = m_controllingClient.GetDefaultAnimation(name);
  1118. if (animID == LLUUID.Zero)
  1119. return;
  1120. RemoveAnimation(animID);
  1121. }
  1122. public void HandleStartAnim(IClientAPI remoteClient, LLUUID animID)
  1123. {
  1124. AddAnimation(animID);
  1125. }
  1126. public void HandleStopAnim(IClientAPI remoteClient, LLUUID animID)
  1127. {
  1128. RemoveAnimation(animID);
  1129. }
  1130. /// <summary>
  1131. /// The movement animation is reserved for "main" animations
  1132. /// that are mutually exclusive, e.g. flying and sitting.
  1133. /// </summary>
  1134. protected void SetMovementAnimation(LLUUID animID)
  1135. {
  1136. if (m_animations.SetDefaultAnimation(animID, m_controllingClient.NextAnimationSequenceNumber))
  1137. {
  1138. SendAnimPack();
  1139. }
  1140. }
  1141. /// <summary>
  1142. /// The movement animation is reserved for "main" animations
  1143. /// that are mutually exclusive, e.g. flying and sitting.
  1144. /// </summary>
  1145. protected void TrySetMovementAnimation(string anim)
  1146. {
  1147. if (m_animations.TrySetDefaultAnimation(anim, m_controllingClient.NextAnimationSequenceNumber))
  1148. {
  1149. SendAnimPack();
  1150. }
  1151. }
  1152. /// <summary>
  1153. /// This method determines the proper movement related animation
  1154. /// </summary>
  1155. protected string GetMovementAnimation()
  1156. {
  1157. if (m_movementflag != 0)
  1158. {
  1159. // We are moving
  1160. if (PhysicsActor != null && PhysicsActor.Flying)
  1161. {
  1162. return "FLY";
  1163. }
  1164. else if (PhysicsActor != null && (m_movementflag & (uint) AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG) != 0 &&
  1165. PhysicsActor.IsColliding)
  1166. {
  1167. if ((m_movementflag & (uint) AgentManager.ControlFlags.AGENT_CONTROL_AT_POS) != 0 ||
  1168. (m_movementflag & (uint) AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG) != 0)
  1169. {
  1170. return "CROUCHWALK";
  1171. }
  1172. else
  1173. {
  1174. return "CROUCH";
  1175. }
  1176. }
  1177. else if (PhysicsActor != null && !PhysicsActor.IsColliding && PhysicsActor.Velocity.Z < -6)
  1178. {
  1179. return "FALLDOWN";
  1180. }
  1181. else if (PhysicsActor != null && !PhysicsActor.IsColliding && Velocity.Z > 1e-6 &&
  1182. (m_movementflag & (uint) AgentManager.ControlFlags.AGENT_CONTROL_UP_POS) != 0)
  1183. {
  1184. return "JUMP";
  1185. }
  1186. else if (m_setAlwaysRun)
  1187. {
  1188. return "RUN";
  1189. }
  1190. else
  1191. {
  1192. return "WALK";
  1193. }
  1194. }
  1195. else
  1196. {
  1197. // We are not moving
  1198. if (PhysicsActor != null && !PhysicsActor.IsColliding && PhysicsActor.Velocity.Z < -6 && !PhysicsActor.Flying)
  1199. {
  1200. return "FALLDOWN";
  1201. }
  1202. else if (PhysicsActor != null && !PhysicsActor.IsColliding && Velocity.Z > 6 && !PhysicsActor.Flying)
  1203. {
  1204. // HACK: We check if Velocity.Z > 6 for this animation in order to avoid false positives during normal movement.
  1205. // TODO: set this animation only when on the ground and UP_POS is received?
  1206. // This is the standing jump
  1207. return "JUMP";
  1208. }
  1209. else if (PhysicsActor != null && PhysicsActor.Flying)
  1210. {
  1211. return "HOVER";
  1212. }
  1213. else
  1214. {
  1215. return "STAND";
  1216. }
  1217. }
  1218. }
  1219. protected void UpdateMovementAnimations()
  1220. {
  1221. TrySetMovementAnimation(GetMovementAnimation());
  1222. }
  1223. /// <summary>
  1224. /// Adds a new movement
  1225. /// </summary>
  1226. protected void AddNewMovement(Vector3 vec, Quaternion rotation)
  1227. {
  1228. if (m_isChildAgent)
  1229. {
  1230. Console.WriteLine("DEBUG: AddNewMovement: child agent");
  1231. return;
  1232. }
  1233. m_perfMonMS = System.Environment.TickCount;
  1234. m_rotation = rotation;
  1235. NewForce newVelocity = new NewForce();
  1236. Vector3 direc = rotation*vec;
  1237. direc.Normalize();
  1238. direc *= 0.03f*128f;
  1239. if (m_physicsActor.Flying)
  1240. {
  1241. direc *= 4;
  1242. //bool controlland = (((m_AgentControlFlags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG) != 0) || ((m_AgentControlFlags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG) != 0));
  1243. //bool colliding = (m_physicsActor.IsColliding==true);
  1244. //if (controlland)
  1245. // m_log.Info("[AGENT]: landCommand");
  1246. //if (colliding)
  1247. // m_log.Info("[AGENT]: colliding");
  1248. //if (m_physicsActor.Flying && colliding && controlland)
  1249. //{
  1250. // StopFlying();
  1251. // m_log.Info("[AGENT]: Stop FLying");
  1252. //}
  1253. }
  1254. else
  1255. {
  1256. if (!m_physicsActor.Flying && m_physicsActor.IsColliding)
  1257. {
  1258. if (direc.z > 2.0f)
  1259. {
  1260. direc.z *= 3;
  1261. // TODO: PreJump and jump happen too quickly. Many times prejump gets ignored.
  1262. TrySetMovementAnimation("PREJUMP");
  1263. TrySetMovementAnimation("JUMP");
  1264. }
  1265. }
  1266. }
  1267. newVelocity.X = direc.x;
  1268. newVelocity.Y = direc.y;
  1269. newVelocity.Z = direc.z;
  1270. m_forcesList.Add(newVelocity);
  1271. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1272. }
  1273. #endregion
  1274. #region Overridden Methods
  1275. /// <summary>
  1276. ///
  1277. /// </summary>
  1278. public override void Update()
  1279. {
  1280. SendPrimUpdates();
  1281. if (m_newCoarseLocations)
  1282. {
  1283. SendCoarseLocations();
  1284. m_newCoarseLocations = false;
  1285. }
  1286. if (m_isChildAgent == false)
  1287. {
  1288. if (m_newForce) // user movement 'forces' (ie commands to move)
  1289. {
  1290. SendTerseUpdateToAllClients();
  1291. m_updateCount = 0;
  1292. }
  1293. else if (m_movementflag != 0) // scripted movement (?)
  1294. {
  1295. m_updateCount++;
  1296. if (m_updateCount > 3)
  1297. {
  1298. SendTerseUpdateToAllClients();
  1299. m_updateCount = 0;
  1300. }
  1301. }
  1302. else if ((Util.GetDistanceTo(lastPhysPos, AbsolutePosition) > 0.02) || (Util.GetDistanceTo(m_lastVelocity, m_velocity) > 0.02)) // physics-related movement
  1303. {
  1304. // Send Terse Update to all clients updates lastPhysPos and m_lastVelocity
  1305. // doing the above assures us that we know what we sent the clients last
  1306. SendTerseUpdateToAllClients();
  1307. m_updateCount = 0;
  1308. }
  1309. // followed suggestion from mic bowman. reversed the two lines below.
  1310. CheckForBorderCrossing();
  1311. CheckForSignificantMovement(); // sends update to the modules.
  1312. }
  1313. }
  1314. #endregion
  1315. #region Update Client(s)
  1316. /// <summary>
  1317. /// Sends a location update to the client connected to this scenePresence
  1318. /// </summary>
  1319. /// <param name="remoteClient"></param>
  1320. public void SendTerseUpdateToClient(IClientAPI remoteClient)
  1321. {
  1322. m_perfMonMS = System.Environment.TickCount;
  1323. LLVector3 pos = m_pos;
  1324. LLVector3 vel = Velocity;
  1325. LLQuaternion rot = new LLQuaternion(m_bodyRot.x, m_bodyRot.y, m_bodyRot.z, m_bodyRot.w);
  1326. remoteClient.SendAvatarTerseUpdate(m_regionHandle, (ushort)(m_scene.TimeDilation * (float)ushort.MaxValue), LocalId, new LLVector3(pos.X, pos.Y, pos.Z),
  1327. new LLVector3(vel.X, vel.Y, vel.Z), rot);
  1328. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1329. m_scene.AddAgentUpdates(1);
  1330. }
  1331. /// <summary>
  1332. /// Send a location/velocity/accelleration update to all agents in scene
  1333. /// </summary>
  1334. public void SendTerseUpdateToAllClients()
  1335. {
  1336. m_perfMonMS = System.Environment.TickCount;
  1337. m_scene.Broadcast(SendTerseUpdateToClient);
  1338. m_lastVelocity = m_velocity;
  1339. lastPhysPos = AbsolutePosition;
  1340. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1341. }
  1342. public void SendCoarseLocations()
  1343. {
  1344. m_perfMonMS = System.Environment.TickCount;
  1345. List<LLVector3> CoarseLocations = new List<LLVector3>();
  1346. List<ScenePresence> avatars = m_scene.GetAvatars();
  1347. for (int i = 0; i < avatars.Count; i++)
  1348. {
  1349. if (avatars[i] != this)
  1350. {
  1351. CoarseLocations.Add(avatars[i].m_pos);
  1352. }
  1353. }
  1354. m_controllingClient.SendCoarseLocationUpdate(CoarseLocations);
  1355. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1356. }
  1357. public void CoarseLocationChange()
  1358. {
  1359. m_newCoarseLocations = true;
  1360. }
  1361. /// <summary>
  1362. /// Tell other client about this avatar (The client previously didn't know or had outdated details about this avatar)
  1363. /// </summary>
  1364. /// <param name="remoteAvatar"></param>
  1365. public void SendFullUpdateToOtherClient(ScenePresence remoteAvatar)
  1366. {
  1367. // 2 stage check is needed.
  1368. if (remoteAvatar == null)
  1369. return;
  1370. IClientAPI cl=remoteAvatar.ControllingClient;
  1371. if (cl == null)
  1372. return;
  1373. if (m_appearance.Texture == null)
  1374. return;
  1375. remoteAvatar.m_controllingClient.SendAvatarData(m_regionInfo.RegionHandle, m_firstname, m_lastname, m_uuid,
  1376. LocalId, m_pos, m_appearance.Texture.ToBytes(),
  1377. m_parentID);
  1378. m_scene.AddAgentUpdates(1);
  1379. }
  1380. /// <summary>
  1381. /// Tell *ALL* agents about this agent
  1382. /// </summary>
  1383. public void SendFullUpdateToAllClients()
  1384. {
  1385. m_perfMonMS = System.Environment.TickCount;
  1386. List<ScenePresence> avatars = m_scene.GetScenePresences();
  1387. foreach (ScenePresence avatar in avatars)
  1388. {
  1389. SendFullUpdateToOtherClient(avatar);
  1390. if (avatar.LocalId != LocalId)
  1391. {
  1392. if (!avatar.m_isChildAgent || m_scene.m_seeIntoRegionFromNeighbor)
  1393. {
  1394. avatar.SendFullUpdateToOtherClient(this);
  1395. avatar.SendAppearanceToOtherAgent(this);
  1396. }
  1397. }
  1398. }
  1399. m_scene.AddAgentUpdates(avatars.Count);
  1400. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1401. }
  1402. /// <summary>
  1403. /// Do everything required once a client completes its movement into a region
  1404. /// </summary>
  1405. public void SendInitialData()
  1406. {
  1407. // Needed for standalone
  1408. m_scene.GetAvatarAppearance(m_controllingClient, out m_appearance);
  1409. m_controllingClient.SendAvatarData(m_regionInfo.RegionHandle, m_firstname, m_lastname, m_uuid, LocalId,
  1410. m_pos, m_appearance.Texture.ToBytes(), m_parentID);
  1411. if (!m_isChildAgent)
  1412. {
  1413. m_scene.InformClientOfNeighbours(this);
  1414. }
  1415. SendFullUpdateToAllClients();
  1416. SendAppearanceToAllOtherAgents();
  1417. }
  1418. /// <summary>
  1419. ///
  1420. /// </summary>
  1421. /// <param name="client"></param>
  1422. public void SendOwnAppearance()
  1423. {
  1424. m_log.Info("[APPEARANCE] Sending Own Appearance");
  1425. ControllingClient.SendWearables(m_appearance.Wearables, m_appearance.Serial++);
  1426. // ControllingClient.SendAppearance(
  1427. // m_appearance.Owner,
  1428. // m_appearance.VisualParams,
  1429. // m_appearance.Texture.ToBytes()
  1430. // );
  1431. }
  1432. /// <summary>
  1433. ///
  1434. /// </summary>
  1435. public void SendAppearanceToAllOtherAgents()
  1436. {
  1437. m_log.Info("[APPEARANCE] Sending Appearance to All Other Agents");
  1438. m_perfMonMS=System.Environment.TickCount;
  1439. m_scene.ForEachScenePresence(delegate(ScenePresence scenePresence)
  1440. {
  1441. if (scenePresence.UUID != UUID)
  1442. {
  1443. SendAppearanceToOtherAgent(scenePresence);
  1444. }
  1445. });
  1446. m_scene.AddAgentTime(System.Environment.TickCount - m_perfMonMS);
  1447. }
  1448. public void SendAppearanceToOtherAgent(ScenePresence avatar)
  1449. {
  1450. avatar.ControllingClient.SendAppearance(
  1451. m_appearance.Owner,
  1452. m_appearance.VisualParams,
  1453. m_appearance.Texture.ToBytes()
  1454. );
  1455. }
  1456. public void SetAppearance(byte[] texture, List<byte> visualParam)
  1457. {
  1458. m_log.Info("[APPEARANCE] Setting Appearance");
  1459. m_appearance.SetAppearance(texture, visualParam);
  1460. SetHeight(m_appearance.AvatarHeight);
  1461. m_scene.CommsManager.AvatarService.UpdateUserAppearance(m_controllingClient.AgentId, m_appearance);
  1462. SendAppearanceToAllOtherAgents();
  1463. SendOwnAppearance();
  1464. }
  1465. public void SetWearable(int wearableId, AvatarWearable wearable)
  1466. {
  1467. m_log.Info("[APPEARANCE] Setting Wearable");
  1468. m_appearance.SetWearable(wearableId, wearable);
  1469. m_scene.CommsManager.AvatarService.UpdateUserAppearance(m_controllingClient.AgentId, m_appearance);
  1470. m_controllingClient.SendWearables(m_appearance.Wearables, m_appearance.Serial++);
  1471. }
  1472. // Because appearance setting is in a module, we actually need
  1473. // to give it access to our appearance directly, otherwise we
  1474. // get a synchronization issue.
  1475. public AvatarAppearance Appearance {
  1476. get { return m_appearance; }
  1477. set { m_appearance = value; }
  1478. }
  1479. /// <summary>
  1480. ///
  1481. /// </summary>
  1482. /// <param name="animations"></param>
  1483. /// <param name="seqs"></param>
  1484. public void SendAnimPack(LLUUID[] animations, int[] seqs)
  1485. {
  1486. if (m_isChildAgent)
  1487. return;
  1488. m_scene.Broadcast(
  1489. delegate(IClientAPI client) { client.SendAnimations(animations, seqs, m_controllingClient.AgentId); });
  1490. }
  1491. /// <summary>
  1492. ///
  1493. /// </summary>
  1494. public void SendAnimPack()
  1495. {
  1496. if (m_isChildAgent)
  1497. return;
  1498. LLUUID[] animIDs;
  1499. int[] sequenceNums;
  1500. m_animations.GetArrays(out animIDs, out sequenceNums);
  1501. SendAnimPack(animIDs, sequenceNums);
  1502. }
  1503. #endregion
  1504. #region Significant Movement Method
  1505. /// <summary>
  1506. /// This checks for a significant movement and sends a courselocationchange update
  1507. /// </summary>
  1508. protected void CheckForSignificantMovement()
  1509. {
  1510. if (Util.GetDistanceTo(AbsolutePosition, posLastSignificantMove) > 0.5)
  1511. {
  1512. posLastSignificantMove = AbsolutePosition;
  1513. m_scene.EventManager.TriggerSignificantClientMovement(m_controllingClient);
  1514. m_scene.NotifyMyCoarseLocationChange();
  1515. }
  1516. // Minimum Draw distance is 64 meters, the Radius of the draw distance sphere is 32m
  1517. if (Util.GetDistanceTo(AbsolutePosition,m_LastChildAgentUpdatePosition) > 32)
  1518. {
  1519. ChildAgentDataUpdate cadu = new ChildAgentDataUpdate();
  1520. cadu.ActiveGroupID=LLUUID.Zero.UUID;
  1521. cadu.AgentID = UUID.UUID;
  1522. cadu.alwaysrun = m_setAlwaysRun;
  1523. cadu.AVHeight = m_avHeight;
  1524. LLVector3 tempCameraCenter = new LLVector3(m_CameraCenter.x, m_CameraCenter.y, m_CameraCenter.z);
  1525. cadu.cameraPosition = new sLLVector3(tempCameraCenter);
  1526. cadu.drawdistance = m_DrawDistance;
  1527. cadu.godlevel = m_godlevel;
  1528. cadu.GroupAccess = 0;
  1529. cadu.Position = new sLLVector3(AbsolutePosition);
  1530. cadu.regionHandle = m_scene.RegionInfo.RegionHandle;
  1531. float multiplier = 1;
  1532. int innacurateNeighbors = m_scene.GetInaccurateNeighborCount();
  1533. if (innacurateNeighbors != 0)
  1534. {
  1535. multiplier = 1f / (float)innacurateNeighbors;
  1536. }
  1537. if (multiplier <= 0f)
  1538. {
  1539. multiplier = 0.25f;
  1540. }
  1541. //m_log.Info("[NeighborThrottle]: " + m_scene.GetInaccurateNeighborCount().ToString() + " - m: " + multiplier.ToString());
  1542. cadu.throttles = ControllingClient.GetThrottlesPacked(multiplier);
  1543. cadu.Velocity = new sLLVector3(Velocity);
  1544. m_scene.SendOutChildAgentUpdates(cadu,this);
  1545. m_LastChildAgentUpdatePosition.X = AbsolutePosition.X;
  1546. m_LastChildAgentUpdatePosition.Y = AbsolutePosition.Y;
  1547. m_LastChildAgentUpdatePosition.Z = AbsolutePosition.Z;
  1548. }
  1549. }
  1550. #endregion
  1551. #region Border Crossing Methods
  1552. /// <summary>
  1553. /// Checks to see if the avatar is in range of a border and calls CrossToNewRegion
  1554. /// </summary>
  1555. protected void CheckForBorderCrossing()
  1556. {
  1557. if (IsChildAgent)
  1558. return;
  1559. LLVector3 pos2 = AbsolutePosition;
  1560. LLVector3 vel = Velocity;
  1561. float timeStep = 0.1f;
  1562. pos2.X = pos2.X + (vel.X*timeStep);
  1563. pos2.Y = pos2.Y + (vel.Y*timeStep);
  1564. pos2.Z = pos2.Z + (vel.Z*timeStep);
  1565. if ((pos2.X < 0) || (pos2.X > Constants.RegionSize))
  1566. {
  1567. CrossToNewRegion();
  1568. }
  1569. if ((pos2.Y < 0) || (pos2.Y > Constants.RegionSize))
  1570. {
  1571. CrossToNewRegion();
  1572. }
  1573. }
  1574. /// <summary>
  1575. /// Moves the agent outside the region bounds
  1576. /// Tells neighbor region that we're crossing to it
  1577. /// If the neighbor accepts, remove the agent's viewable avatar from this scene
  1578. /// set them to a child agent.
  1579. /// </summary>
  1580. protected void CrossToNewRegion()
  1581. {
  1582. LLVector3 pos = AbsolutePosition;
  1583. LLVector3 newpos = new LLVector3(pos.X, pos.Y, pos.Z);
  1584. uint neighbourx = m_regionInfo.RegionLocX;
  1585. uint neighboury = m_regionInfo.RegionLocY;
  1586. // distance to edge that will trigger crossing
  1587. const float boundaryDistance = 1.7f;
  1588. // distance into new region to place avatar
  1589. const float enterDistance = 0.1f;
  1590. if (pos.X < boundaryDistance)
  1591. {
  1592. neighbourx--;
  1593. newpos.X = Constants.RegionSize - enterDistance;
  1594. }
  1595. else if (pos.X > Constants.RegionSize - boundaryDistance)
  1596. {
  1597. neighbourx++;
  1598. newpos.X = enterDistance;
  1599. }
  1600. if (pos.Y < boundaryDistance)
  1601. {
  1602. neighboury--;
  1603. newpos.Y = Constants.RegionSize - enterDistance;
  1604. }
  1605. else if (pos.Y > Constants.RegionSize - boundaryDistance)
  1606. {
  1607. neighboury++;
  1608. newpos.Y = enterDistance;
  1609. }
  1610. LLVector3 vel = m_velocity;
  1611. ulong neighbourHandle = Helpers.UIntsToLong((uint)(neighbourx * Constants.RegionSize), (uint)(neighboury * Constants.RegionSize));
  1612. SimpleRegionInfo neighbourRegion = m_scene.RequestNeighbouringRegionInfo(neighbourHandle);
  1613. if (neighbourRegion != null)
  1614. {
  1615. // When the neighbour is informed of the border crossing, it will set up CAPS handlers for the avatar
  1616. // This means we need to remove the current caps handler here and possibly compensate later,
  1617. // in case both scenes are being hosted on the same region server. Messy
  1618. m_scene.RemoveCapsHandler(UUID);
  1619. newpos = newpos + (vel);
  1620. bool res =
  1621. m_scene.InformNeighbourOfCrossing(neighbourHandle, m_controllingClient.AgentId, newpos,
  1622. m_physicsActor.Flying);
  1623. if (res)
  1624. {
  1625. AgentCircuitData circuitdata = m_controllingClient.RequestClientInfo();
  1626. // TODO Should construct this behind a method
  1627. string capsPath =
  1628. "http://" + neighbourRegion.ExternalHostName + ":" + neighbourRegion.HttpPort
  1629. + "/CAPS/" + circuitdata.CapsPath + "0000/";
  1630. m_log.DebugFormat(
  1631. "[CAPS]: Sending new CAPS seed url {0} to client {1}", capsPath, m_uuid);
  1632. m_controllingClient.CrossRegion(neighbourHandle, newpos, vel, neighbourRegion.ExternalEndPoint,
  1633. capsPath);
  1634. MakeChildAgent();
  1635. CrossAttachmentsIntoNewRegion(neighbourHandle);
  1636. m_scene.SendKillObject(m_localId);
  1637. m_scene.NotifyMyCoarseLocationChange();
  1638. }
  1639. else
  1640. {
  1641. m_scene.AddCapsHandler(UUID);
  1642. }
  1643. }
  1644. }
  1645. #endregion
  1646. /// <summary>
  1647. /// This allows the Sim owner the abiility to kick users from their sim currently.
  1648. /// It tells the client that the agent has permission to do so.
  1649. /// </summary>
  1650. public void GrantGodlikePowers(LLUUID agentID, LLUUID sessionID, LLUUID token, bool godStatus)
  1651. {
  1652. if (godStatus)
  1653. {
  1654. m_godlevel = 250;
  1655. }
  1656. else
  1657. {
  1658. m_godlevel = 0;
  1659. }
  1660. ControllingClient.SendAdminResponse(token, (uint)m_godlevel);
  1661. }
  1662. /// <summary>
  1663. /// This updates important decision making data about a child agent
  1664. /// The main purpose is to figure out what objects to send to a child agent that's in a neighboring region
  1665. /// </summary>
  1666. public void ChildAgentDataUpdate(ChildAgentDataUpdate cAgentData, uint tRegionX, uint tRegionY, uint rRegionX, uint rRegionY)
  1667. {
  1668. //
  1669. if (!IsChildAgent)
  1670. return;
  1671. int shiftx = ((int)rRegionX - (int)tRegionX) * (int)Constants.RegionSize;
  1672. int shifty = ((int)rRegionY - (int)tRegionY) * (int)Constants.RegionSize;
  1673. m_DrawDistance = cAgentData.drawdistance;
  1674. m_pos = new LLVector3(cAgentData.Position.x + shiftx, cAgentData.Position.y + shifty, cAgentData.Position.z);
  1675. // It's hard to say here.. We can't really tell where the camera position is unless it's in world cordinates from the sending region
  1676. m_CameraCenter =
  1677. new Vector3(cAgentData.cameraPosition.x, cAgentData.cameraPosition.y, cAgentData.cameraPosition.z);
  1678. m_godlevel = cAgentData.godlevel;
  1679. m_avHeight = cAgentData.AVHeight;
  1680. //SetHeight(cAgentData.AVHeight);
  1681. ControllingClient.SetChildAgentThrottle(cAgentData.throttles);
  1682. // Sends out the objects in the user's draw distance if m_sendTasksToChild is true.
  1683. if (m_scene.m_seeIntoRegionFromNeighbor)
  1684. m_scene.SendAllSceneObjectsToClient(this);
  1685. //cAgentData.AVHeight;
  1686. //cAgentData.regionHandle;
  1687. //m_velocity = cAgentData.Velocity;
  1688. }
  1689. /// <summary>
  1690. /// Handles part of the PID controller function for moving an avatar.
  1691. /// </summary>
  1692. public override void UpdateMovement()
  1693. {
  1694. m_newForce = false;
  1695. lock (m_forcesList)
  1696. {
  1697. if (m_forcesList.Count > 0)
  1698. {
  1699. for (int i = 0; i < m_forcesList.Count; i++)
  1700. {
  1701. NewForce force = m_forcesList[i];
  1702. m_updateflag = true;
  1703. try
  1704. {
  1705. movementvector.X = force.X;
  1706. movementvector.Y = force.Y;
  1707. movementvector.Z = force.Z;
  1708. Velocity = movementvector;
  1709. }
  1710. catch (NullReferenceException)
  1711. {
  1712. // Under extreme load, this returns a NullReference Exception that we can ignore.
  1713. // Ignoring this causes no movement to be sent to the physics engine...
  1714. // which when the scene is moving at 1 frame every 10 seconds, it doesn't really matter!
  1715. }
  1716. m_newForce = true;
  1717. }
  1718. for (int i = 0; i < m_forcesList.Count; i++)
  1719. {
  1720. m_forcesList.RemoveAt(0);
  1721. }
  1722. }
  1723. }
  1724. }
  1725. static ScenePresence()
  1726. {
  1727. LLObject.TextureEntry textu = AvatarAppearance.GetDefaultTexture();
  1728. DefaultTexture = textu.ToBytes();
  1729. }
  1730. [Serializable]
  1731. public class NewForce
  1732. {
  1733. public float X;
  1734. public float Y;
  1735. public float Z;
  1736. public NewForce()
  1737. {
  1738. }
  1739. }
  1740. [Serializable]
  1741. public class ScenePartUpdate : ISerializable
  1742. {
  1743. public LLUUID FullID;
  1744. public uint LastFullUpdateTime;
  1745. public uint LastTerseUpdateTime;
  1746. public ScenePartUpdate()
  1747. {
  1748. FullID = LLUUID.Zero;
  1749. LastFullUpdateTime = 0;
  1750. LastTerseUpdateTime = 0;
  1751. }
  1752. protected ScenePartUpdate(SerializationInfo info, StreamingContext context)
  1753. {
  1754. //System.Console.WriteLine("ScenePartUpdate Deserialize BGN");
  1755. if (info == null)
  1756. {
  1757. throw new ArgumentNullException("info");
  1758. }
  1759. FullID = new LLUUID((Guid)info.GetValue("FullID", typeof(Guid)));
  1760. LastFullUpdateTime = (uint)info.GetValue("LastFullUpdateTime", typeof(uint));
  1761. LastTerseUpdateTime = (uint)info.GetValue("LastTerseUpdateTime", typeof(uint));
  1762. //System.Console.WriteLine("ScenePartUpdate Deserialize END");
  1763. }
  1764. [SecurityPermission(SecurityAction.LinkDemand,
  1765. Flags = SecurityPermissionFlag.SerializationFormatter)]
  1766. public virtual void GetObjectData(
  1767. SerializationInfo info, StreamingContext context)
  1768. {
  1769. if (info == null)
  1770. {
  1771. throw new ArgumentNullException("info");
  1772. }
  1773. info.AddValue("FullID", FullID.UUID);
  1774. info.AddValue("LastFullUpdateTime", LastFullUpdateTime);
  1775. info.AddValue("LastTerseUpdateTime", LastTerseUpdateTime);
  1776. }
  1777. }
  1778. public override void SetText(string text, Vector3 color, double alpha)
  1779. {
  1780. throw new Exception("Can't set Text on avatar.");
  1781. }
  1782. /// <summary>
  1783. /// Adds a physical representation of the avatar to the Physics plugin
  1784. /// </summary>
  1785. public void AddToPhysicalScene()
  1786. {
  1787. PhysicsScene scene = m_scene.PhysicsScene;
  1788. PhysicsVector pVec =
  1789. new PhysicsVector(AbsolutePosition.X, AbsolutePosition.Y,
  1790. AbsolutePosition.Z);
  1791. if (m_avHeight == 127.0f)
  1792. {
  1793. m_physicsActor = scene.AddAvatar(Firstname + "." + Lastname, pVec, new PhysicsVector(0, 0, 1.56f));
  1794. }
  1795. else
  1796. {
  1797. m_physicsActor = scene.AddAvatar(Firstname + "." + Lastname, pVec, new PhysicsVector(0, 0, m_avHeight));
  1798. }
  1799. //m_physicsActor.OnRequestTerseUpdate += SendTerseUpdateToAllClients;
  1800. m_physicsActor.OnCollisionUpdate += PhysicsCollisionUpdate;
  1801. m_physicsActor.SubscribeEvents(1000);
  1802. m_physicsActor.LocalID = LocalId;
  1803. }
  1804. // Event called by the physics plugin to tell the avatar about a collision.
  1805. private void PhysicsCollisionUpdate(EventArgs e)
  1806. {
  1807. if (e == null)
  1808. return;
  1809. CollisionEventUpdate collisionData = (CollisionEventUpdate)e;
  1810. Dictionary<uint, float> coldata = collisionData.m_objCollisionList;
  1811. float starthealth = Health;
  1812. uint killerObj = 0;
  1813. foreach (uint localid in coldata.Keys)
  1814. {
  1815. if (coldata[localid] <= 0.10f || m_invulnerable)
  1816. continue;
  1817. //if (localid == 0)
  1818. //continue;
  1819. Health -= coldata[localid] * 5;
  1820. if (Health <= 0)
  1821. {
  1822. if (localid != 0)
  1823. killerObj = localid;
  1824. }
  1825. //m_log.Debug("[AVATAR]: Collision with localid: " + localid.ToString() + " at depth: " + coldata[localid].ToString());
  1826. }
  1827. //Health = 100;
  1828. if (!m_invulnerable)
  1829. {
  1830. if (starthealth != Health)
  1831. {
  1832. ControllingClient.SendHealth(Health);
  1833. }
  1834. if (m_health <= 0)
  1835. m_scene.EventManager.TriggerAvatarKill(killerObj, this);
  1836. }
  1837. if (Velocity.X > 0 || Velocity.Y > 0)
  1838. UpdateMovementAnimations();
  1839. }
  1840. public void setHealthWithUpdate(float health)
  1841. {
  1842. Health = health;
  1843. ControllingClient.SendHealth(Health);
  1844. }
  1845. internal void Close()
  1846. {
  1847. lock (m_attachments)
  1848. {
  1849. if (!m_attachmentsTransported)
  1850. {
  1851. try
  1852. {
  1853. foreach (SceneObjectGroup grp in m_attachments)
  1854. {
  1855. // ControllingClient may be null at this point!
  1856. m_scene.m_innerScene.DetachSingleAttachmentToInv(grp.GetFromAssetID(), ControllingClient);
  1857. }
  1858. }
  1859. catch (InvalidOperationException)
  1860. {
  1861. m_log.Info("[CLIENT]: Couldn't save attachments. :(");
  1862. }
  1863. m_attachments.Clear();
  1864. }
  1865. }
  1866. lock (m_knownPrimUUID)
  1867. {
  1868. m_knownPrimUUID.Clear();
  1869. }
  1870. lock (m_knownChildRegions)
  1871. {
  1872. m_knownChildRegions.Clear();
  1873. }
  1874. lock (m_updateTimes)
  1875. {
  1876. m_updateTimes.Clear();
  1877. }
  1878. lock (m_partsUpdateQueue)
  1879. {
  1880. m_partsUpdateQueue.Clear();
  1881. }
  1882. RemoveFromPhysicalScene();
  1883. GC.Collect();
  1884. }
  1885. public ScenePresence()
  1886. {
  1887. /* JB
  1888. if (Animations == null)
  1889. {
  1890. Animations = new AvatarAnimations();
  1891. Animations.LoadAnims();
  1892. }
  1893. */
  1894. if (DefaultTexture == null)
  1895. {
  1896. LLObject.TextureEntry textu = AvatarAppearance.GetDefaultTexture();
  1897. DefaultTexture = textu.ToBytes();
  1898. }
  1899. }
  1900. public void AddAttachment(SceneObjectGroup gobj)
  1901. {
  1902. lock (m_attachments)
  1903. {
  1904. m_attachments.Add(gobj);
  1905. }
  1906. }
  1907. public void RemoveAttachment(SceneObjectGroup gobj)
  1908. {
  1909. lock (m_attachments)
  1910. {
  1911. if (m_attachments.Contains(gobj))
  1912. {
  1913. m_attachments.Remove(gobj);
  1914. }
  1915. }
  1916. }
  1917. public void CrossAttachmentsIntoNewRegion(ulong regionHandle)
  1918. {
  1919. m_attachmentsTransported = true;
  1920. lock (m_attachments)
  1921. {
  1922. foreach (SceneObjectGroup gobj in m_attachments)
  1923. {
  1924. // If the prim group is null then something must have happened to it!
  1925. if (gobj != null)
  1926. {
  1927. // Set the parent localID to 0 so it transfers over properly.
  1928. gobj.RootPart.SetParentLocalId(0);
  1929. gobj.RootPart.m_IsAttachment = false;
  1930. gobj.AbsolutePosition = gobj.RootPart.m_attachedPos;
  1931. gobj.RootPart.LastOwnerID = gobj.GetFromAssetID();
  1932. m_scene.CrossPrimGroupIntoNewRegion(regionHandle, gobj);
  1933. }
  1934. }
  1935. m_attachments.Clear();
  1936. }
  1937. }
  1938. public void initializeScenePresence(IClientAPI client, RegionInfo region, Scene scene)
  1939. {
  1940. m_controllingClient = client;
  1941. m_regionInfo = region;
  1942. m_scene = scene;
  1943. RegisterToEvents();
  1944. /*
  1945. AbsolutePosition = client.StartPos;
  1946. Animations = new AvatarAnimations();
  1947. Animations.LoadAnims();
  1948. m_animations = new List<LLUUID>();
  1949. m_animations.Add(Animations.AnimsLLUUID["STAND"]);
  1950. m_animationSeqs.Add(m_controllingClient.NextAnimationSequenceNumber);
  1951. SetDirectionVectors();
  1952. */
  1953. }
  1954. protected ScenePresence(SerializationInfo info, StreamingContext context)
  1955. : base (info, context)
  1956. {
  1957. //System.Console.WriteLine("ScenePresence Deserialize BGN");
  1958. if (info == null)
  1959. {
  1960. throw new ArgumentNullException("info");
  1961. }
  1962. /* JB
  1963. if (Animations == null)
  1964. {
  1965. Animations = new AvatarAnimations();
  1966. Animations.LoadAnims();
  1967. }
  1968. */
  1969. if (DefaultTexture == null)
  1970. {
  1971. LLObject.TextureEntry textu = AvatarAppearance.GetDefaultTexture();
  1972. DefaultTexture = textu.ToBytes();
  1973. }
  1974. m_animations = (AnimationSet)info.GetValue("m_animations", typeof(AnimationSet));
  1975. m_updateflag = (bool)info.GetValue("m_updateflag", typeof(bool));
  1976. m_movementflag = (byte)info.GetValue("m_movementflag", typeof(byte));
  1977. m_forcesList = (List<NewForce>)info.GetValue("m_forcesList", typeof(List<NewForce>));
  1978. m_updateCount = (short)info.GetValue("m_updateCount", typeof(short));
  1979. m_requestedSitTargetID = (uint)info.GetValue("m_requestedSitTargetID", typeof(uint));
  1980. m_requestedSitOffset
  1981. = new LLVector3(
  1982. (float)info.GetValue("m_requestedSitOffset.X", typeof(float)),
  1983. (float)info.GetValue("m_requestedSitOffset.Y", typeof(float)),
  1984. (float)info.GetValue("m_requestedSitOffset.Z", typeof(float)));
  1985. m_sitAvatarHeight = (float)info.GetValue("m_sitAvatarHeight", typeof(float));
  1986. m_godlevel = (float)info.GetValue("m_godlevel", typeof(float));
  1987. m_setAlwaysRun = (bool)info.GetValue("m_setAlwaysRun", typeof(bool));
  1988. m_bodyRot
  1989. = new Quaternion(
  1990. (float)info.GetValue("m_bodyRot.w", typeof(float)),
  1991. (float)info.GetValue("m_bodyRot.x", typeof(float)),
  1992. (float)info.GetValue("m_bodyRot.y", typeof(float)),
  1993. (float)info.GetValue("m_bodyRot.z", typeof(float)));
  1994. IsRestrictedToRegion = (bool)info.GetValue("IsRestrictedToRegion", typeof(bool));
  1995. m_newForce = (bool)info.GetValue("m_newForce", typeof(bool));
  1996. //m_newAvatar = (bool)info.GetValue("m_newAvatar", typeof(bool));
  1997. m_newCoarseLocations = (bool)info.GetValue("m_newCoarseLocations", typeof(bool));
  1998. m_gotAllObjectsInScene = (bool)info.GetValue("m_gotAllObjectsInScene", typeof(bool));
  1999. m_avHeight = (float)info.GetValue("m_avHeight", typeof(float));
  2000. crossingFromRegion = (ulong)info.GetValue("crossingFromRegion", typeof(ulong));
  2001. List<float[]> Dir_Vectors_work = (List<float[]>)info.GetValue("Dir_Vectors", typeof(List<float[]>));
  2002. List<Vector3> Dir_Vectors_work2 = new List<Vector3>();
  2003. foreach (float[] f3 in Dir_Vectors_work)
  2004. {
  2005. Dir_Vectors_work2.Add(new Vector3(f3[0], f3[1], f3[2]));
  2006. }
  2007. Dir_Vectors = Dir_Vectors_work2.ToArray();
  2008. lastPhysPos
  2009. = new LLVector3(
  2010. (float)info.GetValue("lastPhysPos.X", typeof(float)),
  2011. (float)info.GetValue("lastPhysPos.Y", typeof(float)),
  2012. (float)info.GetValue("lastPhysPos.Z", typeof(float)));
  2013. m_CameraCenter
  2014. = new Vector3(
  2015. (float)info.GetValue("m_CameraCenter.X", typeof(float)),
  2016. (float)info.GetValue("m_CameraCenter.Y", typeof(float)),
  2017. (float)info.GetValue("m_CameraCenter.Z", typeof(float)));
  2018. m_CameraAtAxis
  2019. = new Vector3(
  2020. (float)info.GetValue("m_CameraAtAxis.X", typeof(float)),
  2021. (float)info.GetValue("m_CameraAtAxis.Y", typeof(float)),
  2022. (float)info.GetValue("m_CameraAtAxis.Z", typeof(float)));
  2023. m_CameraLeftAxis
  2024. = new Vector3(
  2025. (float)info.GetValue("m_CameraLeftAxis.X", typeof(float)),
  2026. (float)info.GetValue("m_CameraLeftAxis.Y", typeof(float)),
  2027. (float)info.GetValue("m_CameraLeftAxis.Z", typeof(float)));
  2028. m_CameraUpAxis
  2029. = new Vector3(
  2030. (float)info.GetValue("m_CameraUpAxis.X", typeof(float)),
  2031. (float)info.GetValue("m_CameraUpAxis.Y", typeof(float)),
  2032. (float)info.GetValue("m_CameraUpAxis.Z", typeof(float)));
  2033. m_DrawDistance = (float)info.GetValue("m_DrawDistance", typeof(float));
  2034. m_appearance = (AvatarAppearance)info.GetValue("m_appearance", typeof(AvatarAppearance));
  2035. m_knownChildRegions = (List<ulong>)info.GetValue("m_knownChildRegions", typeof(List<ulong>));
  2036. posLastSignificantMove
  2037. = new LLVector3(
  2038. (float)info.GetValue("posLastSignificantMove.X", typeof(float)),
  2039. (float)info.GetValue("posLastSignificantMove.Y", typeof(float)),
  2040. (float)info.GetValue("posLastSignificantMove.Z", typeof(float)));
  2041. // m_partsUpdateQueue = (UpdateQueue)info.GetValue("m_partsUpdateQueue", typeof(UpdateQueue));
  2042. /*
  2043. Dictionary<Guid, ScenePartUpdate> updateTimes_work
  2044. = (Dictionary<Guid, ScenePartUpdate>)info.GetValue("m_updateTimes", typeof(Dictionary<Guid, ScenePartUpdate>));
  2045. foreach (Guid id in updateTimes_work.Keys)
  2046. {
  2047. m_updateTimes.Add(new LLUUID(id), updateTimes_work[id]);
  2048. }
  2049. */
  2050. m_regionHandle = (ulong)info.GetValue("m_regionHandle", typeof(ulong));
  2051. m_firstname = (string)info.GetValue("m_firstname", typeof(string));
  2052. m_lastname = (string)info.GetValue("m_lastname", typeof(string));
  2053. m_allowMovement = (bool)info.GetValue("m_allowMovement", typeof(bool));
  2054. m_parentPosition = new LLVector3((float)info.GetValue("m_parentPosition.X", typeof(float)),
  2055. (float)info.GetValue("m_parentPosition.Y", typeof(float)),
  2056. (float)info.GetValue("m_parentPosition.Z", typeof(float)));
  2057. m_isChildAgent = (bool)info.GetValue("m_isChildAgent", typeof(bool));
  2058. m_parentID = (uint)info.GetValue("m_parentID", typeof(uint));
  2059. // for OpenSim_v0.5
  2060. currentParcelUUID = new LLUUID((Guid)info.GetValue("currentParcelUUID", typeof(Guid)));
  2061. lastKnownAllowedPosition
  2062. = new Vector3(
  2063. (float)info.GetValue("lastKnownAllowedPosition.X", typeof(float)),
  2064. (float)info.GetValue("lastKnownAllowedPosition.Y", typeof(float)),
  2065. (float)info.GetValue("lastKnownAllowedPosition.Z", typeof(float)));
  2066. sentMessageAboutRestrictedParcelFlyingDown = (bool)info.GetValue("sentMessageAboutRestrictedParcelFlyingDown", typeof(bool));
  2067. m_LastChildAgentUpdatePosition
  2068. = new LLVector3(
  2069. (float)info.GetValue("m_LastChildAgentUpdatePosition.X", typeof(float)),
  2070. (float)info.GetValue("m_LastChildAgentUpdatePosition.Y", typeof(float)),
  2071. (float)info.GetValue("m_LastChildAgentUpdatePosition.Z", typeof(float)));
  2072. m_perfMonMS = (int)info.GetValue("m_perfMonMS", typeof(int));
  2073. m_AgentControlFlags = (uint)info.GetValue("m_AgentControlFlags", typeof(uint));
  2074. m_headrotation
  2075. = new LLQuaternion(
  2076. (float)info.GetValue("m_headrotation.W", typeof(float)),
  2077. (float)info.GetValue("m_headrotation.X", typeof(float)),
  2078. (float)info.GetValue("m_headrotation.Y", typeof(float)),
  2079. (float)info.GetValue("m_headrotation.Z", typeof(float)));
  2080. m_state = (byte)info.GetValue("m_state", typeof(byte));
  2081. List<Guid> knownPrimUUID_work = (List<Guid>)info.GetValue("m_knownPrimUUID", typeof(List<Guid>));
  2082. foreach (Guid id in knownPrimUUID_work)
  2083. {
  2084. m_knownPrimUUID.Add(new LLUUID(id));
  2085. }
  2086. //System.Console.WriteLine("ScenePresence Deserialize END");
  2087. }
  2088. [SecurityPermission(SecurityAction.LinkDemand,
  2089. Flags = SecurityPermissionFlag.SerializationFormatter)]
  2090. public override void GetObjectData(
  2091. SerializationInfo info, StreamingContext context)
  2092. {
  2093. if (info == null)
  2094. {
  2095. throw new ArgumentNullException("info");
  2096. }
  2097. base.GetObjectData(info, context);
  2098. info.AddValue("m_animations", m_animations);
  2099. info.AddValue("m_updateflag", m_updateflag);
  2100. info.AddValue("m_movementflag", m_movementflag);
  2101. info.AddValue("m_forcesList", m_forcesList);
  2102. info.AddValue("m_updateCount", m_updateCount);
  2103. info.AddValue("m_requestedSitTargetID", m_requestedSitTargetID);
  2104. // LLVector3
  2105. info.AddValue("m_requestedSitOffset.X", m_requestedSitOffset.X);
  2106. info.AddValue("m_requestedSitOffset.Y", m_requestedSitOffset.Y);
  2107. info.AddValue("m_requestedSitOffset.Z", m_requestedSitOffset.Z);
  2108. info.AddValue("m_sitAvatarHeight", m_sitAvatarHeight);
  2109. info.AddValue("m_godlevel", m_godlevel);
  2110. info.AddValue("m_setAlwaysRun", m_setAlwaysRun);
  2111. // Quaternion
  2112. info.AddValue("m_bodyRot.w", m_bodyRot.w);
  2113. info.AddValue("m_bodyRot.x", m_bodyRot.x);
  2114. info.AddValue("m_bodyRot.y", m_bodyRot.y);
  2115. info.AddValue("m_bodyRot.z", m_bodyRot.z);
  2116. info.AddValue("IsRestrictedToRegion", IsRestrictedToRegion);
  2117. info.AddValue("m_newForce", m_newForce);
  2118. //info.AddValue("m_newAvatar", m_newAvatar);
  2119. info.AddValue("m_newCoarseLocations", m_newCoarseLocations);
  2120. info.AddValue("m_gotAllObjectsInScene", m_gotAllObjectsInScene);
  2121. info.AddValue("m_avHeight", m_avHeight);
  2122. // info.AddValue("m_regionInfo", m_regionInfo);
  2123. info.AddValue("crossingFromRegion", crossingFromRegion);
  2124. List<float[]> Dir_Vectors_work = new List<float[]>();
  2125. foreach (Vector3 v3 in Dir_Vectors)
  2126. {
  2127. Dir_Vectors_work.Add(new float[] { v3.x, v3.y, v3.z });
  2128. }
  2129. info.AddValue("Dir_Vectors", Dir_Vectors_work);
  2130. // LLVector3
  2131. info.AddValue("lastPhysPos.X", lastPhysPos.X);
  2132. info.AddValue("lastPhysPos.Y", lastPhysPos.Y);
  2133. info.AddValue("lastPhysPos.Z", lastPhysPos.Z);
  2134. // Vector3
  2135. info.AddValue("m_CameraCenter.X", m_CameraCenter.x);
  2136. info.AddValue("m_CameraCenter.Y", m_CameraCenter.y);
  2137. info.AddValue("m_CameraCenter.Z", m_CameraCenter.z);
  2138. // Vector3
  2139. info.AddValue("m_CameraAtAxis.X", m_CameraAtAxis.x);
  2140. info.AddValue("m_CameraAtAxis.Y", m_CameraAtAxis.y);
  2141. info.AddValue("m_CameraAtAxis.Z", m_CameraAtAxis.z);
  2142. // Vector3
  2143. info.AddValue("m_CameraLeftAxis.X", m_CameraLeftAxis.x);
  2144. info.AddValue("m_CameraLeftAxis.Y", m_CameraLeftAxis.y);
  2145. info.AddValue("m_CameraLeftAxis.Z", m_CameraLeftAxis.z);
  2146. // Vector3
  2147. info.AddValue("m_CameraUpAxis.X", m_CameraUpAxis.x);
  2148. info.AddValue("m_CameraUpAxis.Y", m_CameraUpAxis.y);
  2149. info.AddValue("m_CameraUpAxis.Z", m_CameraUpAxis.z);
  2150. info.AddValue("m_DrawDistance", m_DrawDistance);
  2151. info.AddValue("m_appearance", m_appearance);
  2152. info.AddValue("m_knownChildRegions", m_knownChildRegions);
  2153. // LLVector3
  2154. info.AddValue("posLastSignificantMove.X", posLastSignificantMove.X);
  2155. info.AddValue("posLastSignificantMove.Y", posLastSignificantMove.Y);
  2156. info.AddValue("posLastSignificantMove.Z", posLastSignificantMove.Z);
  2157. //info.AddValue("m_partsUpdateQueue", m_partsUpdateQueue);
  2158. /*
  2159. Dictionary<Guid, ScenePartUpdate> updateTimes_work = new Dictionary<Guid, ScenePartUpdate>();
  2160. foreach (LLUUID id in m_updateTimes.Keys)
  2161. {
  2162. updateTimes_work.Add(id.UUID, m_updateTimes[id]);
  2163. }
  2164. info.AddValue("m_updateTimes", updateTimes_work);
  2165. */
  2166. info.AddValue("m_regionHandle", m_regionHandle);
  2167. info.AddValue("m_firstname", m_firstname);
  2168. info.AddValue("m_lastname", m_lastname);
  2169. info.AddValue("m_allowMovement", m_allowMovement);
  2170. //info.AddValue("m_physicsActor", m_physicsActor);
  2171. info.AddValue("m_parentPosition.X", m_parentPosition.X);
  2172. info.AddValue("m_parentPosition.Y", m_parentPosition.Y);
  2173. info.AddValue("m_parentPosition.Z", m_parentPosition.Z);
  2174. info.AddValue("m_isChildAgent", m_isChildAgent);
  2175. info.AddValue("m_parentID", m_parentID);
  2176. // for OpenSim_v0.5
  2177. info.AddValue("currentParcelUUID", currentParcelUUID.UUID);
  2178. info.AddValue("lastKnownAllowedPosition.X", lastKnownAllowedPosition.x);
  2179. info.AddValue("lastKnownAllowedPosition.Y", lastKnownAllowedPosition.y);
  2180. info.AddValue("lastKnownAllowedPosition.Z", lastKnownAllowedPosition.z);
  2181. info.AddValue("sentMessageAboutRestrictedParcelFlyingDown", sentMessageAboutRestrictedParcelFlyingDown);
  2182. info.AddValue("m_LastChildAgentUpdatePosition.X", m_LastChildAgentUpdatePosition.X);
  2183. info.AddValue("m_LastChildAgentUpdatePosition.Y", m_LastChildAgentUpdatePosition.Y);
  2184. info.AddValue("m_LastChildAgentUpdatePosition.Z", m_LastChildAgentUpdatePosition.Z);
  2185. info.AddValue("m_perfMonMS", m_perfMonMS);
  2186. info.AddValue("m_AgentControlFlags", m_AgentControlFlags);
  2187. info.AddValue("m_headrotation.W", m_headrotation.W);
  2188. info.AddValue("m_headrotation.X", m_headrotation.X);
  2189. info.AddValue("m_headrotation.Y", m_headrotation.Y);
  2190. info.AddValue("m_headrotation.Z", m_headrotation.Z);
  2191. info.AddValue("m_state", m_state);
  2192. List<Guid> knownPrimUUID_work = new List<Guid>();
  2193. foreach (LLUUID id in m_knownPrimUUID)
  2194. {
  2195. knownPrimUUID_work.Add(id.UUID);
  2196. }
  2197. info.AddValue("m_knownPrimUUID", knownPrimUUID_work);
  2198. }
  2199. internal void PushForce(PhysicsVector impulse)
  2200. {
  2201. if (PhysicsActor != null)
  2202. {
  2203. PhysicsActor.AddForce(impulse,true);
  2204. }
  2205. }
  2206. public void RegisterControlEventsToScript(int controls, int accept, int pass_on, uint Obj_localID, LLUUID Script_item_LLUUID)
  2207. {
  2208. ScriptControllers obj = new ScriptControllers();
  2209. obj.ignoreControls = ScriptControlled.CONTROL_ZERO;
  2210. obj.eventControls = ScriptControlled.CONTROL_ZERO;
  2211. obj.itemID = Script_item_LLUUID;
  2212. obj.objID = Obj_localID;
  2213. if (pass_on == 0 && accept == 0)
  2214. {
  2215. IgnoredControls |= (ScriptControlled)controls;
  2216. obj.ignoreControls = (ScriptControlled)controls;
  2217. }
  2218. if (pass_on == 0 && accept == 1)
  2219. {
  2220. IgnoredControls |= (ScriptControlled)controls;
  2221. obj.ignoreControls = (ScriptControlled)controls;
  2222. obj.eventControls = (ScriptControlled)controls;
  2223. }
  2224. if (pass_on == 1 && accept == 1)
  2225. {
  2226. IgnoredControls = ScriptControlled.CONTROL_ZERO;
  2227. obj.eventControls = (ScriptControlled)controls;
  2228. obj.ignoreControls = ScriptControlled.CONTROL_ZERO;
  2229. }
  2230. lock (scriptedcontrols)
  2231. {
  2232. if (pass_on == 1 && accept == 0)
  2233. {
  2234. IgnoredControls &= ~(ScriptControlled)controls;
  2235. if (scriptedcontrols.ContainsKey(Script_item_LLUUID))
  2236. scriptedcontrols.Remove(Script_item_LLUUID);
  2237. }
  2238. else
  2239. {
  2240. if (scriptedcontrols.ContainsKey(Script_item_LLUUID))
  2241. {
  2242. scriptedcontrols[Script_item_LLUUID] = obj;
  2243. }
  2244. else
  2245. {
  2246. scriptedcontrols.Add(Script_item_LLUUID, obj);
  2247. }
  2248. }
  2249. }
  2250. ControllingClient.SendTakeControls(controls, pass_on == 1 ? true : false, true);
  2251. }
  2252. public void HandleForceReleaseControls(IClientAPI remoteClient, LLUUID agentID)
  2253. {
  2254. IgnoredControls = ScriptControlled.CONTROL_ZERO;
  2255. lock (scriptedcontrols)
  2256. {
  2257. scriptedcontrols.Clear();
  2258. }
  2259. ControllingClient.SendTakeControls(int.MaxValue, false, false);
  2260. }
  2261. public void UnRegisterControlEventsToScript(uint Obj_localID, LLUUID Script_item_LLUUID)
  2262. {
  2263. lock (scriptedcontrols)
  2264. {
  2265. if (scriptedcontrols.ContainsKey(Script_item_LLUUID))
  2266. {
  2267. scriptedcontrols.Remove(Script_item_LLUUID);
  2268. IgnoredControls = ScriptControlled.CONTROL_ZERO;
  2269. foreach (ScriptControllers scData in scriptedcontrols.Values)
  2270. {
  2271. IgnoredControls |= scData.ignoreControls;
  2272. }
  2273. }
  2274. }
  2275. }
  2276. internal void SendControlToScripts(uint flags, ScriptControlled lastFlags)
  2277. {
  2278. ScriptControlled allflags = ScriptControlled.CONTROL_ZERO;
  2279. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_AT_POS) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS) != 0)
  2280. {
  2281. allflags |= ScriptControlled.CONTROL_FWD;
  2282. }
  2283. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_NEG) != 0)
  2284. {
  2285. allflags |= ScriptControlled.CONTROL_BACK;
  2286. }
  2287. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_UP_POS) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_POS) != 0)
  2288. {
  2289. allflags |= ScriptControlled.CONTROL_UP;
  2290. }
  2291. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG) != 0)
  2292. {
  2293. allflags |= ScriptControlled.CONTROL_DOWN;
  2294. }
  2295. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_POS) != 0)
  2296. {
  2297. allflags |= ScriptControlled.CONTROL_LEFT;
  2298. }
  2299. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_NEG) != 0)
  2300. {
  2301. allflags |= ScriptControlled.CONTROL_RIGHT;
  2302. }
  2303. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_YAW_POS) != 0)
  2304. {
  2305. allflags |= ScriptControlled.CONTROL_ROT_RIGHT;
  2306. }
  2307. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_YAW_NEG) != 0)
  2308. {
  2309. allflags |= ScriptControlled.CONTROL_ROT_LEFT;
  2310. }
  2311. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_ML_LBUTTON_DOWN) != 0)
  2312. {
  2313. allflags |= ScriptControlled.CONTROL_ML_LBUTTON;
  2314. }
  2315. if ((flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_LBUTTON_UP) != 0 || (flags & (uint)AgentManager.ControlFlags.AGENT_CONTROL_LBUTTON_DOWN) != 0)
  2316. {
  2317. allflags |= ScriptControlled.CONTROL_LBUTTON;
  2318. }
  2319. ScriptControlled held = ScriptControlled.CONTROL_ZERO;
  2320. ScriptControlled change = ScriptControlled.CONTROL_ZERO;
  2321. foreach (ScriptControlled DCF in Enum.GetValues(typeof (ScriptControlled)))
  2322. {
  2323. // Held
  2324. if ((lastFlags & DCF) != 0 && (allflags & DCF) != 0)
  2325. {
  2326. held |= DCF;
  2327. continue;
  2328. }
  2329. // Not held recently
  2330. if ((lastFlags & DCF) != 0 && (allflags & DCF) == 0)
  2331. {
  2332. change |= DCF;
  2333. continue;
  2334. }
  2335. // Newly pressed.
  2336. if ((lastFlags & DCF) == 0 && (allflags & DCF) != 0)
  2337. {
  2338. change |= DCF;
  2339. continue;
  2340. }
  2341. }
  2342. lock (scriptedcontrols)
  2343. {
  2344. foreach (LLUUID scriptUUID in scriptedcontrols.Keys)
  2345. {
  2346. ScriptControllers scriptControlData = scriptedcontrols[scriptUUID];
  2347. ScriptControlled localHeld = held & scriptControlData.eventControls;
  2348. //if (localHeld != ScriptControlled.CONTROL_ZERO)
  2349. //{
  2350. //int i = 1;
  2351. //}
  2352. ScriptControlled localChange = change & scriptControlData.eventControls;
  2353. m_scene.EventManager.TriggerControlEvent(scriptControlData.objID, scriptUUID, UUID, (uint)localHeld, (uint)localChange);
  2354. }
  2355. }
  2356. LastCommands = allflags;
  2357. }
  2358. internal uint RemoveIgnoredControls(uint flags, ScriptControlled Ignored)
  2359. {
  2360. if (Ignored == ScriptControlled.CONTROL_ZERO)
  2361. return flags;
  2362. if ((Ignored & ScriptControlled.CONTROL_BACK) != 0)
  2363. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG | (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_NEG);
  2364. if ((Ignored & ScriptControlled.CONTROL_FWD) != 0)
  2365. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS | (uint)AgentManager.ControlFlags.AGENT_CONTROL_AT_POS);
  2366. if ((Ignored & ScriptControlled.CONTROL_DOWN) != 0)
  2367. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG | (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG);
  2368. if ((Ignored & ScriptControlled.CONTROL_UP) != 0)
  2369. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_POS | (uint)AgentManager.ControlFlags.AGENT_CONTROL_UP_POS);
  2370. if ((Ignored & ScriptControlled.CONTROL_LEFT) != 0)
  2371. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS | (uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_POS);
  2372. if ((Ignored & ScriptControlled.CONTROL_RIGHT) != 0)
  2373. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_NEG | (uint)AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG);
  2374. if ((Ignored & ScriptControlled.CONTROL_ROT_LEFT) != 0)
  2375. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_YAW_NEG);
  2376. if ((Ignored & ScriptControlled.CONTROL_ROT_RIGHT) != 0)
  2377. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_YAW_POS);
  2378. if ((Ignored & ScriptControlled.CONTROL_ML_LBUTTON) != 0)
  2379. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_ML_LBUTTON_DOWN);
  2380. if ((Ignored & ScriptControlled.CONTROL_LBUTTON) != 0)
  2381. flags &= ~((uint)AgentManager.ControlFlags.AGENT_CONTROL_LBUTTON_UP | (uint)AgentManager.ControlFlags.AGENT_CONTROL_LBUTTON_DOWN);
  2382. //DIR_CONTROL_FLAG_FORWARD = AgentManager.ControlFlags.AGENT_CONTROL_AT_POS,
  2383. //DIR_CONTROL_FLAG_BACK = AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG,
  2384. //DIR_CONTROL_FLAG_LEFT = AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS,
  2385. //DIR_CONTROL_FLAG_RIGHT = AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG,
  2386. //DIR_CONTROL_FLAG_UP = AgentManager.ControlFlags.AGENT_CONTROL_UP_POS,
  2387. //DIR_CONTROL_FLAG_DOWN = AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG,
  2388. //DIR_CONTROL_FLAG_DOWN_NUDGE = AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG
  2389. return flags;
  2390. }
  2391. }
  2392. }