PhysicsBot.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 OpenSimulator 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.IO;
  29. using System.Threading;
  30. using System.Timers;
  31. using OpenMetaverse;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using Timer=System.Timers.Timer;
  36. namespace OpenSim.TestSuite
  37. {
  38. public class PhysicsBot
  39. {
  40. public delegate void AnEvent(PhysicsBot callbot, EventType someevent); // event delegate for bot events
  41. public IConfig startupConfig; // bot config, passed from BotManager
  42. public string firstname;
  43. public string lastname;
  44. public string password;
  45. public string loginURI;
  46. public event AnEvent OnConnected;
  47. public event AnEvent OnDisconnected;
  48. protected Timer m_action; // Action Timer
  49. protected Random somthing = new Random(Environment.TickCount);// We do stuff randomly here
  50. //New instance of a SecondLife client
  51. public GridClient client = new GridClient();
  52. protected string[] talkarray;
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. /// <param name="bsconfig">nini config for the bot</param>
  57. public PhysicsBot(IConfig bsconfig)
  58. {
  59. startupConfig = bsconfig;
  60. readconfig();
  61. talkarray = readexcuses();
  62. }
  63. //We do our actions here. This is where one would
  64. //add additional steps and/or things the bot should do
  65. void m_action_Elapsed(object sender, ElapsedEventArgs e)
  66. {
  67. //client.Throttle.Task = 500000f;
  68. //client.Throttle.Set();
  69. int walkorrun = somthing.Next(4); // Randomize between walking and running. The greater this number,
  70. // the greater the bot's chances to walk instead of run.
  71. if (walkorrun == 0)
  72. {
  73. client.Self.Movement.AlwaysRun = true;
  74. }
  75. else
  76. {
  77. client.Self.Movement.AlwaysRun = false;
  78. }
  79. // TODO: unused: Vector3 pos = client.Self.SimPosition;
  80. Vector3 newpos = new Vector3(somthing.Next(255), somthing.Next(255), somthing.Next(255));
  81. client.Self.Movement.TurnToward(newpos);
  82. for (int i = 0; i < 2000; i++)
  83. {
  84. client.Self.Movement.AtPos = true;
  85. Thread.Sleep(somthing.Next(25, 75)); // Makes sure the bots keep walking for this time.
  86. }
  87. client.Self.Jump(true);
  88. string randomf = talkarray[somthing.Next(talkarray.Length)];
  89. if (talkarray.Length > 1 && randomf.Length > 1)
  90. client.Self.Chat(randomf, 0, ChatType.Normal);
  91. //Thread.Sleep(somthing.Next(1, 10)); // Apparently its better without it right now.
  92. }
  93. /// <summary>
  94. /// Read the Nini config and initialize
  95. /// </summary>
  96. public void readconfig()
  97. {
  98. firstname = startupConfig.GetString("firstname", "random");
  99. lastname = startupConfig.GetString("lastname", "random");
  100. password = startupConfig.GetString("password", "12345");
  101. loginURI = startupConfig.GetString("loginuri");
  102. }
  103. /// <summary>
  104. /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes.
  105. /// </summary>
  106. public void shutdown()
  107. {
  108. client.Network.Logout();
  109. }
  110. /// <summary>
  111. /// This is the bot startup loop.
  112. /// </summary>
  113. public void startup()
  114. {
  115. client.Settings.LOGIN_SERVER = loginURI;
  116. client.Network.OnConnected += new NetworkManager.ConnectedCallback(this.Network_OnConnected);
  117. client.Network.OnSimConnected += new NetworkManager.SimConnectedCallback(this.Network_OnConnected);
  118. client.Network.OnDisconnected += new NetworkManager.DisconnectedCallback(this.Network_OnDisconnected);
  119. if (client.Network.Login(firstname, lastname, password, "pCampBot", "Your name"))
  120. {
  121. if (OnConnected != null)
  122. {
  123. m_action = new Timer(somthing.Next(1000, 10000));
  124. m_action.Elapsed += new ElapsedEventHandler(m_action_Elapsed);
  125. m_action.Start();
  126. OnConnected(this, EventType.CONNECTED);
  127. client.Self.Jump(true);
  128. }
  129. }
  130. else
  131. {
  132. MainConsole.Instance.Output(firstname + " " + lastname + "Can't login: " + client.Network.LoginMessage);
  133. if (OnDisconnected != null)
  134. {
  135. OnDisconnected(this, EventType.DISCONNECTED);
  136. }
  137. }
  138. }
  139. public void Network_OnConnected(object sender)
  140. {
  141. if (OnConnected != null)
  142. {
  143. OnConnected(this, EventType.CONNECTED);
  144. }
  145. }
  146. public void Simulator_Connected(object sender)
  147. {
  148. }
  149. public void Network_OnDisconnected(NetworkManager.DisconnectType reason, string message)
  150. {
  151. if (OnDisconnected != null)
  152. {
  153. OnDisconnected(this, EventType.DISCONNECTED);
  154. }
  155. }
  156. public string[] readexcuses()
  157. {
  158. string allexcuses = "";
  159. string file = Path.Combine(Util.configDir(), "pCampBotSentences.txt");
  160. if (File.Exists(file))
  161. {
  162. StreamReader csr = File.OpenText(file);
  163. allexcuses = csr.ReadToEnd();
  164. csr.Close();
  165. }
  166. return allexcuses.Split(Environment.NewLine.ToCharArray());
  167. }
  168. }
  169. }