//Microsoft .NET Framework using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; //MudEngine using MudEngine.FileSystem; using MudEngine.GameManagement; using MudEngine.GameObjects; using MudEngine.GameObjects.Characters; using MudEngine.GameObjects.Environment; using MudEngine.GameObjects.Items; using MudEngine.Scripting; namespace MudEngine.GameManagement { public enum ObjectTypes { Realm, Zone, Room, Character, Item, Standard, //Any } public class GameWorld { /// /// Gets the collection of Objects that do not belong to any other Type of categories. /// public List Objects { get; private set; } /// /// Gets the collection of Items that are available in the game world. /// public List Items { get; private set; } /// /// Gets the collection of Characters (NPC/Monster) that are available in the Game world /// //TODO: This should be BaseAI public List Characters { get; private set; } /// /// Gets the collection of Realms currently available in the game world /// public List RealmCollection { get; private set; } private Game _Game; public GameWorld(Game game) { _Game = game; Objects = new List(); Items = new List(); Characters = new List(); RealmCollection = new List(); } public void Start() { //See if we have an Initial Realm set //TODO: Check for saved Realm files and load Log.Write("Initializing World..."); foreach (Realm r in RealmCollection) { if (r.IsInitialRealm) { _Game.InitialRealm = r; break; } } //Check if any the initial room exists or not. if ((_Game.InitialRealm == null) || (_Game.InitialRealm.InitialZone == null) || (_Game.InitialRealm.InitialZone.InitialRoom == null)) { Log.Write("ERROR: No initial location defined. Game startup failed!"); Log.Write("Players will start in the Abyss. Each player will contain their own instance of this room."); //return false; } else Log.Write("Initial Location loaded-> " + _Game.InitialRealm.Name + "." + _Game.InitialRealm.InitialZone.Name + "." + _Game.InitialRealm.InitialZone.InitialRoom.Name); } public void Save() { //Save all of the Environments for (Int32 x = 0; x <= RealmCollection.Count - 1; x++) { RealmCollection[x].Save(_Game.DataPaths.Environment); } } public void Load() { String filename = _Game.GameTitle + ".ini"; //Restore initial realm String realmFile = FileManager.GetData(filename, "InitialRealm"); String realmFolder = Path.GetFileNameWithoutExtension(realmFile); String realmPath = Path.Combine(_Game.DataPaths.Environment, realmFolder, realmFile); foreach (String realm in FileManager.GetCollectionData(filename, "RealmCollection")) { Realm r = new Realm(_Game); r.Load(Path.Combine(realmPath, realm)); } foreach (Realm r in RealmCollection) { if (r.IsInitialRealm) { _Game.InitialRealm = r; break; } } } /// /// Adds a object to the game world. Any Game Object can be supplied as a parameter. /// /// public Boolean AddObject(dynamic worldObject) { //Check if this object is a Realm if (worldObject is Realm) { ///Add it to the Worlds Realm collection this.RealmCollection.Add(worldObject); } //Check if this object is a Zone else if (worldObject is Zone) { //Query the Realm collection to find the Realm this Zone belongs to. var realmQuery = from r in RealmCollection where r.Filename == worldObject.Realm select r; //Add the zone to the Realm we found if (realmQuery.FirstOrDefault() != null) realmQuery.FirstOrDefault().AddZone(worldObject); else { Log.Write("Error: Attempted to add Zone " + worldObject.Filename + " to a unspecified Realm."); return false; } } else if (worldObject is Room) { var realmQuery = from r in RealmCollection where r.Filename == worldObject.Realm select r; var zoneQuery = from z in realmQuery.FirstOrDefault().ZoneCollection where z.Filename == worldObject.Zone select z; if (zoneQuery.FirstOrDefault() != null) zoneQuery.FirstOrDefault().AddRoom(worldObject); else { Log.Write("Error: Attempted to add Room " + worldObject.Filename + " to a unspecified Realm and/or Zone"); return false; } } return true; } /// /// Adds a Realm to the Games current list of Realms. /// /// private void AddRealm(Realm realm) { //If this Realm is set as Initial then we need to disable any previously //set Realms to avoid conflict. if (realm.IsInitialRealm) { foreach (Realm r in RealmCollection) { if (r.IsInitialRealm) { r.IsInitialRealm = false; break; } } } //Set this Realm as the Games initial Realm if (realm.IsInitialRealm) _Game.InitialRealm = realm; //TODO: Check for duplicate Realms. RealmCollection.Add(realm); } /// /// Returs a reference to an object that matches the supplied Type and filename. /// Object MUST inherit from BaseObject or one of its child classes in order to be found. /// /// Determins the Type of object to perform the search for. Using Standard will search for objects that inherit from BaseObject, but none of BaseObjects child Types. /// /// public BaseObject GetObject(ObjectTypes objectType, String filename) { BaseObject obj = new BaseObject(_Game); switch (objectType) { case ObjectTypes.Standard: break; case ObjectTypes.Character: break; case ObjectTypes.Item: break; case ObjectTypes.Realm: obj = GetRealm(filename); break; case ObjectTypes.Zone: break; case ObjectTypes.Room: break; } return obj; } /// /// Returns a reference to the Realm contained within the Realms collection if it exists. /// /// /// private Realm GetRealm(String filename) { foreach (Realm r in RealmCollection) { if (r.Filename == filename) return r; } return null; } } }