- Added FileManager.GetDataSpan() method. This allows developers to start at a line and collect the next 'x' number of lines after that starting line. Example: GetDataSpan("MyFile.txt", 5, "DoorwayArrivalRoom", true); This seeks MyFile.txt until it finds "DoorwayArrivalRoom" and then it stores that line and the next 4 lines for a total of 5 lines and returns the collection. The last argument 'true' means that the method will scan the rest of the file after the initial 5 lines and add any other lines that match the "DoorwayArrivalRoom" string as well. - Deleted CommandResults class as it's no longer used by the script engine. - Singleplayer and Multiplayer save data paths are now the same paths by default. - Game.Update() method now checks for auto-saving (instead of MudGame performing this check) - Saving and restoring of Realms, Zones, Rooms and Doorways now fully implemented. - GameTime now supports auto-saving - GameWorld.Update() now calls BaseCharacter.Update() and is ready for future update code. - GameWorld.AddObject and GameWorld.RemoveObject() have been removed. - GameWorld.AddRealm() re-added for adding Realms pre-populated with zones/rooms. Note that Zones and Rooms can be added to a Realm even after it has been added to the GameWorld.RealmCollection - BaseObject now contains a OnStart() event method. - BaseObject.Save() now saves BaseObject.DetailedDescription collection content. - Updated BaseCharacter to retrieve Environments by Filename rather than Object name. - BaseStats.Experience property added. - Door.RoomTravelType enum added for determining if the room is Arrival or Departure - Door.SetRoom() method added for restoring a Rooms Doorway link during world restoration. - Renamed Room.InstallPath to Room.RoomLocation. Contains a MyRealm.Realm>MyZone.Zone>MyRoom.Room path - Added Room.RoomLocationWithoutExtension property for returning the Rooms location without file extensions. Ex: MyRealm>MyZone>MyRoom - Room now saves Doorways. - The GameWorld now restores the link between Rooms once all Environment objects have been instanced and restored from their saved state. MudGame: - Minor clean-up with MudGame loop and shutdown. - Updated scripts to reflect changes made to the engine.
200 lines
6.5 KiB
C#
200 lines
6.5 KiB
C#
//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
|
|
{
|
|
/// <summary>
|
|
/// Gets the collection of Objects that do not belong to any other Type of categories.
|
|
/// </summary>
|
|
public List<BaseObject> Objects { get; private set; }
|
|
/// <summary>
|
|
/// Gets the collection of Items that are available in the game world.
|
|
/// </summary>
|
|
public List<BaseItem> Items { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the collection of Characters (NPC/Monster) that are available in the Game world
|
|
/// </summary>
|
|
//TODO: This should be BaseAI
|
|
public List<BaseCharacter> Characters { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the collection of Realms currently available in the game world
|
|
/// </summary>
|
|
public List<Realm> RealmCollection { get; private set; }
|
|
|
|
private Game _Game;
|
|
|
|
public GameWorld(Game game)
|
|
{
|
|
_Game = game;
|
|
|
|
Objects = new List<BaseObject>();
|
|
Items = new List<BaseItem>();
|
|
Characters = new List<BaseCharacter>();
|
|
RealmCollection = new List<Realm>();
|
|
}
|
|
|
|
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.InitialZone.InitialRoom.RoomLocationWithoutExtension);
|
|
|
|
}
|
|
|
|
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";
|
|
|
|
//Get the Initial Realm information from saved file.
|
|
String realmFile = FileManager.GetData(filename, "InitialRealm");
|
|
String realmFolder = Path.GetFileNameWithoutExtension(realmFile);
|
|
String realmPath = Path.Combine(_Game.DataPaths.Environment, realmFolder, realmFile);
|
|
|
|
Log.Write("Restoring Game World...");
|
|
|
|
//Loop through each RealmCollection data entry stored in the _Game saved file.
|
|
foreach (String realm in FileManager.GetCollectionData(filename, "RealmCollection"))
|
|
{
|
|
Log.Write("Restoring Realm " + realm);
|
|
|
|
Realm r = new Realm(_Game);
|
|
|
|
//Restore the Realm objects properties from file.
|
|
r.Load(Path.Combine(_Game.DataPaths.Environment, Path.GetFileNameWithoutExtension(realm), realm));
|
|
|
|
//Loop through each of the Realm objects instanced during startup and find one matching the loaded filename
|
|
for (int x = 0; x != RealmCollection.Count; x++)
|
|
{
|
|
//If the filenames match, then overwrite the pre-loaded Realm with the restored Realm with the saved data.
|
|
if (RealmCollection[x].Filename == r.Filename)
|
|
{
|
|
RealmCollection[x] = r;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
foreach (Realm r in RealmCollection)
|
|
{
|
|
if (r.IsInitialRealm)
|
|
{
|
|
_Game.InitialRealm = r;
|
|
}
|
|
|
|
foreach (Zone z in r.ZoneCollection)
|
|
{
|
|
Log.Write("Restoring Zone: " + z.Filename);
|
|
z.RestoreLinkedRooms();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a Realm to the Games current list of Realms.
|
|
/// </summary>
|
|
/// <param name="realm"></param>
|
|
public 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a reference to the Realm contained within the Realms collection if it exists.
|
|
/// </summary>
|
|
/// <param name="filename"></param>
|
|
/// <returns></returns>
|
|
public Realm GetRealm(String filename)
|
|
{
|
|
foreach (Realm r in RealmCollection)
|
|
{
|
|
if (r.Filename == filename)
|
|
return r;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public virtual void Update()
|
|
{
|
|
foreach (BaseCharacter character in Characters)
|
|
{
|
|
character.Update();
|
|
}
|
|
}
|
|
}
|
|
}
|