PhysicsBot.cs 7.3 KB

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