/// <summary> /// Copy (initializing) constructor. /// </summary> /// <param name="existing"></param> public Mappable(Mappable existing) : base(existing) { if (existing == null) { return; } // Use a new object, no just a reference to the copy's NovaPoint Position = new NovaPoint(existing.Position); }
/// <summary> /// Fleet construction based on a ShipToken and some parameters from a star (this is /// the usual case for most fleets when a new ship is manufactured at a star). /// </summary> /// <param name="ship">The ShipToken being constructed.</param> /// <param name="star">The star constructing the ship.</param> public Fleet(ShipToken token, Star star, long newKey) { tokens.Add(token.Key, token); FuelAvailable = TotalFuelCapacity; Type = ItemType.Fleet; // Have one waypoint to reflect the fleet's current position and the // planet it is in orbit around. Waypoint w = new Waypoint(); w.Position = star.Position; w.Destination = star.Name; w.WarpFactor = 0; Waypoints.Add(w); // Inititialise the fleet elements that come from the star. Position = star.Position; InOrbit = star; Key = newKey; }
/// <summary> /// Load: initializing constructor to load a fleet from an XmlNode (save file). /// </summary> /// <param name="node">An XmlNode representing the fleet.</param> public Fleet(XmlNode node) : base(node) { // Read the node XmlNode mainNode = node.FirstChild; try { while (mainNode != null) { switch (mainNode.Name.ToLower()) { case "fleetid": Id = uint.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "cargo": Cargo = new Cargo(mainNode); break; case "inorbit": InOrbit = new Star(); InOrbit.Name = mainNode.FirstChild.Value; break; case "bearing": Bearing = double.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "cloaked": Cloaked = double.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "fuelavailable": FuelAvailable = double.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "targetdistance": TargetDistance = double.Parse(mainNode.FirstChild.Value, System.Globalization.CultureInfo.InvariantCulture); break; case "battleplan": BattlePlan = mainNode.FirstChild.Value; break; case "tokens": XmlNode subNode = mainNode.FirstChild; ShipToken token; while (subNode != null) { token = new ShipToken(subNode); tokens.Add(token.Key, token); subNode = subNode.NextSibling; } break; case "waypoint": Waypoint waypoint = new Waypoint(mainNode); Waypoints.Add(waypoint); break; default: break; } mainNode = mainNode.NextSibling; } } catch (Exception e) { Report.Error("Error loading fleet:" + Environment.NewLine + e.Message); throw e; } }
/// <summary> /// Move the fleet towards the waypoint at the top of the list. Fuel is consumed /// at the rate of the sum of each of the individual ships (i.e. available fuel /// is automatically "pooled" between the ships). /// </summary> /// <param name="availableTime">The portion of a year left for travel.</param> /// <param name="race">The race this fleet belongs to.</param> /// <returns>A TravelStatus indicating arrival or in-transit.</returns> public TravelStatus Move(ref double availableTime, Race race) { if (GetTravelStatus() == TravelStatus.Arrived) { return(TravelStatus.Arrived); } Waypoint target = Waypoints[0]; InOrbit = null; double legDistance = PointUtilities.Distance(Position, target.Position); int warpFactor = target.WarpFactor; int speed = warpFactor * warpFactor; double targetTime = legDistance / speed; double fuelConsumptionRate = FuelConsumption(warpFactor, race); double fuelTime = FuelAvailable / fuelConsumptionRate; double travelTime = targetTime; // Determine just how long we have available to travel towards the // waypoint target. This will be the smaller of target time (the ideal // case, we get there) available time (didn't get there but still can // move towards there next turn) and fuel time. TravelStatus arrived = TravelStatus.Arrived; if (travelTime > availableTime) { travelTime = availableTime; arrived = TravelStatus.InTransit; } if (travelTime >= fuelTime) { travelTime = fuelTime; arrived = TravelStatus.InTransit; } // If we have arrived then the new fleet position is the waypoint // target. Otherwise the position is determined by how far we got // in the time or fuel available. if (arrived == TravelStatus.Arrived) { Position = target.Position; target.WarpFactor = 0; } else { double travelled = speed * travelTime; Position = PointUtilities.MoveTo(Position, target.Position, travelled); } // Update the travel time left for this year and the total fuel we // now have available. availableTime -= travelTime; int fuelUsed = (int)(fuelConsumptionRate * travelTime); FuelAvailable -= fuelUsed; // Added check if fleet run out of full it's speed will be changed // to free warp speed. if (arrived == TravelStatus.InTransit && fuelConsumptionRate > this.FuelAvailable) { target.WarpFactor = this.FreeWarpSpeed; } return(arrived); }