public void getAreaCreator(byte _direction, int _userId, Ship _ship, ref List <Ship> _ships, ref List <SpacegameServer.Core.SystemMap> _stars, ref List <SpacegameServer.Core.Colony> _colonies, Colony scanningColony = null) { var starXY = getDestinationField(_ship, _direction); if (_userId < 0) { return; } SpacegameServer.Core.User user = (SpacegameServer.Core.User)users[_userId]; if (user == null) { return; } //fetch an area of radius 7 around the target field //check all colonies inside it. Every colony that has the targetfield inside it's border range is returned... //List<Field> neigbouringFields = new List<Field>(); //GeometryIndex.getFields(_ship.field, 7, neigbouringFields); //int targetRegionId = GeometryIndex.calcRegionId(starXY.Key, starXY.Value); //var targetField = GeometryIndex.regions[targetRegionId].findOrCreateField(starXY.Key, starXY.Value); int targetRegionId = GeometryIndex.calcRegionId(starXY.Key, starXY.Value); var targetField = GeometryIndex.regions[targetRegionId].findOrCreateField(starXY.Key, starXY.Value); //fetch all colonies affecting the targetField _colonies = Core.Instance.colonies.Where(colony => targetField.InfluencedBy.Any(influencer => influencer == colony.Value)).Select(colKeyValue => colKeyValue.Value).ToList(); //fetch the star each colony is positioned foreach (int fieldId in (_colonies.Select(colony => colony.field.id))) { Field currentField = ((Field)GeometryIndex.allFields[fieldId]); if (currentField.starId != null) { int starId = (int)currentField.starId; SystemMap star = stars[starId]; _stars.Add(star); //if scan is determined for a single ship, check if that user already kwos the systems scanned: if ((_ship != null) && !user.knownStars.Contains(starId)) { user.knownStars.Add(starId); UserStarMap newStar = new UserStarMap(user.id, starId); List <AsyncInsertable> newKnownStar = new List <AsyncInsertable>(); newKnownStar.Add(newStar); dataConnection.insertAsyncTransaction(newKnownStar); } } } return; }
public void getUserScans(int _userId, Ship _ship, ref List <Ship> _ships, ref List <SpacegameServer.Core.SystemMap> _stars, ref List <SpacegameServer.Core.Colony> _colonies, Colony scanningColony = null) { if (_userId < 0) { return; } SpacegameServer.Core.User user = (SpacegameServer.Core.User)users[_userId]; if (user == null) { return; } //during startup, add all colonies of stars known to the user if (_ship == null && scanningColony == null) { _colonies = Core.Instance.stars.Where(star => user.knownStars.Any(known => known == star.Value.id)) .SelectMany(star => star.Value.planets) .Where(planet => planet.colony != null) .Select(planet => planet.colony).ToList(); /*foreach (int starId in user.knownStars) * { * if (Core.Instance.stars[starId].planets.Any(planet => planet.colony != null)) * { * _colonies = Core.Instance.stars[starId].planets.Where(planet => planet.colony != null).Select(planet => planet.colony).ToList(); * } * } */ } //make a list with unique fields, only the scanner with the greates scanrange is needed Dictionary <Int32, KeyValuePair <Field, byte> > fieldScans = new Dictionary <int, KeyValuePair <Field, byte> >(); if (_ship != null || scanningColony != null) { if (_ship != null) { fieldScans[_ship.field.id] = new KeyValuePair <Field, byte>(_ship.field, _ship.scanRange); } if (scanningColony != null) { fieldScans[scanningColony.field.id] = new KeyValuePair <Field, byte>(scanningColony.field, scanningColony.scanRange); } } else { for (int i = 0; i < user.ships.Count; i++) { Ship currentShip = user.ships[i]; KeyValuePair <Field, byte> val; if (fieldScans.TryGetValue(user.ships[i].field.id, out val)) { if (val.Value < currentShip.scanRange) { fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange); } } else { fieldScans[currentShip.field.id] = new KeyValuePair <Field, byte>(currentShip.field, currentShip.scanRange); } } for (int i = 0; i < user.colonies.Count; i++) { Colony currentColony = user.colonies[i]; KeyValuePair <Field, byte> val; if (fieldScans.TryGetValue(currentColony.field.id, out val)) { if (val.Value < currentColony.scanRange) { fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange); } } else { fieldScans[currentColony.field.id] = new KeyValuePair <Field, byte>(currentColony.field, currentColony.scanRange); } } } //find all neighbouring fields of the scanners //use the regions to accomplish this List <int> scannedFields = new List <int>(); foreach (KeyValuePair <Int32, KeyValuePair <Field, byte> > scanner in fieldScans) { scanner.Value.Key.getScanRange(scanner.Value.Value, scannedFields); } //detect all ships, stars and colonies on the fields scanned foreach (int fieldId in (scannedFields.Distinct())) { Field currentField = ((Field)GeometryIndex.allFields[fieldId]); foreach (Ship ship in currentField.ships) { _ships.Add(ship); } _ships = _ships.OrderBy(o => o.id).ToList(); if (currentField.starId != null) { int starId = (int)currentField.starId; SystemMap star = stars[starId]; _stars.Add(star); foreach (Colony colony in currentField.colonies) { if (!_colonies.Any(c => c == colony)) { _colonies.Add(colony); } } //if scan is determined for a single ship, check if that user already kwos the systems scanned: if ((_ship != null) && !user.knownStars.Contains(starId)) { user.knownStars.Add(starId); UserStarMap newStar = new UserStarMap(user.id, starId); List <AsyncInsertable> newKnownStar = new List <AsyncInsertable>(); newKnownStar.Add(newStar); dataConnection.insertAsyncTransaction(newKnownStar); } } } //add usermap and transcensionStars to the list of known stars: if (_ship == null && scanningColony == null) { foreach (int starId in user.knownStars) { if (!_stars.Exists(x => x.id == starId)) { _stars.Add(this.stars[starId]); } } List <Ship> transcensions = (from ship in this.ships where ship.Value.shipTranscension != null select ship.Value).ToList(); foreach (Ship transc in transcensions) { if (transc.systemId == 0) { continue; } if (!_stars.Exists(x => x.id == transc.systemId)) { _stars.Add(this.stars[transc.systemId]); } } } return; }