DataPaths now have values for the games Root directory and Script directory.

DataPath class is now completed and includes a new SetExtension() method for setting game object file extensions.
Scripting support fully implemented.
StandardGame now contains a Initialize() method for compiling scripts and searching for sub-classes of StandardGame
Server app will now use a Scripted game class instead of the default StandardGame if one is present.
StandardGame.Start() is now virtual so child classes can override it.
Sample Game script created to show how to create a custom game script, including how to setup the game and create Rooms pragamatically.
ScriptFactory has a new method for searching all scripts and scripts that inherit from a specified class.
Renamed all of the Command scripts.  They no longer start with 'Command'.  Example: "CommandSay" has now become "Say".  There is no need to preceed the command name with the word "Command" anymore.
This commit is contained in:
Scionwest_cp 2012-03-05 20:30:54 -08:00
parent c40d32e7ae
commit 8639403255
14 changed files with 323 additions and 69 deletions

View file

@ -9,10 +9,12 @@ namespace MudEngine.DAL
{ {
public enum DataTypes public enum DataTypes
{ {
Root,
Players, Players,
Environments, Environments,
Characters, Characters,
Equipment Equipment,
Scripts,
} }
/// <summary> /// <summary>
@ -26,7 +28,17 @@ namespace MudEngine.DAL
String assemblyFile = Path.GetFileName(path); String assemblyFile = Path.GetFileName(path);
this._InstallRoot = path.Substring(0, path.Length - assemblyFile.Length); this._InstallRoot = path.Substring(0, path.Length - assemblyFile.Length);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Characters"), DataTypes.Characters);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Environments"), DataTypes.Environments);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Equipment"), DataTypes.Equipment);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Players"), DataTypes.Players); this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Players"), DataTypes.Players);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "GameScripts"), DataTypes.Scripts);
this.SetExtension(DataTypes.Characters, ".character");
this.SetExtension(DataTypes.Environments, ".environment");
this.SetExtension(DataTypes.Equipment, ".equipment");
this.SetExtension(DataTypes.Players, ".player");
this.SetExtension(DataTypes.Scripts, ".cs");
} }
public void SetAbsolutePath(String path, DataTypes objectType) public void SetAbsolutePath(String path, DataTypes objectType)
@ -36,9 +48,21 @@ namespace MudEngine.DAL
switch (objectType) switch (objectType)
{ {
case DataTypes.Characters:
this._Characters = path;
break;
case DataTypes.Environments:
this._Environments = path;
break;
case DataTypes.Equipment:
this._Equipment = path;
break;
case DataTypes.Players: case DataTypes.Players:
this._Players = path; this._Players = path;
break; break;
case DataTypes.Scripts:
this._Scripts = path;
break;
} }
} }
@ -48,9 +72,22 @@ namespace MudEngine.DAL
public String GetPath(DataTypes objectType) public String GetPath(DataTypes objectType)
{ {
if (objectType == DataTypes.Players) switch (objectType)
{
case DataTypes.Root:
return this._InstallRoot;
case DataTypes.Characters:
return this._Characters;
case DataTypes.Environments:
return this._Environments;
case DataTypes.Equipment:
return this._Equipment;
case DataTypes.Players:
return this._Players; return this._Players;
else case DataTypes.Scripts:
return this._Scripts;
}
return String.Empty; return String.Empty;
} }
@ -60,9 +97,24 @@ namespace MudEngine.DAL
switch (objectType) switch (objectType)
{ {
case DataTypes.Root:
result = Path.Combine(this._InstallRoot, filename + this.GetExtension(DataTypes.Root));
break;
case DataTypes.Characters:
result = Path.Combine(this._Characters, filename + this.GetExtension(DataTypes.Characters));
break;
case DataTypes.Environments:
result = Path.Combine(this._Environments, filename + this.GetExtension(DataTypes.Environments));
break;
case DataTypes.Equipment:
result = Path.Combine(this._Equipment, filename + this.GetExtension(DataTypes.Equipment));
break;
case DataTypes.Players: case DataTypes.Players:
result = Path.Combine(this._Players, filename + this.GetExtension(DataTypes.Players)); result = Path.Combine(this._Players, filename + this.GetExtension(DataTypes.Players));
break; break;
case DataTypes.Scripts:
result = Path.Combine(this._Scripts, filename + this.GetExtension(DataTypes.Scripts));
break;
} }
return result; return result;
@ -74,18 +126,64 @@ namespace MudEngine.DAL
switch (objectType) switch (objectType)
{ {
case DataTypes.Root:
result = ".dat";
break;
case DataTypes.Characters:
result = this._CharacterExt;
break;
case DataTypes.Environments:
result = this._EnvironmentExt;
break;
case DataTypes.Equipment:
result = this._EquipmentExt;
break;
case DataTypes.Scripts:
result = this._ScriptExt;
break;
case DataTypes.Players: case DataTypes.Players:
result = ".player"; result = this._PlayersExt;
break; break;
} }
return result; return result;
} }
public void SetExtension(DataTypes objectType, String extension)
{
if (!extension.StartsWith("."))
extension = extension.Insert(0, ".");
switch (objectType)
{
case DataTypes.Characters:
this._CharacterExt = extension;
break;
case DataTypes.Environments:
this._EnvironmentExt = extension;
break;
case DataTypes.Equipment:
this._EquipmentExt = extension;
break;
case DataTypes.Players:
this._PlayersExt = extension;
break;
case DataTypes.Scripts:
this._ScriptExt = extension;
break;
}
}
private String _InstallRoot; private String _InstallRoot;
private String _Players; private String _Players;
private String _PlayersExt;
private String _Environments; private String _Environments;
private String _EnvironmentExt;
private String _Characters; private String _Characters;
private String _CharacterExt;
private String _Equipment; private String _Equipment;
private String _EquipmentExt;
private String _Scripts;
private String _ScriptExt;
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Reflection;
using System.IO; using System.IO;
using MudEngine.Networking; using MudEngine.Networking;
@ -10,6 +11,7 @@ using MudEngine.Core;
using MudEngine.Game.Characters; using MudEngine.Game.Characters;
using MudEngine.DAL; using MudEngine.DAL;
using MudEngine.Game.Environment; using MudEngine.Game.Environment;
using MudEngine.Scripting;
namespace MudEngine.Game namespace MudEngine.Game
{ {
@ -64,7 +66,7 @@ namespace MudEngine.Game
/// <summary> /// <summary>
/// Gets if the game is currently running or not. /// Gets if the game is currently running or not.
/// </summary> /// </summary>
public Boolean Enabled { get; private set; } public Boolean Enabled { get; protected set; }
/// <summary> /// <summary>
/// Gets or Sets if the game is in debug more or not. /// Gets or Sets if the game is in debug more or not.
@ -86,6 +88,8 @@ namespace MudEngine.Game
/// </summary> /// </summary>
public DataPaths SavePaths { get; set; } public DataPaths SavePaths { get; set; }
public ScriptFactory ScriptFactory { get; set; }
/// <summary> /// <summary>
/// StandardGame constructor. If no Port number is provided, 4000 is used. /// StandardGame constructor. If no Port number is provided, 4000 is used.
/// </summary> /// </summary>
@ -101,7 +105,7 @@ namespace MudEngine.Game
/// <param name="port"></param> /// <param name="port"></param>
public StandardGame(String name, Int32 port) public StandardGame(String name, Int32 port)
{ {
Logger.WriteLine("Initializing Standard Mud Game"); Logger.WriteLine("Initializing Mud Game");
this.Name = name; this.Name = name;
this.Website = "http://scionwest.net"; this.Website = "http://scionwest.net";
this.Description = "A sample Mud game created using the Mud Designer kit."; this.Description = "A sample Mud game created using the Mud Designer kit.";
@ -123,6 +127,27 @@ namespace MudEngine.Game
this.World = new World(this); this.World = new World(this);
} }
public StandardGame Initialize()
{
CompileEngine compiler = new CompileEngine();
compiler.AddAssemblyReference(Path.Combine(this.SavePaths.GetPath(DataTypes.Root), Assembly.GetExecutingAssembly().Location));
Boolean result = compiler.Compile(this.SavePaths.GetPath(DataTypes.Scripts));
if (result)
{
ScriptFactory factory = new ScriptFactory(compiler.CompiledAssembly);
StandardGame game = (StandardGame)factory.FindInheritedScripted("StandardGame", "Mud Game");
if (game == null)
return null;
else
return game;
}
return null;
}
/// <summary> /// <summary>
/// Starts the game by getting all of the game scripts, loading the world /// Starts the game by getting all of the game scripts, loading the world
/// loading all of the games commands and starting the server. /// loading all of the games commands and starting the server.
@ -130,19 +155,36 @@ namespace MudEngine.Game
/// <param name="maxPlayers"></param> /// <param name="maxPlayers"></param>
/// <param name="maxQueueSize"></param> /// <param name="maxQueueSize"></param>
/// <returns></returns> /// <returns></returns>
public Boolean Start(Int32 maxPlayers, Int32 maxQueueSize) public virtual Boolean Start(Int32 maxPlayers, Int32 maxQueueSize)
{ {
Logger.WriteLine("Starting up Standard Game"); Logger.WriteLine("Starting up " + this.Name);
//Instance Script Engine //Instance Script Engine
CompileEngine compiler = new CompileEngine("cs");
//compiler.AddAssemblyReference(Assembly.GetExecutingAssembly().FullName);
compiler.AddAssemblyReference(Path.Combine(this.SavePaths.GetPath(DataTypes.Root), Assembly.GetExecutingAssembly().Location));
//Compile any scripts //Compile any scripts
Boolean result = compiler.Compile(this.SavePaths.GetPath(DataTypes.Scripts));
if (!result)
{
Logger.WriteLine(compiler.Errors, Logger.Importance.Error);
//Load our Commands //Fail safe in the event compiling fails. Just use this assembly.
this.ScriptFactory = new ScriptFactory(Assembly.GetExecutingAssembly());
}
else
{
this.ScriptFactory = new ScriptFactory(compiler.CompiledAssembly);
}
//Load the default engine Commands
CommandSystem.LoadCommands(); CommandSystem.LoadCommands();
CommandSystem.LoadCommandLibrary(this.ScriptFactory.Assembly, false);
//Load World //Load World
this.World.Initialize(); this.World.Initialize();
this.World.Load();
//Start our server. //Start our server.
this.Server.Start(maxPlayers, maxQueueSize); this.Server.Start(maxPlayers, maxQueueSize);
@ -180,8 +222,16 @@ namespace MudEngine.Game
private void SetupPaths() private void SetupPaths()
{ {
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Characters)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Characters));
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Environments)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Environments));
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Equipment)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Equipment));
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Players))) if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Players)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Players)); Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Players));
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Scripts)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Scripts));
} }
} }
} }

