/*------------------------------------------------------ * * RidR utility classes * * */ using System; using System.Collections.Generic; using System.IO; namespace RidR { /// /// Represents a basic info container for robots. /// public struct RobotInfo { /// /// Gets or sets the robots current orientation. /// public Orientation Orientation { get; set; } /// /// Gets or sets the robots current position. /// public Position Position { get; set; } /// /// Gets or sets the current team role. /// public TeamRole TeamRole { get; set; } /// /// Gets or sets the current team. /// public int Team { get; set; } /// /// Gets the botcode for this robot. /// public string BotCode { get { return "" + Team + (int)TeamRole; } } /// /// Constructs a new RobotInfo instance. /// /// The team ID of this bot /// The team role of this bot /// The position of this bot /// The orientation of this bot public RobotInfo(int teamID, TeamRole role, Position p, Orientation o) : this() { Team = teamID; TeamRole = role; Position = p; Orientation = o; } internal RobotInfo(string botCode, Position p, Orientation o) : this() { Orientation = o; Position = p; Team = int.Parse("" + botCode[0]); TeamRole = (TeamRole)int.Parse("" + botCode[1]); } } /// /// Represents a laser (sub)range. /// public struct LaserRange { /// /// The ID of the team firing the laser. /// public int TeamId { get; set; } /// /// Starting position of this range. /// public Position StartPoint { get; set; } /// /// End position of this range. /// public Position EndPoint { get; set; } /// /// Amount of time this laser has been active in milliseconds. /// public int ActiveTime { get; set; } /// /// Converts this LaserRange to a human-readable string /// public override string ToString() { return TeamId + " " + StartPoint.X + " " + EndPoint.X + " " + StartPoint.Y + " " + EndPoint.Y + " " + ActiveTime; } } /// /// Represents a crystal acquire event. /// public struct ScoreEvent { /// /// Position of the Crystal being acquired /// public Position Position { get; set; } /// /// ID of the team acquiring the crystal. /// public int TeamId { get; set; } /// /// Converts this ScoreEvent to a human-readable string /// public override string ToString() { return TeamId + " " + Position.ToString(); } } /// /// Represents a laser hit event. /// public struct HitEvent { /// /// The bot code of the robot that got hit. /// public int BotCode { get; set; } /// /// The time at which the bot got hit in milliseconds. /// public int HitTime { get; set; } /// /// Amount of time the bot remains stunned in milliseconds. /// public int RemainingStun { get; set; } } /// /// Represents a set of 3 starting positions /// public struct StartPositionSet { /// /// The ID of this starting position /// public int PositionID { get; set; } /// /// The first position of this set /// public Position P1 { get; set; } /// /// The second position of this set /// public Position P2 { get; set; } /// /// The third position of this set /// public Position P3 { get; set; } } /// /// Provides some utility methods. /// public static class Utility { /// /// Reads a settings file for a RidR server. /// /// Path to the settings file /// The port number /// The maximum amount of time in seconds one round may take /// The radius in which bots can see /// The amount of time in milliseconds bots need to charge their laser 10 points /// The amount of time in milliseconds a bot is stunned when hit by a laser /// The amount of time in milliseconds a laser persists from the time it is fired /// The path to the map file /// The amount of gameThreads public static void ReadSettingsFile(string settingsPath, out int portNumber, out int roundTime, out int sightRadius, out int chargeTime, out int laserStun, out int laserPersist, out string mapPath, out int gameThreads) { FileStream settingsFS = new FileStream(settingsPath, FileMode.Open, FileAccess.Read); StreamReader settingsReader = new StreamReader(settingsFS); portNumber = int.Parse(settingsReader.ReadLine()); roundTime = int.Parse(settingsReader.ReadLine()); sightRadius = int.Parse(settingsReader.ReadLine()); chargeTime = int.Parse(settingsReader.ReadLine()); laserStun = int.Parse(settingsReader.ReadLine()); laserPersist = int.Parse(settingsReader.ReadLine()); mapPath = settingsReader.ReadLine(); gameThreads = int.Parse(settingsReader.ReadLine()); settingsReader.Close(); } /// /// Writes a settings file for a RidR server. /// /// Path to the settings file /// The port number /// The maximum amount of time in seconds one round may take /// The radius in which bots can see /// The amount of time in milliseconds bots need to charge their laser 10 points /// The amount of time in milliseconds a bot is stunned when hit by a laser /// The amount of time in milliseconds a laser persists from the time it is fired /// The path to the map file /// The amount of gameThreads public static void WriteSettingsFile(string settingsPath, int portNumber, int roundTime, int sightRadius, int chargeTime, int laserStun, int laserPersist, string mapPath, int gameThreads) { FileStream settingsFS = new FileStream(settingsPath, FileMode.Create, FileAccess.Write); StreamWriter settingsWriter = new StreamWriter(settingsFS); settingsWriter.WriteLine(portNumber); settingsWriter.WriteLine(roundTime); settingsWriter.WriteLine(sightRadius); settingsWriter.WriteLine(chargeTime); settingsWriter.WriteLine(laserStun); settingsWriter.WriteLine(laserPersist); settingsWriter.WriteLine(mapPath); settingsWriter.WriteLine(gameThreads); settingsWriter.Flush(); settingsWriter.Close(); } /// /// Reads a map file for a RidR game. /// /// Path to the map file /// The map /// List of starting positions on this map public static void ReadMapFile(string mapPath, out TileType[,] map, out IList startPositions) { FileStream mapFS = new FileStream(mapPath, FileMode.Open, FileAccess.Read); StreamReader mapReader = new StreamReader(mapFS); string[] header = mapReader.ReadLine().Split(); int width = int.Parse(header[0]); int height = int.Parse(header[1]); int msgCount = int.Parse(header[2]); map = new TileType[width, height]; for (int i = 0; i < msgCount; ++i) { string[] split = mapReader.ReadLine().Split(); TileType tile; switch (split[0]) { case "R": tile = TileType.Rift; break; case "W": tile = TileType.Wall; break; case "C": tile = TileType.Crystal; break; default: tile = TileType.Floor; break; } int x = int.Parse(split[1]); int y = int.Parse(split[2]); map[x, y] = tile; } int posCount = int.Parse(mapReader.ReadLine()); startPositions = new StartPositionSet[posCount]; for (int i = 0; i < posCount * 3; ++i) { string[] split; split = mapReader.ReadLine().Split(); int index = int.Parse(split[0]) - 1; Position p = new Position(int.Parse(split[1]), int.Parse(split[2])); StartPositionSet set = startPositions[index]; set.PositionID = index + 1; if (set.P1.IsEmpty) { set.P1 = p; } else if (set.P2.IsEmpty) { set.P2 = p; } else { set.P3 = p; } startPositions[index] = set; } mapReader.Close(); } } /// /// Provides debug functionality. /// public static class Debug { /// /// Writes a line to the console, but only if this assembly is built in debug mode. /// /// public static void WriteLine(string line) { #if DEBUG Console.WriteLine(line); #endif } /// /// Asserts the condition, but only if this assembly is built in debug mode. /// /// The condition to check public static void Assert(bool condition) { #if DEBUG System.Diagnostics.Debug.Assert(condition); #endif } /// /// Asserts the condition, but only if this assembly is built in debug mode. /// /// The condition to check /// The message to show the user if the assert fails public static void Assert(bool condition, string message) { #if DEBUG System.Diagnostics.Debug.Assert(condition, message); #endif } } }