|
1 | | -using Pulsar4X.Orbital; |
| 1 | +using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Linq; |
3 | 4 | using Pulsar4X.Datablobs; |
4 | 5 | using Pulsar4X.Engine; |
5 | | -using Pulsar4X.Names; |
6 | | -using Pulsar4X.Orbits; |
7 | 6 | using Pulsar4X.Galaxy; |
8 | 7 | using Pulsar4X.Movement; |
| 8 | +using Pulsar4X.Names; |
| 9 | +using Pulsar4X.Orbital; |
| 10 | +using Pulsar4X.Orbits; |
9 | 11 |
|
10 | 12 | namespace Pulsar4X.JumpPoints |
11 | 13 | { |
@@ -102,6 +104,14 @@ public static void GenerateJumpPoints(StarSystemFactory ssf, StarSystem system, |
102 | 104 | { |
103 | 105 | int numJumpPoints = GetNumJPForSystem(system); |
104 | 106 |
|
| 107 | + // Cap JumpPoints to maxSystems - 1 to ensure we don't create more than can be linked |
| 108 | + // In a 2-system game, each system should have at most 1 JP |
| 109 | + int maxSystems = system.Game.Settings.MaxSystems; |
| 110 | + if (maxSystems > 1 && numJumpPoints > maxSystems - 1) |
| 111 | + { |
| 112 | + numJumpPoints = maxSystems - 1; |
| 113 | + } |
| 114 | + |
105 | 115 | while (numJumpPoints > 0) |
106 | 116 | { |
107 | 117 | numJumpPoints--; |
@@ -129,5 +139,71 @@ private static void LinkJumpPoints(Entity JP1, Entity JP2) |
129 | 139 | jp1TransitableDB.DestinationId = JP2.Id; |
130 | 140 | jp2TransitableDB.DestinationId = JP1.Id; |
131 | 141 | } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Links all unlinked JumpPoints across all systems in the game. |
| 145 | + /// JumpPoints are paired between different systems to create inter-system connections. |
| 146 | + /// </summary> |
| 147 | + public static void LinkAllJumpPoints(Game game) |
| 148 | + { |
| 149 | + // Collect all unlinked JumpPoints grouped by system |
| 150 | + var unlinkedBySystem = new Dictionary<string, List<Entity>>(); |
| 151 | + |
| 152 | + foreach (var system in game.Systems) |
| 153 | + { |
| 154 | + var jumpPoints = system.GetAllEntitiesWithDataBlob<JumpPointDB>() |
| 155 | + .Where(jp => jp.GetDataBlob<JumpPointDB>().DestinationId <= 0) |
| 156 | + .ToList(); |
| 157 | + |
| 158 | + if (jumpPoints.Count > 0) |
| 159 | + { |
| 160 | + unlinkedBySystem[system.ID] = jumpPoints; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // If we have fewer than 2 systems with JumpPoints, nothing to link |
| 165 | + if (unlinkedBySystem.Count < 2) |
| 166 | + return; |
| 167 | + |
| 168 | + var systemIds = unlinkedBySystem.Keys.ToList(); |
| 169 | + var random = new Random(game.Settings.MasterSeed); |
| 170 | + |
| 171 | + // Keep linking JumpPoints until we can't make any more pairs |
| 172 | + bool madeLink; |
| 173 | + do |
| 174 | + { |
| 175 | + madeLink = false; |
| 176 | + |
| 177 | + // Find two systems that both have unlinked JumpPoints |
| 178 | + var systemsWithJPs = systemIds.Where(id => unlinkedBySystem[id].Count > 0).ToList(); |
| 179 | + |
| 180 | + if (systemsWithJPs.Count < 2) |
| 181 | + break; |
| 182 | + |
| 183 | + // Pick two different systems randomly |
| 184 | + int idx1 = random.Next(systemsWithJPs.Count); |
| 185 | + string system1Id = systemsWithJPs[idx1]; |
| 186 | + systemsWithJPs.RemoveAt(idx1); |
| 187 | + |
| 188 | + int idx2 = random.Next(systemsWithJPs.Count); |
| 189 | + string system2Id = systemsWithJPs[idx2]; |
| 190 | + |
| 191 | + // Get one JumpPoint from each system |
| 192 | + var jp1List = unlinkedBySystem[system1Id]; |
| 193 | + var jp2List = unlinkedBySystem[system2Id]; |
| 194 | + |
| 195 | + var jp1 = jp1List[random.Next(jp1List.Count)]; |
| 196 | + var jp2 = jp2List[random.Next(jp2List.Count)]; |
| 197 | + |
| 198 | + // Link them |
| 199 | + LinkJumpPoints(jp1, jp2); |
| 200 | + |
| 201 | + // Remove from unlinked lists |
| 202 | + jp1List.Remove(jp1); |
| 203 | + jp2List.Remove(jp2); |
| 204 | + |
| 205 | + madeLink = true; |
| 206 | + } while (madeLink); |
| 207 | + } |
132 | 208 | } |
133 | 209 | } |
0 commit comments