MudEngine:

- Added BaseCharacter.FlushConsole() method. Sends a hex sequence to the clients telnet terminal that clears the screen. If IsMultiplayer=false then it just clears the C# Console.
 - Added a 'Clear' command for users to use. It invokes the new FlushConsole command.
 - Removed the need for a full file path and filename when calling an objects save method. Now it just needs the path.
 - Adjusted the Exit command, Login command and Save command to reflect the objects save parameter changes.
 - Removed the Unique ID from objects. 
 - All objects now reference each other via their filenames rather than their object names. Allows for 3 objects with the name Bedroom to exist, but with different filenames such as Bedroom1, Bedroom2 etc.
 - Zone now has a GetRoomByName method that replace the removed GetRoomByID method. Returns a List<> collection of Rooms found with a matching filename.
 - BaseCharacter updated to work with the new Zone.GetRoomByName method.
 - Realm.GetZoneByID renamed to GetZoneByName()
This commit is contained in:
Scionwest_cp 2010-08-13 21:35:46 -07:00
parent 717034f9ed
commit 9585cede63
11 changed files with 144 additions and 82 deletions

View file

@ -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();
}
}
}

View file

@ -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();

View file

@ -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
{

View file

@ -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();
}

View file

@ -229,6 +229,11 @@ namespace MudEngine.GameManagement
WorldTime.MinutesPerHour = 59;
WorldTime.SecondsPerMinute = 59;
}
~Game()
{
Server = null;
}
#endregion
#region ==== Methods ====

View file

@ -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)

View file

@ -97,15 +97,38 @@ namespace MudEngine.GameObjects.Characters
return;
}
Zone zone = realm.GetZoneByID(Convert.ToInt32(FileManager.GetData(filename, "CurrentZone")));
if (zone == null)
List<Zone> 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<Room> 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");
}
/// <summary>
@ -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;

View file

@ -35,32 +35,25 @@ namespace MudEngine.GameObjects.Environment
ZoneCollection = new List<Zone>();
}
/// <summary>
/// Returns the requested Zone if the Zone exists within this Realm.
/// </summary>
/// <param name="zoneName"></param>
/// <returns></returns>
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);
//TODO: Save Realm collections and Zone to file.
}
return null;
}
public List<Zone> GetZoneByName(string name)
public List<Zone> GetZoneByFilename(string filename)
{
List<Zone> zones = new List<Zone>();
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;
}
}
}

View file

@ -83,21 +83,6 @@ namespace MudEngine.GameObjects.Environment
Realm = "No Realm Associated.";
}
/// <summary>
///
/// </summary>
/// <param name="RoomName"></param>
/// <returns></returns>
public Room GetRoomByID(Int32 id)
{
foreach (Room room in RoomCollection)
{
if (room.ID == id)
return room;
}
return null;
}
/// <summary>
/// Adds the supplied room into the Zones Room collection.
/// </summary>
@ -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<Room> GetRoomByFilename(string filename)
{
List<Room> rooms = new List<Room>();
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);

View file

@ -58,6 +58,7 @@
<Compile Include="Commands\CommandWalk.cs" />
<Compile Include="Commands\CommandLoad.cs" />
<Compile Include="Commands\CommandLogin.cs" />
<Compile Include="Commands\CommandClear.cs" />
<Compile Include="FileSystem\SaveDataPaths.cs" />
<Compile Include="GameManagement\CommandEngine.cs" />
<Compile Include="GameManagement\CommandResults.cs" />

View file

@ -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);
}
}
}