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:
parent
717034f9ed
commit
9585cede63
11 changed files with 144 additions and 82 deletions
27
MudEngine/Commands/CommandClear.cs
Normal file
27
MudEngine/Commands/CommandClear.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,7 +43,7 @@ namespace MudEngine.Commands
|
||||||
{
|
{
|
||||||
//Save the player prior to attempting to shutdown.
|
//Save the player prior to attempting to shutdown.
|
||||||
//Player saving is handled in the server disconnect code but not in game 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();
|
player.ActiveGame.Shutdown();
|
||||||
}
|
}
|
||||||
return new CommandResults();
|
return new CommandResults();
|
||||||
|
|
|
@ -62,6 +62,9 @@ namespace MudEngine.Commands
|
||||||
{
|
{
|
||||||
player.Name = input;
|
player.Name = input;
|
||||||
player.Send("Welcome " + player.Name + "!");
|
player.Send("Welcome " + player.Name + "!");
|
||||||
|
|
||||||
|
//Save the new player.
|
||||||
|
player.Save(player.ActiveGame.DataPaths.Players);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,21 +18,18 @@ namespace MudEngine.Commands
|
||||||
|
|
||||||
public CommandResults Execute(string command, BaseCharacter player)
|
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.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);
|
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
|
else */
|
||||||
player.Save(filename); //Should never be called?
|
player.Save(player.ActiveGame.DataPaths.Players);
|
||||||
|
|
||||||
return new CommandResults();
|
return new CommandResults();
|
||||||
}
|
}
|
||||||
|
|
|
@ -229,6 +229,11 @@ namespace MudEngine.GameManagement
|
||||||
WorldTime.MinutesPerHour = 59;
|
WorldTime.MinutesPerHour = 59;
|
||||||
WorldTime.SecondsPerMinute = 59;
|
WorldTime.SecondsPerMinute = 59;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~Game()
|
||||||
|
{
|
||||||
|
Server = null;
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region ==== Methods ====
|
#region ==== Methods ====
|
||||||
|
|
|
@ -36,8 +36,6 @@ namespace MudEngine.GameObjects
|
||||||
}
|
}
|
||||||
private string _Name;
|
private string _Name;
|
||||||
|
|
||||||
public Int32 ID { get; internal set; }
|
|
||||||
|
|
||||||
[Category("Object Setup")]
|
[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")]
|
[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; }
|
public string Description { get; set; }
|
||||||
|
@ -90,21 +88,11 @@ namespace MudEngine.GameObjects
|
||||||
this.Name = DefaultName();
|
this.Name = DefaultName();
|
||||||
|
|
||||||
this.Filename = DefaultName() + "." + this.GetType().Name;
|
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()
|
~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()
|
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))
|
if (!Directory.Exists(path))
|
||||||
Directory.CreateDirectory(path);
|
Directory.CreateDirectory(path);
|
||||||
|
|
||||||
if (File.Exists(filename))
|
path = Path.Combine(path, Filename);
|
||||||
File.Delete(filename);
|
|
||||||
|
|
||||||
FileManager.WriteLine(filename, this.Name, "Name");
|
if (File.Exists(path))
|
||||||
FileManager.WriteLine(filename, this.ID.ToString(), "Identifier");
|
File.Delete(path);
|
||||||
FileManager.WriteLine(filename, this.Description, "Description");
|
|
||||||
FileManager.WriteLine(filename, this.Feel, "Feel");
|
FileManager.WriteLine(path, this.Name, "Name");
|
||||||
FileManager.WriteLine(filename, this.Listen, "Listen");
|
FileManager.WriteLine(path, this.Description, "Description");
|
||||||
FileManager.WriteLine(filename, this.Smell, "Smell");
|
FileManager.WriteLine(path, this.Feel, "Feel");
|
||||||
|
FileManager.WriteLine(path, this.Listen, "Listen");
|
||||||
|
FileManager.WriteLine(path, this.Smell, "Smell");
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void Load(string filename)
|
public virtual void Load(string filename)
|
||||||
|
|
|
@ -97,15 +97,38 @@ namespace MudEngine.GameObjects.Characters
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Zone zone = realm.GetZoneByID(Convert.ToInt32(FileManager.GetData(filename, "CurrentZone")));
|
List<Zone> zones = realm.GetZoneByFilename(FileManager.GetData(filename, "CurrentZone"));
|
||||||
|
Zone zone = new Zone(ActiveGame);
|
||||||
if (zone == null)
|
if (zones.Count == 0)
|
||||||
{
|
{
|
||||||
zone = new Zone(ActiveGame);
|
Log.Write("Error: Failed to find " + FileManager.GetData(filename, "CurrentZone") + " zone.");
|
||||||
return;
|
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)
|
if (CurrentRoom == null)
|
||||||
{
|
{
|
||||||
CurrentRoom = new Room(ActiveGame);
|
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.
|
base.Save(path);
|
||||||
if (String.IsNullOrEmpty(filename))
|
|
||||||
return;
|
|
||||||
|
|
||||||
base.Save(filename);
|
path = Path.Combine(path, Filename);
|
||||||
|
|
||||||
FileManager.WriteLine(filename, this.IsControlled.ToString(), "IsControlled");
|
FileManager.WriteLine(path, this.IsControlled.ToString(), "IsControlled");
|
||||||
FileManager.WriteLine(filename, this.Role.ToString(), "Role");
|
FileManager.WriteLine(path, this.Role.ToString(), "Role");
|
||||||
FileManager.WriteLine(filename, this.CurrentRoom.Name, "CurrentRoom");
|
FileManager.WriteLine(path, this.CurrentRoom.Filename, "CurrentRoom");
|
||||||
FileManager.WriteLine(filename, this.CurrentRoom.Zone, "CurrentZone");
|
FileManager.WriteLine(path, this.CurrentRoom.Zone, "CurrentZone");
|
||||||
FileManager.WriteLine(filename, this.CurrentRoom.Realm, "CurrentRealm");
|
FileManager.WriteLine(path, this.CurrentRoom.Realm, "CurrentRealm");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -150,11 +171,11 @@ namespace MudEngine.GameObjects.Characters
|
||||||
if (ActiveGame.PlayerCollection[i].Name == Name)
|
if (ActiveGame.PlayerCollection[i].Name == Name)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
string room = ActiveGame.PlayerCollection[i].CurrentRoom.Name;
|
string room = ActiveGame.PlayerCollection[i].CurrentRoom.Filename;
|
||||||
string realm = ActiveGame.PlayerCollection[i].CurrentRoom.Realm;
|
string realm = ActiveGame.PlayerCollection[i].CurrentRoom.Realm;
|
||||||
string zone = ActiveGame.PlayerCollection[i].CurrentRoom.Zone;
|
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());
|
ActiveGame.PlayerCollection[i].Send(Name + " walked out towards the " + travelDirection.ToString());
|
||||||
}
|
}
|
||||||
|
@ -289,12 +310,34 @@ namespace MudEngine.GameObjects.Characters
|
||||||
Send(data, true);
|
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()
|
internal void Disconnect()
|
||||||
{
|
{
|
||||||
if (IsActive)
|
if (IsActive)
|
||||||
{
|
{
|
||||||
string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename);
|
this.Save(ActiveGame.DataPaths.Players);
|
||||||
this.Save(filePath);
|
|
||||||
|
|
||||||
Send("Goodbye!");
|
Send("Goodbye!");
|
||||||
IsActive = false;
|
IsActive = false;
|
||||||
|
|
|
@ -35,32 +35,25 @@ namespace MudEngine.GameObjects.Environment
|
||||||
ZoneCollection = new List<Zone>();
|
ZoneCollection = new List<Zone>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
public override void Save(string path)
|
||||||
/// Returns the requested Zone if the Zone exists within this Realm.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="zoneName"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public Zone GetZoneByID(Int32 id)
|
|
||||||
{
|
{
|
||||||
foreach (Zone zone in ZoneCollection)
|
base.Save(path);
|
||||||
{
|
|
||||||
if (zone.ID == id)
|
//TODO: Save Realm collections and Zone to file.
|
||||||
return zone;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
public List<Zone> GetZoneByFilename(string filename)
|
||||||
}
|
|
||||||
|
|
||||||
public List<Zone> GetZoneByName(string name)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
List<Zone> zones = new List<Zone>();
|
List<Zone> zones = new List<Zone>();
|
||||||
|
|
||||||
foreach (Zone zone in ZoneCollection)
|
foreach (Zone zone in ZoneCollection)
|
||||||
{
|
{
|
||||||
if (zone.Name == name)
|
if (zone.Filename == filename)
|
||||||
|
{
|
||||||
zones.Add(zone);
|
zones.Add(zone);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return zones;
|
return zones;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +78,7 @@ namespace MudEngine.GameObjects.Environment
|
||||||
|
|
||||||
//TODO: Check fo duplicates
|
//TODO: Check fo duplicates
|
||||||
ZoneCollection.Add(zone);
|
ZoneCollection.Add(zone);
|
||||||
zone.Realm = Name;
|
zone.Realm = Filename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,21 +83,6 @@ namespace MudEngine.GameObjects.Environment
|
||||||
Realm = "No Realm Associated.";
|
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>
|
/// <summary>
|
||||||
/// Adds the supplied room into the Zones Room collection.
|
/// Adds the supplied room into the Zones Room collection.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -122,10 +107,26 @@ namespace MudEngine.GameObjects.Environment
|
||||||
|
|
||||||
//TODO: Check for duplicate Rooms.
|
//TODO: Check for duplicate Rooms.
|
||||||
RoomCollection.Add(room);
|
RoomCollection.Add(room);
|
||||||
room.Zone = Name;
|
room.Zone = Filename;
|
||||||
room.Realm = Realm;
|
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)
|
public void LinkRooms(AvailableTravelDirections departureDirection, Room arrivalRoom, Room departureRoom)
|
||||||
{
|
{
|
||||||
LinkRooms(departureDirection, arrivalRoom, departureRoom, 0);
|
LinkRooms(departureDirection, arrivalRoom, departureRoom, 0);
|
||||||
|
|
|
@ -58,6 +58,7 @@
|
||||||
<Compile Include="Commands\CommandWalk.cs" />
|
<Compile Include="Commands\CommandWalk.cs" />
|
||||||
<Compile Include="Commands\CommandLoad.cs" />
|
<Compile Include="Commands\CommandLoad.cs" />
|
||||||
<Compile Include="Commands\CommandLogin.cs" />
|
<Compile Include="Commands\CommandLogin.cs" />
|
||||||
|
<Compile Include="Commands\CommandClear.cs" />
|
||||||
<Compile Include="FileSystem\SaveDataPaths.cs" />
|
<Compile Include="FileSystem\SaveDataPaths.cs" />
|
||||||
<Compile Include="GameManagement\CommandEngine.cs" />
|
<Compile Include="GameManagement\CommandEngine.cs" />
|
||||||
<Compile Include="GameManagement\CommandResults.cs" />
|
<Compile Include="GameManagement\CommandResults.cs" />
|
||||||
|
|
|
@ -15,13 +15,14 @@ namespace MudGame
|
||||||
static class Program
|
static class Program
|
||||||
{
|
{
|
||||||
const string SettingsFile = "Settings.ini";
|
const string SettingsFile = "Settings.ini";
|
||||||
|
static Game game;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
Log.Write("Launching...");
|
Log.Write("Launching...");
|
||||||
ScriptEngine scriptEngine;
|
ScriptEngine scriptEngine;
|
||||||
Game game;
|
|
||||||
|
|
||||||
//Re-create the settings file if it is missing
|
//Re-create the settings file if it is missing
|
||||||
if (!File.Exists(SettingsFile))
|
if (!File.Exists(SettingsFile))
|
||||||
|
@ -111,6 +112,7 @@ namespace MudGame
|
||||||
while (game.IsRunning)
|
while (game.IsRunning)
|
||||||
{
|
{
|
||||||
game.Update();
|
game.Update();
|
||||||
|
System.Threading.Thread.Sleep(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue