//# Generates a string representation of a planet. This is used to send data //# about the planets to the client programs. public static string serialize_planet(Planet p, int pov) { int owner = switch_pov(p.owner, pov); string message = "P " + p.x + " " + p.y + " " + owner + " " + p.num_ships + " " + p.growth_rate; return message.Replace(".0 ", " "); }
//# Calculates the travel time between two planets. This is the cartesian //# distance, rounded up to the nearest integer. public static int travel_time(Planet aP, Planet bP) { double dx = bP.x - aP.x; double dy = bP.y - aP.y; return (int)Math.Ceiling(Math.Sqrt(dx * dx + dy * dy)); }
//# Resolves the battle at planet p, if there is one. //# * removes all fleets involved in the battle //# * sets the number of ships and owner of the planet according the outcome public static void fight_battle(int pid, ref Planet p, ref List<Fleet> fleets) { List<int> participants = new List<int>(); participants = addToArray(p.owner, p.num_ships, participants); for (int i = fleets.Count - 1; i > -1; i--) { Fleet f = fleets[i]; int owner = f.owner; if (f.turns_remaining <= 0 && f.destination == pid) { participants = addToArray(f.owner, f.num_ships, participants); fleets[i] = null; } } cleanUpFleets(ref fleets); Fleet winner = new Fleet(); Fleet second = new Fleet(); for (int iOwner = 0; iOwner < participants.Count; iOwner++) { int ships = participants[iOwner]; if (ships >= second.num_ships) { if (ships >= winner.num_ships) { second.owner = winner.owner; second.num_ships = winner.num_ships; winner.owner = iOwner; winner.num_ships = ships; } else { second.owner = iOwner; second.num_ships = ships; } } } if (winner.num_ships > second.num_ships) { p.num_ships = winner.num_ships - second.num_ships; p.owner = winner.owner; } else p.num_ships = 0; }