/// <summary> /// Creates and registers a colony and all associated structures. Links colony to planet appropriately. /// </summary> /// <param name="xPos"></param> /// <param name="yPos"></param> /// <param name="owner"></param> /// <param name="area"></param> /// <param name="ls"></param> /// <returns></returns> public static Colony CreateColony(float xPos, float yPos, Player owner, Planet planet, LocatorService ls) { Colony c = new Colony(_localIDManager.PopFreeID(), owner, planet, ls); c.Name = "Colony " + planet.AreaName; c.AddStructure(StructureFactory.CreateCommandCenter(xPos, yPos, owner, planet.Id)); Biodome b = StructureFactory.CreateBiodome(-9999999, -9999999, owner, planet.Id); c.AddStructure(b); b.Stats = new SmallBiodomeStats(); b.Population = 10; _galaxyRegistrationManager.RegisterObject(c); planet.SetColony(c); planet.GetOwner().ColonizedPlanetIDs.Add(planet.Id); planet.IsColonized = true; planet.AddStructure(c.CommandCenter); planet.AddStructure(b); return(c); }
public void AddModulesToShip(IShip ship, int numMods, CargoSynchronizer cargoSynchronizer, LocalIDManager galaxyIDManager) { for (int i = 0; i < numMods; i++) { Module m = null; int moduleID = galaxyIDManager.PopFreeID(); byte level = (byte)Rand.Random.Next(1, 4); int modnum = Rand.Random.Next(1, 11); { switch (modnum) { case 1: m = new EnergyRegenModule(moduleID, level); break; case 2: m = new ThrustModule(moduleID, level); break; case 3: m = new MaxShieldModule(moduleID, level); break; case 4: m = new ShieldRegenModule(moduleID, level); break; case 5: m = new MaxEnergyModule(moduleID, level); break; case 6: m = new DamageModule(moduleID, level); break; case 7: m = new DefenseModule(moduleID, level); break; case 8: m = new TurnRateModule(moduleID, level); break; case 9: m = new LateralThrustModule(moduleID, level); break; case 10: m = new TopSpeedModule(moduleID, level); break; } } TransactionAddStatefulCargo str = new TransactionAddStatefulCargo(ship, m, true); str.OnCompletion += ship.CargoAdded; cargoSynchronizer.RequestTransaction(str); } }
/// <summary> /// Creates a structure /// </summary> /// <param name="type"></param> /// <param name="health"></param> /// <param name="xPos"></param> /// <param name="yPos"></param> /// <param name="owner"></param> /// <param name="commandCenter"></param> /// <returns></returns> public static IStructure CreateStructure(StructureTypes type, float xPos, float yPos, Player owner, CommandCenter commandCenter, int currentAreaID, IPlayerLocator pl) { IStructure s; switch (type) { case (StructureTypes.LaserTurret): s = new Turret(_localIDManager.PopFreeID(), xPos, yPos, owner, commandCenter, currentAreaID, pl); break; case (StructureTypes.Biodome): s = new Biodome(xPos, yPos, _localIDManager.PopFreeID(), owner, currentAreaID); break; case (StructureTypes.PowerPlant): s = new PowerPlant(xPos, yPos, _localIDManager.PopFreeID(), owner, currentAreaID); break; case (StructureTypes.Silo): s = new Silo(xPos, yPos, _localIDManager.PopFreeID(), owner, currentAreaID); break; default: throw new Exception("CreateStructure not implemented for structure type " + type.ToString()); } _galaxyRegistrationManager.RegisterObject(s); return(s); }
/// <summary> /// Creates a structure /// </summary> /// <param name="type"></param> /// <param name="xPos"></param> /// <param name="yPos"></param> /// <param name="owner"></param> /// <param name="commandCenter"></param> /// <param name="currentAreaID"></param> /// <param name="pl"></param> /// <param name="writeToDB">If true, must specify dbm</param> /// <param name="dbm">must be specified if writeToDB is true</param> /// <returns></returns> public static IStructure CreateStructure(StructureTypes type, float xPos, float yPos, Player owner, CommandCenter commandCenter, int currentAreaID, IPlayerLocator pl, bool writeToDB = false, IDatabaseManager dbm = null) { IStructure s; switch (type) { case (StructureTypes.LaserTurret): TurretTypes t = owner.GetArea().AreaType == AreaTypes.Planet ? TurretTypes.Planet : TurretTypes.Space; s = new Turret(_localIDManager.PopFreeID(), xPos, yPos, owner.Id, currentAreaID, t, pl); break; case (StructureTypes.Biodome): return(CreateBiodome(xPos, yPos, owner, currentAreaID)); case (StructureTypes.PowerPlant): s = new PowerPlant(xPos, yPos, _localIDManager.PopFreeID(), owner.Id, currentAreaID); break; case (StructureTypes.Silo): s = new Silo(xPos, yPos, _localIDManager.PopFreeID(), owner.Id, currentAreaID); break; case (StructureTypes.CommandCenter): return(CreateCommandCenter(xPos, yPos, owner, currentAreaID)); case (StructureTypes.Factory): s = new Factory(xPos, yPos, _localIDManager.PopFreeID(), owner.Id, currentAreaID); break; default: throw new Exception("CreateStructure not implemented for structure type " + type.ToString()); } _galaxyRegistrationManager.RegisterObject(s); if (writeToDB) { if (dbm == null) { throw new Exception("Error: must specify IDatabaseManager dbm if writeToDB is true."); } else { dbm.SaveAsync(s); } } return(s); }
/// <summary> /// Returns created NPCPlayers /// </summary> /// <param name="galaxyManager"></param> /// <param name="IDManager"></param> /// <param name="rm"></param> /// <param name="pm"></param> /// <param name="npcShips"></param> /// <returns></returns> async Task <IEnumerable <NPCPlayer> > CreateNPCs(GalaxyManager galaxyManager, LocalIDManager IDManager, GalaxyRegistrationManager rm, PlayerManager pm, LocatorService locatorService, CargoSynchronizer cargoSynchronizer, GlobalTeamManager teamManager, LocalIDManager galaxyIDManager) { Random r = new Random(666); var players = new List <NPCPlayer>(); var systems = galaxyManager.Systems; int npcCount = 0; foreach (PSystem s in systems) { List <Player> team1 = new List <Player>(); List <Player> team2 = new List <Player>(); List <Player> team3 = new List <Player>(); for (int i = 0; i < _config.NumNPCsPerSystem; i++) { List <WeaponTypes> weapons = new List <WeaponTypes>(); ShipTypes shipType = ShipTypes.Barge; switch (npcCount % 3) { case 0: shipType = ShipTypes.Penguin; break; case 1: shipType = ShipTypes.Barge; break; case 2: shipType = ShipTypes.Reaper; break; } if (shipType == ShipTypes.Reaper) { weapons.Add(WeaponTypes.LaserWave); weapons.Add(WeaponTypes.PlasmaCannon); } else { weapons.Add(WeaponTypes.AltLaser); weapons.Add(WeaponTypes.LaserWave); } ShipCreationProperties props = new ShipCreationProperties(r.Next(-20, 20), r.Next(-20, 20), (int)galaxyManager.SolAreaID, PilotTypes.NPC, shipType, weapons); IShip tempShip = _dbFillerUtils.ShipFactory.CreateShip(props); tempShip.ShipStats.ShieldType = ShieldTypes.QuickRegen; NPCPlayer p = pm.CreateNPCPlayer(locatorService); pm.RegisterPlayer(p); players.Add(p); tempShip.SetPlayer(p); p.SetActiveShip(tempShip, MockServer.WarpManager); TransactionAddStatelessCargo tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.AmbassadorMissile, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.Biodome, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; Helpers.DebugWarp(s, p, tempShip); ships.Add(tempShip); //Random team assignment switch (npcCount % 2) { case 0: team1.Add(p); break; case 1: team2.Add(p); break; case 2: team3.Add(p); break; } //Give the guy some turrets for (int j = 0; j < _config.NumTurretsPerNPC; j++) { var t = StructureFactory.CreateStructure(StructureTypes.LaserTurret, r.Next(-20, 20), r.Next(-20, 20), p, null, (int)p.CurrentAreaID, locatorService.PlayerLocator, true, dbm); p.GetArea().AddStructure(t); } AddModulesToShip(tempShip, 5, cargoSynchronizer, galaxyIDManager); npcCount++; } foreach (Planet pl in s.GetPlanets()) { npcCount = 0; for (int i = 0; i < _config.NumNpcsPerPlanet; i++) { ShipTypes shipType = ShipTypes.Barge; switch (npcCount % 3) { case 0: shipType = ShipTypes.Penguin; break; case 1: shipType = ShipTypes.Barge; break; case 2: shipType = ShipTypes.Reaper; break; } NPCPlayer p = pm.CreateNPCPlayer(locatorService); pm.RegisterPlayer(p); players.Add(p); IShip tempShip = new NPCShip(ShipStatManager.TypeToStats[shipType], locatorService); tempShip.ShipStats.ShieldType = ShieldTypes.QuickRegen; tempShip.Id = IDManager.PopFreeID(); rm.RegisterObject(tempShip); p.SetActiveShip(tempShip, MockServer.WarpManager); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.MissileLauncher)); if (shipType == ShipTypes.Reaper) { tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.LaserWave)); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.PlasmaCannon)); } else { tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.AltLaser)); tempShip.SetWeapon(WeaponManager.GetNewWeapon(WeaponTypes.LaserWave)); } TransactionAddStatelessCargo tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.AmbassadorMissile, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tr = new TransactionAddStatelessCargo(tempShip, StatelessCargoTypes.Biodome, 666666, true); cargoSynchronizer.RequestTransaction(tr); await tr.ResultTask; tempShip.PosX = r.Next(-20, 20); tempShip.PosY = r.Next(-20, 20); Helpers.DebugWarp(pl, p, tempShip); ships.Add(tempShip); //Random team assignment switch (npcCount % 2) { case 0: team1.Add(p); break; case 1: team2.Add(p); break; case 2: team3.Add(p); break; } AddModulesToShip(tempShip, 5, cargoSynchronizer, galaxyIDManager); npcCount++; } teamManager.DebugCreateNewTeam(team1); teamManager.DebugCreateNewTeam(team2); teamManager.DebugCreateNewTeam(team3); } } return(players); }
/// <summary> /// receivingPlayers get mines in cargo, owningPlayers get the mines added to the receivingSystems (deployed in space) /// </summary> /// <param name="receivingPlayers"></param> /// <param name="receivingSystems"></param> /// <param name="owningPlayers"></param> void AddMines(IEnumerable <Player> receivingPlayers, IEnumerable <PSystem> receivingSystems, IEnumerable <Player> owningPlayers, CargoSynchronizer cargoSynchronizer, GalaxyManager galaxyManager, LocalIDManager galaxyIDManager, StructureManager structureManager, LocatorService locatorService) { foreach (Player p in receivingPlayers) { for (int i = 0; i < _config.CARGO_NumMines; i++) { TransactionAddStatefulCargo t = new TransactionAddStatefulCargo(p.GetActiveShip(), new StatefulCargo(galaxyIDManager.PopFreeID(), StatefulCargoTypes.DefensiveMine), true); cargoSynchronizer.RequestTransaction(t); } } if (owningPlayers.Count() < 2) { return; } var itr = new CyclicalIterator <Player>(owningPlayers); itr.MoveNext(); foreach (var system in galaxyManager.Systems) { for (int i = 0; i < _config.NumMinesPerSystem; i++) { int ownerID = itr.Current.Id; var mine = new DefensiveMine(Rand.Random.Next(-system.AreaSize / 100, system.AreaSize / 100), Rand.Random.Next(-system.AreaSize / 100, system.AreaSize / 100), galaxyIDManager.PopFreeID(), ownerID, system.Id, locatorService.PlayerLocator); structureManager.RegisterObject(mine); system.AddStructure(mine); itr.MoveNext(); } } }
async Task FillPorts(GalaxyManager galaxyManager, LocalIDManager galaxyIDManager, CargoSynchronizer cargoSynchronizer) { var ports = galaxyManager.GetAllAreas().Where(a => a.AreaType == AreaTypes.Port); CargoTransaction lastTransaction = null; foreach (var p in ports) { var port = p as Port; foreach (var s in _config.PortConfig.StatefulCargoCounts) { StatefulCargo sc; for (int i = 0; i < s.Value; i++)//Yes, this loop is lazy, but it's 11:30PM... { //TODO: make a StatefulCargoFactory switch (s.Key) { case StatefulCargoTypes.Barge: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Barge]); break; } case StatefulCargoTypes.BattleCruiser: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.BattleCruiser]); break; } case StatefulCargoTypes.Penguin: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Penguin]); break; } case StatefulCargoTypes.Reaper: { sc = new CargoShip(galaxyIDManager.PopFreeID(), 666, ShipStats[ShipTypes.Reaper]); break; } case StatefulCargoTypes.LaserTurret: { sc = new CargoLaserTurret(galaxyIDManager.PopFreeID(), 666, new LaserWeaponStats()); break; } default: { sc = new StatefulCargo(galaxyIDManager.PopFreeID(), s.Key); break; } } CargoTransaction tr = new TransactionAddStatefulCargo(port, sc, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } } foreach (var s in _config.PortConfig.StatelessCargoCounts) { var tr = new TransactionAddStatelessCargo(port, s.Key, s.Value, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } foreach (var s in _config.PortConfig.ModuleCounts) { Module m = ModuleFactory.CreateModule(s.Key, galaxyIDManager.PopFreeID(), 1); var tr = new TransactionAddStatefulCargo(port, m, true); cargoSynchronizer.RequestTransaction(tr); lastTransaction = tr; } } if (lastTransaction != null) { await lastTransaction.ResultTask; } }
public static PSystem GenerateStarsystem(int numberOfPlanets, int ID, GalaxyManager galaxyManager, LocalIDManager idManager, GalaxyRegistrationManager rm, LocatorService ls) { bool hasPort = false; // Add sun float size = r.Next(100, 255) / 100f; float mass = r.Next(50, 255) / 10f; float innerGravity = mass / 1875f; float outerGravity = innerGravity / 6f; var s = (SunTypes[])Enum.GetValues(typeof(SunTypes)); int type = r.Next(0, s.Length); var star = new Star(size, mass, innerGravity, outerGravity, s[type]); var system = new PSystem(star, ID, ls); system.AreaSize = baseBorder + r.Next(0, borderVariation); // Add Name system.AreaName = NameProvider.GetRandomName(); rm.RegisterObject(system); for (int i = 0; i < numberOfPlanets; i++) { Planet planet; bool SatisfiedWithResults = false; int numAttempts = MaxAttempts; system.IsCluster = false; int clusterType = 0; planet = new Planet(system, 0, ls); planet.AreaName = system.AreaName + " " + ToRoman(i + 1); if (r.Next(0, 101) > (100 - chanceOfCluster)) // Chance for planets to cluster { system.IsCluster = true; clusterType = r.Next(0, clusterRadii.Count()); // Get which type of cluster } planet.Init(i, moonChance, minDistance, incrementOfOrbit, variationOfOrbit, 0, r.Next()); SatisfiedWithResults = !CheckForCollisions(system, planet); // True if we find any collisions. while (!SatisfiedWithResults)// While we're not content with the generation { planet.Init(i, moonChance, minDistance, incrementOfOrbit, variationOfOrbit, 0, r.Next()); SatisfiedWithResults = !CheckForCollisions(system, planet); // True if we find any collisions. if (SatisfiedWithResults && system.IsCluster && i > 0) { SatisfiedWithResults = CheckForCollisions(system, planet, clusterRadii[clusterType]); // Cluster planets } numAttempts++; if (numAttempts >= MaxAttempts && !system.IsCluster) { break; // Breaks out of infinite pass if it ever occurs } else if (numAttempts >= MaxAttempts && system.IsCluster) { i = 0; // Reset the whole operation because there's a bad system being generated. foreach (Planet pll in system.GetPlanets()) { system.RemovePlanet(pll); rm.DeRegisterObject(pll); } break; } } if (SatisfiedWithResults == false) // Don't add a colliding planet! { //Logger.log(Log_Type.WARNING, "Skipped adding a planet due to MaxAttempts being too much"); continue; } planet.Id = idManager.PopFreeID(); system.AddPlanet(planet); planet.ParentAreaID = system.Id; rm.RegisterObject(planet); if (system.AreaSize < planet.Distance - baseBorder) // If we're skinny, throw in a little extra space { system.AreaSize = planet.Distance + baseBorder + r.Next(0, borderVariation); } } foreach (Planet p in system.GetPlanets()) { _generateMoons(system, p, galaxyManager, idManager, rm, ls); } foreach (Planet m in system.GetMoons()) { if (system.AreaSize < ls.AreaLocator.GetArea(m.IDToOrbit).Distance + m.Distance + baseBorder) { // If we're skinny, throw in a little extra space system.AreaSize = ls.AreaLocator.GetArea(m.IDToOrbit).Distance + m.Distance + baseBorder + r.Next(0, borderVariation); } } // Port Generation if (r.Next(0, 100) > 100 - portChance && !hasPort) { if (system.MoonCount > 0) { for (int i = 0; i < system.MoonCount; i++) { } int cr = r.Next(0, system.MoonCount + 1); // Finds moon to turn into port. int currentMoon = 0; // Used to get the moon foreach (Planet m in system.GetMoons()) { if (currentMoon == cr) { Planet moonToPort = m; moonToPort.ParentAreaID = system.Id; system.RemoveMoon(m); rm.DeRegisterObject(m); Port por = new Port(idManager.PopFreeID(), moonToPort, system.AreaName, ShipStatManager.StatShipList.ToList <ShipStats>(), ls); // Converts a moon into a port. system.AddPort(por); rm.RegisterObject(por); hasPort = true; break; } currentMoon++; } } } return(system); }
/// <summary> /// Generates n moons for a single planet, where n is divined from some mystical voodoo black magic related to planet.Scale /// </summary> /// <param name="system"></param> /// <param name="planet"></param> /// <param name="galaxyManager"></param> /// <param name="idManager"></param> /// <param name="rm"></param> /// <param name="pl"></param> /// <param name="al"></param> /// <param name="sl"></param> /// <param name="tl"></param> /// <param name="mem"></param> static void _generateMoons(PSystem system, Planet planet, GalaxyManager galaxyManager, LocalIDManager idManager, GalaxyRegistrationManager rm, LocatorService ls) { // If any moons, add them if (planet.HasMoons) { int parentID = planet.Id; int numMoons = 0; // Creates moon based on scale of size switch (planet.Scale) { case 0: case 1: case 2: case 3: case 4: case 5: planet.PlanetType = (PlanetTypes)r.Next(0, (int)PlanetTypes.OceanicSmall + 1); planet.Scale += 3; // Makes the small sprites scale better break; case 6: case 7: planet.PlanetType = (PlanetTypes)r.Next(0, (int)PlanetTypes.OceanicSmall + 1); if (planet.PlanetType >= PlanetTypes.Crystalline && planet.PlanetType < PlanetTypes.Port) { planet.Scale += 3; // Makes the small sprites scale better } numMoons = r.Next(3) + 2; break; case 8: case 9: numMoons = r.Next(2) + 3; break; case 10: case 11: case 12: numMoons = r.Next(3) + 3; break; } for (int m = 0; m < numMoons; m++) { Planet moon; moon = new Planet(planet, idManager.PopFreeID(), ls); InitializeMoon(moon, m, moonMinimumDistance, moonIncrementOfOrbit * m, moonVariationOfOrbit, planet, planet.Id, r.Next()); if (moon.PlanetType >= PlanetTypes.Crystalline && moon.PlanetType < PlanetTypes.Port) { moon.Scale += 3; // Makes the small sprites scale better } // #Yolo moon.AreaName = planet.AreaName + "abcdefghijklmnopqrstuvwxyz"[m]; bool SatisfiedWithResults = false; int maxAttempts = 200; int numAttempts = 0; while (!SatisfiedWithResults)// While we're not content with the generation { // Add moons moon = new Planet(planet, moon.Id, ls); InitializeMoon(moon, m, moonMinimumDistance, moonIncrementOfOrbit * m, moonVariationOfOrbit, planet, planet.Id, r.Next()); // Add another while here to check for maxDistance, as well as an If for if the system should follow that path of generation SatisfiedWithResults = !CheckForCollisions(system, planet, moon, galaxyManager); // True if we find any collisions. numAttempts++; if (numAttempts >= maxAttempts) { break; // Breaks out of infinite pass if it ever occurs } } if (!SatisfiedWithResults) // Don't add a colliding planet! { //Logger.log(Log_Type.ERROR, "Skipped adding a moon due to MaxAttempts being too much"); idManager.PushFreeID(moon.Id); continue; } moon.IsMoon = true; rm.RegisterObject(moon); system.AddMoon(moon); moon.ParentAreaID = system.Id; } } }
public List <PSystem> generateAndFillGalaxy(int numPlanetsPerSystem, int solID, int numSystems, IEnumerable <PlanetLayout> layouts, GalaxyManager galaxyManager, LocalIDManager IDManager, GalaxyRegistrationManager rm, LocatorService ls) { float warpXPos = 0; float warpYPos = 0; int amount = 0; List <PSystem> generatedSystems = new List <PSystem>(); PSystem sol = GenerateStarsystem(numPlanetsPerSystem, solID, galaxyManager, IDManager, rm, ls); sol.AreaName = "Sol"; amount += sol.PlanetCount; generatedSystems.Add(sol); for (int i = 0; i < numSystems - 1; i++) { tempsys = GenerateStarsystem(numPlanetsPerSystem, IDManager.PopFreeID(), galaxyManager, IDManager, rm, ls); amount += tempsys.PlanetCount; generatedSystems.Add(tempsys); //GalaxyManager.idToSystem.TryAdd(tempsys.ID, tempsys); } //ConsoleManager.WriteToFreeLine("Average of " + amount / numSystems + " planets per system"); //Randomly link the systems //Take each system, iterate through the systems list and randomly connect them, or don't for (int i = 0; i < generatedSystems.Count; i++) { for (int j = i + 1; j < generatedSystems.Count; j++) { if (r.Next(0, 100) <= 30) //30% probability of having the systems link { warpXPos = r.Next(-(generatedSystems[i].AreaSize - 200), (generatedSystems[i].AreaSize - 200)); warpYPos = -(int)Math.Sqrt(Math.Pow((generatedSystems[i].AreaSize - 200f), 2) - Math.Pow(warpXPos, 2)); tempWarp = new Warphole(warpXPos / 100, warpYPos / 100, generatedSystems[i].Id, generatedSystems[j].Id, (byte)generatedSystems[i].Warpholes.Count); tempWarp.DestinationAreaID = generatedSystems[j].Id; generatedSystems[i].Warpholes.Add(tempWarp); //Normalizing the vector warpXPos = warpXPos / (generatedSystems[i].AreaSize - 200f); warpYPos = warpYPos / (generatedSystems[i].AreaSize - 200f); //Converting it to the length for the other system, flipping to other side of system warpXPos = -warpXPos * (generatedSystems[j].AreaSize - 200f); warpYPos = -warpYPos * (generatedSystems[j].AreaSize - 200f); tempWarp = new Warphole(warpXPos / 100, warpYPos / 100, generatedSystems[j].Id, generatedSystems[i].Id, (byte)generatedSystems[j].Warpholes.Count); tempWarp.DestinationAreaID = generatedSystems[i].Id; generatedSystems[j].Warpholes.Add(tempWarp); } } } AssignPlanetLayouts(layouts, generatedSystems); return(generatedSystems); }