diff --git a/MudEngine/Commands/CommandClear.cs b/MudEngine/Commands/CommandClear.cs new file mode 100644 index 0000000..92f74a8 --- /dev/null +++ b/MudEngine/Commands/CommandClear.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Text; + +using MudEngine.FileSystem; +using MudEngine.GameObjects.Characters; +using MudEngine.GameManagement; +using MudEngine.Commands; +using MudEngine.GameObjects.Environment; + +namespace MudEngine.Commands +{ + public class CommandClear : IGameCommand + { + public bool Override { get; set; } + public string Name { get; set; } + + public CommandResults Execute(string command, BaseCharacter player) + { + player.FlushConsole(); + + return new CommandResults(); + } + } +} diff --git a/MudEngine/Commands/CommandExit.cs b/MudEngine/Commands/CommandExit.cs index 6b9e2e8..585d299 100644 --- a/MudEngine/Commands/CommandExit.cs +++ b/MudEngine/Commands/CommandExit.cs @@ -43,7 +43,7 @@ namespace MudEngine.Commands { //Save the player prior to attempting to shutdown. //Player saving is handled in the server disconnect code but not in game shutdown. - player.Save(Path.Combine(player.ActiveGame.DataPaths.Players, player.Filename)); + player.Save(player.ActiveGame.DataPaths.Players); player.ActiveGame.Shutdown(); } return new CommandResults(); diff --git a/MudEngine/Commands/CommandLogin.cs b/MudEngine/Commands/CommandLogin.cs index b0d1597..7080b10 100644 --- a/MudEngine/Commands/CommandLogin.cs +++ b/MudEngine/Commands/CommandLogin.cs @@ -62,6 +62,9 @@ namespace MudEngine.Commands { player.Name = input; player.Send("Welcome " + player.Name + "!"); + + //Save the new player. + player.Save(player.ActiveGame.DataPaths.Players); } else { diff --git a/MudEngine/Commands/CommandSave.cs b/MudEngine/Commands/CommandSave.cs index 25bc50e..0760ab3 100644 --- a/MudEngine/Commands/CommandSave.cs +++ b/MudEngine/Commands/CommandSave.cs @@ -18,21 +18,18 @@ namespace MudEngine.Commands public CommandResults Execute(string command, BaseCharacter player) { - string path = player.ActiveGame.DataPaths.Players; - string filename = Path.Combine(path, player.Filename); - - //Temporary hack + /* if (player.ActiveGame.PlayerCollection.Length != 0) { - if (player.ActiveGame.PlayerCollection[0].GetType().Name != "BaseCharacter") + if (player.GetType().Name != "BaseCharacter") { Scripting.GameObject obj = player.ActiveGame.scriptEngine.GetObject(player.ActiveGame.PlayerCollection.GetType().Name); - obj.InvokeMethod("Save", new object[] { player.Name }); + obj.InvokeMethod("Save", new object[] { player.ActiveGame.DataPaths.Players }); } } - else - player.Save(filename); //Should never be called? + else */ + player.Save(player.ActiveGame.DataPaths.Players); return new CommandResults(); } diff --git a/MudEngine/GameManagement/Game.cs b/MudEngine/GameManagement/Game.cs index f695bf5..16b47e9 100644 --- a/MudEngine/GameManagement/Game.cs +++ b/MudEngine/GameManagement/Game.cs @@ -229,6 +229,11 @@ namespace MudEngine.GameManagement WorldTime.MinutesPerHour = 59; WorldTime.SecondsPerMinute = 59; } + + ~Game() + { + Server = null; + } #endregion #region ==== Methods ==== diff --git a/MudEngine/GameObjects/BaseObject.cs b/MudEngine/GameObjects/BaseObject.cs index d443e11..a370117 100644 --- a/MudEngine/GameObjects/BaseObject.cs +++ b/MudEngine/GameObjects/BaseObject.cs @@ -36,8 +36,6 @@ namespace MudEngine.GameObjects } private string _Name; - public Int32 ID { get; internal set; } - [Category("Object Setup")] [Description("A brief description of this object. The description is displayed to users when they use a command for investigating an object")] public string Description { get; set; } @@ -90,21 +88,11 @@ namespace MudEngine.GameObjects this.Name = DefaultName(); this.Filename = DefaultName() + "." + this.GetType().Name; - - //This must be called on instancing of the object. - //It is unique and will be used for saving the object. - //Allows for multiple objects with the same Name property - //but different Identifiers. Letting there be multiple versions - //of the same object. - - //ActiveGame.World.AddObject(this); } ~BaseObject() { - //We must free up this ID so that it can be used by other objects being instanced. - //ActiveGame.World.RemoveObject(this); } private bool ShouldSerializeName() @@ -160,22 +148,24 @@ namespace MudEngine.GameObjects { } - public virtual void Save(string filename) + public virtual void Save(string path) { - string path = Path.GetDirectoryName(filename); + if (String.IsNullOrEmpty(path)) + return; if (!Directory.Exists(path)) Directory.CreateDirectory(path); - if (File.Exists(filename)) - File.Delete(filename); + path = Path.Combine(path, Filename); - FileManager.WriteLine(filename, this.Name, "Name"); - FileManager.WriteLine(filename, this.ID.ToString(), "Identifier"); - FileManager.WriteLine(filename, this.Description, "Description"); - FileManager.WriteLine(filename, this.Feel, "Feel"); - FileManager.WriteLine(filename, this.Listen, "Listen"); - FileManager.WriteLine(filename, this.Smell, "Smell"); + if (File.Exists(path)) + File.Delete(path); + + FileManager.WriteLine(path, this.Name, "Name"); + FileManager.WriteLine(path, this.Description, "Description"); + FileManager.WriteLine(path, this.Feel, "Feel"); + FileManager.WriteLine(path, this.Listen, "Listen"); + FileManager.WriteLine(path, this.Smell, "Smell"); } public virtual void Load(string filename) diff --git a/MudEngine/GameObjects/Characters/BaseCharacter.cs b/MudEngine/GameObjects/Characters/BaseCharacter.cs index f944f93..e8740bb 100644 --- a/MudEngine/GameObjects/Characters/BaseCharacter.cs +++ b/MudEngine/GameObjects/Characters/BaseCharacter.cs @@ -97,15 +97,38 @@ namespace MudEngine.GameObjects.Characters return; } - Zone zone = realm.GetZoneByID(Convert.ToInt32(FileManager.GetData(filename, "CurrentZone"))); - - if (zone == null) + List zones = realm.GetZoneByFilename(FileManager.GetData(filename, "CurrentZone")); + Zone zone = new Zone(ActiveGame); + if (zones.Count == 0) { - zone = new Zone(ActiveGame); + Log.Write("Error: Failed to find " + FileManager.GetData(filename, "CurrentZone") + " zone."); return; } + else if (zones.Count == 1) + { + zone = zones[0]; + } + else + { + //TODO: determin which zone is the appropriate zone to assign if more than one exists. + } + + List rooms = zone.GetRoomByFilename(FileManager.GetData(filename, "CurrentRoom")); + + if (rooms.Count == 0) + { + Log.Write("Error: Failed to find " + FileManager.GetData(filename, "CurrentRoom") + " room."); + return; + } + else if (rooms.Count == 1) + { + CurrentRoom = rooms[0]; + } + else + { + //TODO: Loop through each found room and determin in some manor what should be the correct Room to assign. + } - CurrentRoom = zone.GetRoomByID(Convert.ToInt32(FileManager.GetData(filename, "CurrentRoom"))); if (CurrentRoom == null) { CurrentRoom = new Room(ActiveGame); @@ -115,19 +138,17 @@ namespace MudEngine.GameObjects.Characters } } - public override void Save(string filename) + public override void Save(string path) { - //Don't attempt to save a blank filename. - if (String.IsNullOrEmpty(filename)) - return; + base.Save(path); - base.Save(filename); + path = Path.Combine(path, Filename); - FileManager.WriteLine(filename, this.IsControlled.ToString(), "IsControlled"); - FileManager.WriteLine(filename, this.Role.ToString(), "Role"); - FileManager.WriteLine(filename, this.CurrentRoom.Name, "CurrentRoom"); - FileManager.WriteLine(filename, this.CurrentRoom.Zone, "CurrentZone"); - FileManager.WriteLine(filename, this.CurrentRoom.Realm, "CurrentRealm"); + FileManager.WriteLine(path, this.IsControlled.ToString(), "IsControlled"); + FileManager.WriteLine(path, this.Role.ToString(), "Role"); + FileManager.WriteLine(path, this.CurrentRoom.Filename, "CurrentRoom"); + FileManager.WriteLine(path, this.CurrentRoom.Zone, "CurrentZone"); + FileManager.WriteLine(path, this.CurrentRoom.Realm, "CurrentRealm"); } /// @@ -150,11 +171,11 @@ namespace MudEngine.GameObjects.Characters if (ActiveGame.PlayerCollection[i].Name == Name) continue; - string room = ActiveGame.PlayerCollection[i].CurrentRoom.Name; + string room = ActiveGame.PlayerCollection[i].CurrentRoom.Filename; string realm = ActiveGame.PlayerCollection[i].CurrentRoom.Realm; string zone = ActiveGame.PlayerCollection[i].CurrentRoom.Zone; - if ((room == CurrentRoom.Name) && (realm == CurrentRoom.Realm) && (zone == CurrentRoom.Zone)) + if ((room == CurrentRoom.Filename) && (realm == CurrentRoom.Realm) && (zone == CurrentRoom.Zone)) { ActiveGame.PlayerCollection[i].Send(Name + " walked out towards the " + travelDirection.ToString()); } @@ -289,12 +310,34 @@ namespace MudEngine.GameObjects.Characters Send(data, true); } + internal void FlushConsole() + { + try + { + if (ActiveGame.IsMultiplayer) + { + System.Text.ASCIIEncoding encoding = new ASCIIEncoding(); + + //\x1B is hex + //[2J is terminal sequences for clearing the screen. + client.Send(encoding.GetBytes("\x1B[2J")); + //[H is terminal sequence for sending the cursor back to its home position. + client.Send(encoding.GetBytes("\x1B[H")); + } + else + Console.Clear(); + } + catch + { + Disconnect(); + } + } + internal void Disconnect() { if (IsActive) { - string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename); - this.Save(filePath); + this.Save(ActiveGame.DataPaths.Players); Send("Goodbye!"); IsActive = false; diff --git a/MudEngine/GameObjects/Environment/Realm.cs b/MudEngine/GameObjects/Environment/Realm.cs index f85d5cc..d07626d 100644 --- a/MudEngine/GameObjects/Environment/Realm.cs +++ b/MudEngine/GameObjects/Environment/Realm.cs @@ -35,31 +35,24 @@ namespace MudEngine.GameObjects.Environment ZoneCollection = new List(); } - /// - /// Returns the requested Zone if the Zone exists within this Realm. - /// - /// - /// - public Zone GetZoneByID(Int32 id) + public override void Save(string path) { - foreach (Zone zone in ZoneCollection) - { - if (zone.ID == id) - return zone; - } + base.Save(path); - return null; + //TODO: Save Realm collections and Zone to file. } - public List GetZoneByName(string name) + public List GetZoneByFilename(string filename) { List zones = new List(); foreach (Zone zone in ZoneCollection) { - if (zone.Name == name) + if (zone.Filename == filename) + { zones.Add(zone); + } } return zones; @@ -85,7 +78,7 @@ namespace MudEngine.GameObjects.Environment //TODO: Check fo duplicates ZoneCollection.Add(zone); - zone.Realm = Name; + zone.Realm = Filename; } } } diff --git a/MudEngine/GameObjects/Environment/Zone.cs b/MudEngine/GameObjects/Environment/Zone.cs index 1d445a9..3241732 100644 --- a/MudEngine/GameObjects/Environment/Zone.cs +++ b/MudEngine/GameObjects/Environment/Zone.cs @@ -83,21 +83,6 @@ namespace MudEngine.GameObjects.Environment Realm = "No Realm Associated."; } - /// - /// - /// - /// - /// - public Room GetRoomByID(Int32 id) - { - foreach (Room room in RoomCollection) - { - if (room.ID == id) - return room; - } - return null; - } - /// /// Adds the supplied room into the Zones Room collection. /// @@ -122,10 +107,26 @@ namespace MudEngine.GameObjects.Environment //TODO: Check for duplicate Rooms. RoomCollection.Add(room); - room.Zone = Name; + room.Zone = Filename; room.Realm = Realm; } + public List GetRoomByFilename(string filename) + { + + List rooms = new List(); + + foreach (Room r in RoomCollection) + { + if (r.Filename == filename) + { + rooms.Add(r); + } + } + + return rooms; + } + public void LinkRooms(AvailableTravelDirections departureDirection, Room arrivalRoom, Room departureRoom) { LinkRooms(departureDirection, arrivalRoom, departureRoom, 0); diff --git a/MudEngine/MudEngine.csproj b/MudEngine/MudEngine.csproj index 90a1100..c7090c8 100644 --- a/MudEngine/MudEngine.csproj +++ b/MudEngine/MudEngine.csproj @@ -58,6 +58,7 @@ + diff --git a/MudGame/Program.cs b/MudGame/Program.cs index a206e3e..8a0319b 100644 --- a/MudGame/Program.cs +++ b/MudGame/Program.cs @@ -15,13 +15,14 @@ namespace MudGame static class Program { const string SettingsFile = "Settings.ini"; + static Game game; static void Main(string[] args) { Log.Write("Launching..."); ScriptEngine scriptEngine; - Game game; + //Re-create the settings file if it is missing if (!File.Exists(SettingsFile)) @@ -111,6 +112,7 @@ namespace MudGame while (game.IsRunning) { game.Update(); + System.Threading.Thread.Sleep(1); } } }