PhysicsBot.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. namespace pCampBot
  39. {
  40. public class PhysicsBot
  41. {
  42. public delegate void AnEvent(PhysicsBot callbot, EventType someevent); // event delegate for bot events
  43. public IConfig startupConfig; // bot config, passed from BotManager
  44. public string firstname;
  45. public string lastname;
  46. public string password;
  47. public string loginURI;
  48. public event AnEvent OnConnected;
  49. public event AnEvent OnDisconnected;
  50. protected Timer m_action; // Action Timer
  51. protected Random somthing = new Random(System.Environment.TickCount);// We do stuff randomly here
  52. //New instance of a SecondLife client
  53. public SecondLife client = new SecondLife();
  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, System.Timers.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: LLVector3 pos = client.Self.SimPosition;
  82. LLVector3 newpos = new LLVector3(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();
  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.Network.OnConnected += new NetworkManager.ConnectedCallback(this.Network_OnConnected);
  119. client.Network.OnSimConnected += new NetworkManager.SimConnectedCallback(this.Network_OnConnected);
  120. client.Network.OnDisconnected += new NetworkManager.DisconnectedCallback(this.Network_OnDisconnected);
  121. if (client.Network.Login(firstname, lastname, password, "pCampBot", "Your name"))
  122. {
  123. if (OnConnected != null)
  124. {
  125. m_action = new Timer(somthing.Next(1000, 10000));
  126. m_action.Elapsed += new System.Timers.ElapsedEventHandler(m_action_Elapsed);
  127. m_action.Start();
  128. OnConnected(this, EventType.CONNECTED);
  129. client.Self.Jump();
  130. }
  131. }
  132. else
  133. {
  134. MainConsole.Instance.Error(firstname + " " + lastname, "Can't login: " + client.Network.LoginMessage);
  135. if (OnDisconnected != null)
  136. {
  137. OnDisconnected(this, EventType.DISCONNECTED);
  138. }
  139. }
  140. }
  141. public void Network_OnConnected(object sender)
  142. {
  143. if (OnConnected != null)
  144. {
  145. OnConnected(this, EventType.CONNECTED);
  146. }
  147. }
  148. public void Simulator_Connected(object sender)
  149. {
  150. }
  151. public void Network_OnDisconnected(NetworkManager.DisconnectType reason, string message)
  152. {
  153. if (OnDisconnected != null)
  154. {
  155. OnDisconnected(this, EventType.DISCONNECTED);
  156. }
  157. }
  158. public string[] readexcuses()
  159. {
  160. string allexcuses = "";
  161. string file = Path.Combine(Util.configDir(), "pCampBotSentences.txt");
  162. if (File.Exists(file))
  163. {
  164. StreamReader csr = File.OpenText(file);
  165. allexcuses = csr.ReadToEnd();
  166. csr.Close();
  167. }
  168. return allexcuses.Split(Environment.NewLine.ToCharArray());
  169. }
  170. }
  171. }