Annotation of /trunk/ModularRex/RexBot/RexBotManager.cs
Parent Directory
|
Revision Log
Revision 98 - (view) (download)
| 1 : | tuco | 88 | /* |
| 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 : | |||
| 28 : | using System; | ||
| 29 : | using System.Collections.Generic; | ||
| 30 : | using System.IO; | ||
| 31 : | using System.Text; | ||
| 32 : | using System.Xml; | ||
| 33 : | using OpenMetaverse; | ||
| 34 : | using Nini.Config; | ||
| 35 : | using OpenSim.Framework; | ||
| 36 : | using OpenSim.Region.Framework.Interfaces; | ||
| 37 : | using OpenSim.Region.Framework.Scenes; | ||
| 38 : | |||
| 39 : | namespace OpenSim.Region.Examples.RexBot | ||
| 40 : | { | ||
| 41 : | public class RexBotManager : IRegionModule | ||
| 42 : | { | ||
| 43 : | #region IRegionModule Members | ||
| 44 : | |||
| 45 : | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | ||
| 46 : | private const string DEFAULT_CONFIG_FILENAME = "RexBots.xml"; | ||
| 47 : | |||
| 48 : | private Scene m_scene; | ||
| 49 : | private List<RexBot> m_bots; | ||
| 50 : | |||
| 51 : | private AgentCircuitData m_aCircuitData; | ||
| 52 : | |||
| 53 : | private NavMeshManager m_navMeshManager; | ||
| 54 : | |||
| 55 : | public void Initialise(Scene scene, IConfigSource source) | ||
| 56 : | { | ||
| 57 : | m_navMeshManager = new NavMeshManager(); | ||
| 58 : | |||
| 59 : | m_scene = scene; | ||
| 60 : | m_aCircuitData = new AgentCircuitData(); | ||
| 61 : | m_aCircuitData.child = false; | ||
| 62 : | m_bots = new List<RexBot>(); | ||
| 63 : | } | ||
| 64 : | |||
| 65 : | public void PostInitialise() | ||
| 66 : | { | ||
| 67 : | RegionInfo regionInfo = m_scene.RegionInfo; | ||
| 68 : | |||
| 69 : | Vector3 pos = new Vector3(110, 129, 27); | ||
| 70 : | |||
| 71 : | AddAvatars(); | ||
| 72 : | } | ||
| 73 : | |||
| 74 : | // add avatar defined in config file to scene | ||
| 75 : | private void AddAvatars() | ||
| 76 : | { | ||
| 77 : | readBotConfig(); | ||
| 78 : | } | ||
| 79 : | |||
| 80 : | // read bot data from config file and add avatars to scene | ||
| 81 : | private void readBotConfig() | ||
| 82 : | { | ||
| 83 : | mikkopa | 98 | //No need to warn |
| 84 : | //m_log.Warn("[RexBotManager]: Reading bot config file."); | ||
| 85 : | tuco | 88 | XmlDocument xml = new XmlDocument(); |
| 86 : | try | ||
| 87 : | { | ||
| 88 : | string file = Path.Combine(Util.configDir(), DEFAULT_CONFIG_FILENAME); | ||
| 89 : | xml.Load(file); | ||
| 90 : | |||
| 91 : | XmlNodeList paths = xml.GetElementsByTagName("navi_mesh"); | ||
| 92 : | foreach (XmlNode node in paths) | ||
| 93 : | { | ||
| 94 : | createNavMesh(node); | ||
| 95 : | } | ||
| 96 : | |||
| 97 : | XmlNodeList bots = xml.GetElementsByTagName("bot"); | ||
| 98 : | foreach (XmlNode node in bots) | ||
| 99 : | { | ||
| 100 : | createAvatar(node); | ||
| 101 : | } | ||
| 102 : | } | ||
| 103 : | catch (System.IO.FileNotFoundException) | ||
| 104 : | { | ||
| 105 : | mikkopa | 98 | m_log.InfoFormat("[RexBotManager]: Bot config file {0} not present. Bots not loaded.", DEFAULT_CONFIG_FILENAME); |
| 106 : | tuco | 88 | } |
| 107 : | catch (System.IO.IOException e) | ||
| 108 : | { | ||
| 109 : | m_log.Warn("[RexBotManager]: Failed to load bot config file: " + DEFAULT_CONFIG_FILENAME + ". Reason: " + e.Message); | ||
| 110 : | } | ||
| 111 : | catch (System.Xml.XmlException e) | ||
| 112 : | { | ||
| 113 : | m_log.Error("[RexBotManager]: Failed to parse bot config file: " + DEFAULT_CONFIG_FILENAME + ". Reason: " + e.Message); | ||
| 114 : | } | ||
| 115 : | } | ||
| 116 : | |||
| 117 : | // read config data for single navmesh | ||
| 118 : | private void createNavMesh(XmlNode node) | ||
| 119 : | { | ||
| 120 : | NavMesh mesh = new NavMesh(); | ||
| 121 : | NavMeshSerializer serializer = new NavMeshSerializer(); | ||
| 122 : | serializer.Import(mesh, node); | ||
| 123 : | try | ||
| 124 : | { | ||
| 125 : | m_navMeshManager.AddNavMesh(mesh); | ||
| 126 : | } | ||
| 127 : | catch (System.ArgumentException) | ||
| 128 : | { | ||
| 129 : | m_log.Error("[RexBotManager]: NavMesh with the name: " + mesh.Name + " already exists! NavMeshes must be unique."); | ||
| 130 : | } | ||
| 131 : | } | ||
| 132 : | |||
| 133 : | // read config data for single bot and add the avatar to the scene | ||
| 134 : | private void createAvatar(XmlNode node) | ||
| 135 : | { | ||
| 136 : | RexBot m_character = new RexBot(m_scene, m_navMeshManager); | ||
| 137 : | RexBotSerializer serializer = new RexBotSerializer(); | ||
| 138 : | serializer.ImportName(m_character, node); // import avatar name from file, we need it early | ||
| 139 : | |||
| 140 : | m_aCircuitData.firstname = m_character.FirstName; | ||
| 141 : | m_aCircuitData.lastname = m_character.LastName; | ||
| 142 : | m_aCircuitData.circuitcode = m_character.CircuitCode; | ||
| 143 : | m_scene.AuthenticateHandler.AgentCircuits.Add(m_character.CircuitCode, m_aCircuitData); | ||
| 144 : | |||
| 145 : | m_scene.AddNewClient(m_character); | ||
| 146 : | m_character.Initialize(); | ||
| 147 : | m_bots.Add(m_character); | ||
| 148 : | |||
| 149 : | serializer.ImportRexBot(m_character, node); // import after bot has been properly added to scene, because import might call some code that needs it | ||
| 150 : | |||
| 151 : | m_log.Info("[RexBotManager]: Added bot " + m_character.Name + " to scene."); | ||
| 152 : | } | ||
| 153 : | |||
| 154 : | public void Close() | ||
| 155 : | { | ||
| 156 : | |||
| 157 : | m_scene = null; | ||
| 158 : | m_bots.Clear(); | ||
| 159 : | } | ||
| 160 : | |||
| 161 : | public string Name | ||
| 162 : | { | ||
| 163 : | get { return GetType().AssemblyQualifiedName; } | ||
| 164 : | } | ||
| 165 : | |||
| 166 : | public bool IsSharedModule | ||
| 167 : | { | ||
| 168 : | get { return false; } | ||
| 169 : | } | ||
| 170 : | |||
| 171 : | #endregion | ||
| 172 : | } | ||
| 173 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