View file

@ -39,6 +39,14 @@ namespace MudEngine.Game
this.StartLocation = zone.GetRoom("Bedroom"); this.StartLocation = zone.GetRoom("Bedroom");
} }
public void Save()
{
}
public void Load()
{
}
public void Destroy() public void Destroy()
{ {
throw new NotImplementedException(); throw new NotImplementedException();

View file

@ -14,7 +14,7 @@ using MudEngine.GameScripts;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandCreatePlayer : ICommand public class CreatePlayer : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -23,7 +23,7 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandCreatePlayer() public CreatePlayer()
{ {
Help = new List<string>(); Help = new List<string>();
Name = "CreatePlayer"; Name = "CreatePlayer";

View file

@ -13,7 +13,7 @@ using MudEngine.GameScripts;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandLogin : ICommand public class Login : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -22,10 +22,10 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandLogin() public Login()
{ {
Help = new List<string>(); Help = new List<string>();
Name = "Login"; //Name = "Login";
Description = "Account login command."; Description = "Account login command.";
} }

View file

@ -10,7 +10,7 @@ using MudEngine.Core.Interfaces;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandLook : ICommand public class Look : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -18,7 +18,7 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandLook() public Look()
{ {
this.Name = "Look"; this.Name = "Look";
} }

View file

@ -10,7 +10,7 @@ using MudEngine.Networking;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandSay : ICommand public class Say : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -18,7 +18,7 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandSay() public Say()
{ {
this.Name = "Say"; this.Name = "Say";
this.Description = "Chat command that allows objects to communicate."; this.Description = "Chat command that allows objects to communicate.";

View file

@ -10,7 +10,7 @@ using MudEngine.Networking;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandSetRole : ICommand public class SetRole : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -18,7 +18,7 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandSetRole() public SetRole()
{ {
this.Name = "SetRole"; this.Name = "SetRole";
this.Description = "Chat command that allows objects to communicate."; this.Description = "Chat command that allows objects to communicate.";
@ -50,7 +50,7 @@ namespace MudEngine.GameScripts.Commands
{ {
StandardCharacter target = game.Server.ConnectionManager.GetConnectedCharacter(names[0].ToLower()); StandardCharacter target = game.Server.ConnectionManager.GetConnectedCharacter(names[0].ToLower());
this.SetRole(character, target); this.ApplyRole(character, target);
} }
else else
{ {
@ -61,7 +61,7 @@ namespace MudEngine.GameScripts.Commands
return false; return false;
} }
public void SetRole(StandardCharacter admin, StandardCharacter target) public void ApplyRole(StandardCharacter admin, StandardCharacter target)
{ {
admin.SendMessage("Please choose from one of the available Roles:"); admin.SendMessage("Please choose from one of the available Roles:");

View file

@ -11,7 +11,7 @@ using MudEngine.Networking;
namespace MudEngine.GameScripts.Commands namespace MudEngine.GameScripts.Commands
{ {
public class CommandStop : ICommand public class Stop : ICommand
{ {
public string Name { get; set; } public string Name { get; set; }
@ -19,7 +19,7 @@ namespace MudEngine.GameScripts.Commands
public List<string> Help { get; set; } public List<string> Help { get; set; }
public CommandStop() public Stop()
{ {
this.Name = "Stop"; this.Name = "Stop";
this.Description = "Chat command that allows objects to communicate."; this.Description = "Chat command that allows objects to communicate.";

View file

@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MudEngine.Game;
using MudEngine.DAL;
using MudEngine.Game.Environment;
using MudEngine.Game;
namespace MudEngine.GameScripts
{
public class SampleGame : StandardGame
{
public SampleGame(String name)
: base(name)
{
this.Name = "Sample Mud Game";
this.Debugging = true;
this.Description = "A sample MUD game created using the Mud Designer engine.";
this.Website = "http://muddesigner.codeplex.com";
}
public override Boolean Start(Int32 maxPlayers, Int32 maxQueueSize)
{
this.Server.ServerOwner = "Admin";
//Quick demonstration on how to create the initial starting room for new players.
this.World.CreateRealm("Azeroth", "Starting Realm for beginning players");
Zone z = this.World.GetRealm("Azeroth").CreateZone("Bedlam", "Initial Zone for new players.");
Room bedroom = z.CreateRoom("Bedroom", "This is your bedroom.");
Room hallway = z.CreateRoom("Hallway", "This is the hallway.");
//Save if the result of the Linkage.
Boolean linked = bedroom.LinkRooms(AvailableTravelDirections.West, hallway);
//Call our parent Start() method which will start the world and server
//along with compile all of our commands and scripts.
Boolean startOK = base.Start(maxPlayers, maxQueueSize);
//If the parent started ok and our rooms were linked together
//Set the starting location as our new room
if (startOK && linked)
{
this.World.StartLocation = bedroom;
return true;
}
//Otherwise return false and prevent the game from running.
else
{
this.Enabled = false;
return false;
}
}
}
}

View file

@ -11,6 +11,8 @@ namespace MudEngine.Scripting
{ {
public class ScriptFactory public class ScriptFactory
{ {
public Assembly Assembly { get; private set; }
//The assembly loaded that will be used. //The assembly loaded that will be used.
private List<Assembly> _AssemblyCollection; private List<Assembly> _AssemblyCollection;
@ -51,6 +53,8 @@ namespace MudEngine.Scripting
_AssemblyCollection = new List<Assembly>(); _AssemblyCollection = new List<Assembly>();
//Add the supplied assembly to our AssemblyCollection //Add the supplied assembly to our AssemblyCollection
_AssemblyCollection.Add(assembly); _AssemblyCollection.Add(assembly);
this.Assembly = assembly;
} }
#endif #endif
/// <summary> /// <summary>
@ -96,7 +100,6 @@ namespace MudEngine.Scripting
try try
{ {
#if WINDOWS_PC
foreach (Assembly a in _AssemblyCollection) foreach (Assembly a in _AssemblyCollection)
{ {
//The assembly can be null if accessing after a failed compilation. //The assembly can be null if accessing after a failed compilation.
@ -116,17 +119,6 @@ namespace MudEngine.Scripting
if (foundScript) if (foundScript)
break; break;
} }
#elif WINDOWS_PHONE
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes())
{
if (t.Name == scriptName)
{
script = t;
foundScript = true;
break;
}
}
#endif
} }
catch catch
{ {
@ -144,5 +136,52 @@ namespace MudEngine.Scripting
return new BaseScript(game, "New Object", String.Empty); return new BaseScript(game, "New Object", String.Empty);
} }
} }
public Object FindInheritedScripted(String baseScript, params Object[] arguments)
{
Type script = typeof(BaseScript);
Boolean foundScript = false;
if (this._AssemblyCollection.Count == 0)
return null;
try
{
foreach (Assembly a in _AssemblyCollection)
{
if (a == null)
continue;
foreach (Type t in a.GetTypes())
{
if (t.BaseType.Name == baseScript)
{
script = t;
foundScript = true;
break;
}
}
if (foundScript)
break;
}
}
catch (Exception ex)
{
Logger.WriteLine(ex.Message);
}
try
{
Object obj = Activator.CreateInstance(script, arguments);
return obj;
}
catch
{
return null;
}
}
} }
} }

View file

@ -55,12 +55,27 @@
<Compile Include="Core\ObjectCollection.cs" /> <Compile Include="Core\ObjectCollection.cs" />
<Compile Include="DAL\DataPaths.cs" /> <Compile Include="DAL\DataPaths.cs" />
<Compile Include="DAL\XMLData.cs" /> <Compile Include="DAL\XMLData.cs" />
<Compile Include="GameScripts\Commands\CommandLook.cs" /> <None Include="GameScripts\Commands\Look.cs">
<Compile Include="GameScripts\Commands\CommandLogin.cs" /> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Compile Include="GameScripts\Commands\CommandSay.cs" /> </None>
<Compile Include="GameScripts\Commands\CommandStop.cs" /> <None Include="GameScripts\Commands\Login.cs">
<Compile Include="GameScripts\Commands\CommandCreatePlayer.cs" /> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Compile Include="GameScripts\Commands\CommandSetRole.cs" /> </None>
<None Include="GameScripts\Commands\Say.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="GameScripts\Commands\Stop.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="GameScripts\Commands\CreatePlayer.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="GameScripts\Commands\SetRole.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="GameScripts\SampleGame.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Include="Game\Characters\CharacterRoles.cs" /> <Compile Include="Game\Characters\CharacterRoles.cs" />
<Compile Include="Game\Characters\CharacterStats.cs" /> <Compile Include="Game\Characters\CharacterStats.cs" />
<Compile Include="Game\Characters\MyCharacter.cs" /> <Compile Include="Game\Characters\MyCharacter.cs" />

View file

@ -24,29 +24,17 @@ namespace WinPC_Server
Logger.ConsoleOutPut = true; Logger.ConsoleOutPut = true;
//Instance and setup our game //Instance and setup our game
StandardGame game = new StandardGame("Sample Game"); StandardGame game = new StandardGame("Sample Mud Game");
game.AutoSave = true; StandardGame g = game.Initialize();
game.Debugging = true;
game.Description = "This is a very simple game that was created to demonstrate MUD game creation with the Mud Designer Game Engine."; //Check if 'g' is null. If it's not, then we have a custom game
game.HiddenRoomNames = false; //that was found and we will use that. Otherwise use the default.
game.Multiplayer = true; if (g != null)
game.Server.MOTD = "Welcome to the Sample Game demonstration server! This is the Servers MOTD!"; game = g;
game.Version = "1.0";
game.Website = "http://muddesigner.codeplex.com";
game.Server.ServerOwner = "Akiyuki";
//Start the game and server. //Start the game and server.
game.Start(100, 20); game.Start(100, 20);
//Setup our Server console input class
/*
ConsoleInput input = new ConsoleInput();
//Run the console input on its own thread.
Thread inputThread = new Thread(input.GetInput);
inputThread.Start();
*/
//Game loops until it is disabled. //Game loops until it is disabled.
while (game.Enabled) while (game.Enabled)
{ {