private void tick() { Console.Out.WriteLine("TICK!"); List <Tank> myTanks = populateTanks(); Tank me = null; foreach (Tank tank in myTanks) { if (tank._index == _agentNumber) { me = tank; } } if (me == null) { throw new Exception("Where am I?? I can't find myself!!"); } Console.Out.WriteLine("Tank: " + _agentNumber + " Location: " + me._xPosition + ", " + me._yPosition); string constantString = TelnetConnection.Connection.SendMessage("constants", true); Constants constants = new Constants(constantString); string obstaclesString = TelnetConnection.Connection.SendMessage("obstacles", true); ObstacleCollection obstacles = new ObstacleCollection(obstaclesString); List <PotentialField> fields = new List <PotentialField>(); fields.Add(new RandomField()); fields.AddRange(obstacles.ObstacleFields); Vector myPosition = new Vector(me._xPosition, me._yPosition); if (me._hasFlag) { //get bases string baseString = TelnetConnection.Connection.SendMessage("bases", true); BaseCollection bases = new BaseCollection(baseString); fields.Add(bases.GetFieldForMyBase(myPosition, constants.MyColor)); //get my base //set up field for base } else { string flagsString = TelnetConnection.Connection.SendMessage("flags", true); FlagCollection flags = new FlagCollection(flagsString); fields.Add(flags.GetFieldForNearestFlag(myPosition, constants.MyColor)); } PotentialFieldsCollection fieldCollection = new PotentialFieldsCollection(fields); Vector delta = fieldCollection.GetCombinedVectorForPoint(myPosition); Vector moveHere = myPosition + delta; me.moveToPosition(myPosition, moveHere); if (!_fieldsPlotted) { GnuPlotGenerator.PlotMyFields("fields", fieldCollection, obstacles); _fieldsPlotted = true; } }
public static void PlotMyFields(string filename, PotentialFieldsCollection fields, ObstacleCollection obstacles) { StringBuilder sb = new StringBuilder(); sb.AppendLine("set xrange [-400.0: 400.0]"); sb.AppendLine("set yrange [-400.0: 400.0]"); sb.AppendLine("unset key"); sb.AppendLine("set size square"); if (obstacles != null) { sb.AppendLine("unset arrow"); foreach (Obstacle obstacle in obstacles.Obstacles) { foreach (var segment in obstacle.Lines) { sb.Append("set arrow from "); sb.Append(segment.V1.X); sb.Append(", "); sb.Append(segment.V1.Y); sb.Append(" to "); sb.Append(segment.V2.X); sb.Append(", "); sb.Append(segment.V2.Y); sb.Append(" nohead lt 3"); sb.AppendLine(); } } } sb.AppendLine("plot '-' with vectors head"); double increment = 800.0 / 30.0; for (double xVal = -400.0; xVal <= 400.0; xVal += increment) { for (double yVal = -400.0; yVal <= 400.0; yVal += increment) { Vector theVector = fields.GetCombinedVectorForPoint(new Vector(xVal, yVal)); sb.AppendLine(xVal + " " + yVal + " " + theVector.X + " " + theVector.Y); } } FileStream stream = new FileStream(filename + ".gpi", FileMode.Create); StreamWriter writer = new StreamWriter(stream); writer.Write(sb.ToString()); writer.Close(); }