PhysicsBot.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.IO;
  30. using System.Threading;
  31. using System.Timers;
  32. using OpenMetaverse;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using Timer=System.Timers.Timer;
  37. namespace pCampBot
  38. {
  39. public class PhysicsBot
  40. {
  41. public delegate void AnEvent(PhysicsBot callbot, EventType someevent); // event delegate for bot events
  42. public IConfig startupConfig; // bot config, passed from BotManager
  43. public string firstname;
  44. public string lastname;
  45. public string password;
  46. public string loginURI;
  47. public event AnEvent OnConnected;
  48. public event AnEvent OnDisconnected;
  49. protected Timer m_action; // Action Timer
  50. protected List<uint> objectIDs = new List<uint>();
  51. protected Random somthing = new Random(Environment.TickCount);// We do stuff randomly here
  52. //New instance of a SecondLife client
  53. public GridClient client = new GridClient();
  54. protected string[] talkarray;
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. /// <param name="bsconfig">nini config for the bot</param>
  59. public PhysicsBot(IConfig bsconfig)
  60. {
  61. startupConfig = bsconfig;
  62. readconfig();
  63. talkarray = readexcuses();
  64. }
  65. //We do our actions here. This is where one would
  66. //add additional steps and/or things the bot should do
  67. void m_action_Elapsed(object sender, ElapsedEventArgs e)
  68. {
  69. //client.Throttle.Task = 500000f;
  70. //client.Throttle.Set();
  71. int walkorrun = somthing.Next(4); // Randomize between walking and running. The greater this number,
  72. // the greater the bot's chances to walk instead of run.
  73. if (walkorrun == 0)
  74. {
  75. client.Self.Movement.AlwaysRun = true;
  76. }
  77. else
  78. {
  79. client.Self.Movement.AlwaysRun = false;
  80. }
  81. // TODO: unused: Vector3 pos = client.Self.SimPosition;
  82. Vector3 newpos = new Vector3(somthing.Next(255), somthing.Next(255), somthing.Next(255));
  83. client.Self.Movement.TurnToward(newpos);
  84. for (int i = 0; i < 2000; i++)
  85. {
  86. client.Self.Movement.AtPos = true;
  87. Thread.Sleep(somthing.Next(25, 75)); // Makes sure the bots keep walking for this time.
  88. }
  89. client.Self.Jump(true);
  90. string randomf = talkarray[somthing.Next(talkarray.Length)];
  91. if (talkarray.Length > 1 && randomf.Length > 1)
  92. client.Self.Chat(randomf, 0, ChatType.Normal);
  93. //Thread.Sleep(somthing.Next(1, 10)); // Apparently its better without it right now.
  94. }
  95. /// <summary>
  96. /// Read the Nini config and initialize
  97. /// </summary>
  98. public void readconfig()
  99. {
  100. firstname = startupConfig.GetString("firstname", "random");
  101. lastname = startupConfig.GetString("lastname", "random");
  102. password = startupConfig.GetString("password", "12345");
  103. loginURI = startupConfig.GetString("loginuri");
  104. }
  105. /// <summary>
  106. /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes.
  107. /// </summary>
  108. public void shutdown()
  109. {
  110. client.Network.Logout();
  111. }
  112. /// <summary>
  113. /// This is the bot startup loop.
  114. /// </summary>
  115. public void startup()
  116. {
  117. client.Settings.LOGIN_SERVER = loginURI;
  118. client.Settings.ALWAYS_DECODE_OBJECTS = false;
  119. client.Settings.AVATAR_TRACKING = false;
  120. client.Settings.OBJECT_TRACKING = false;
  121. client.Settings.SEND_AGENT_THROTTLE = true;
  122. client.Settings.SEND_PINGS = true;
  123. client.Settings.STORE_LAND_PATCHES = false;
  124. client.Settings.USE_TEXTURE_CACHE = false;
  125. client.Settings.MULTIPLE_SIMS = true;
  126. client.Throttle.Asset = 100000;
  127. client.Throttle.Land = 100000;
  128. client.Throttle.Task = 100000;
  129. client.Throttle.Texture = 100000;
  130. client.Throttle.Wind = 100000;
  131. client.Throttle.Total = 400000;
  132. client.Network.OnConnected += new NetworkManager.ConnectedCallback(this.Network_OnConnected);
  133. client.Network.OnSimConnected += new NetworkManager.SimConnectedCallback(this.Network_OnConnected);
  134. client.Network.OnDisconnected += new NetworkManager.DisconnectedCallback(this.Network_OnDisconnected);
  135. client.Objects.OnNewPrim += Objects_NewPrim;
  136. client.Assets.OnImageReceived += Asset_TextureCallback;
  137. if (client.Network.Login(firstname, lastname, password, "pCampBot", "Your name"))
  138. {
  139. if (OnConnected != null)
  140. {
  141. m_action = new Timer(somthing.Next(1000, 10000));
  142. m_action.Elapsed += new ElapsedEventHandler(m_action_Elapsed);
  143. m_action.Start();
  144. OnConnected(this, EventType.CONNECTED);
  145. client.Self.Jump(true);
  146. }
  147. }
  148. else
  149. {
  150. MainConsole.Instance.Error(firstname + " " + lastname, "Can't login: " + client.Network.LoginMessage);
  151. if (OnDisconnected != null)
  152. {
  153. OnDisconnected(this, EventType.DISCONNECTED);
  154. }
  155. }
  156. }
  157. public void Network_OnConnected(object sender)
  158. {
  159. if (OnConnected != null)
  160. {
  161. OnConnected(this, EventType.CONNECTED);
  162. }
  163. }
  164. public void Simulator_Connected(object sender)
  165. {
  166. }
  167. public void Network_OnDisconnected(NetworkManager.DisconnectType reason, string message)
  168. {
  169. if (OnDisconnected != null)
  170. {
  171. OnDisconnected(this, EventType.DISCONNECTED);
  172. }
  173. }
  174. public void Objects_NewPrim(Simulator simulator, Primitive prim, ulong regionHandle, ushort timeDilation)
  175. {
  176. if (prim != null)
  177. {
  178. if (prim.Textures != null)
  179. {
  180. if (prim.Textures.DefaultTexture.TextureID != UUID.Zero)
  181. {
  182. client.Assets.RequestImage(prim.Textures.DefaultTexture.TextureID, ImageType.Normal);
  183. }
  184. for (int i = 0; i < prim.Textures.FaceTextures.Length; i++ )
  185. {
  186. if (prim.Textures.FaceTextures[i] != null)
  187. {
  188. if (prim.Textures.FaceTextures[i].TextureID != UUID.Zero)
  189. {
  190. client.Assets.RequestImage(prim.Textures.FaceTextures[i].TextureID, ImageType.Normal);
  191. }
  192. }
  193. }
  194. }
  195. if (prim.Sculpt.SculptTexture != UUID.Zero)
  196. {
  197. client.Assets.RequestImage(prim.Sculpt.SculptTexture, ImageType.Normal);
  198. }
  199. }
  200. }
  201. public void Asset_TextureCallback(ImageDownload image, AssetTexture asset)
  202. {
  203. }
  204. public string[] readexcuses()
  205. {
  206. string allexcuses = "";
  207. string file = Path.Combine(Util.configDir(), "pCampBotSentences.txt");
  208. if (File.Exists(file))
  209. {
  210. StreamReader csr = File.OpenText(file);
  211. allexcuses = csr.ReadToEnd();
  212. csr.Close();
  213. }
  214. return allexcuses.Split(Environment.NewLine.ToCharArray());
  215. }
  216. }
  217. }