internal static Ship Load(NounCollection shelf, string id, Sector sector, Universe u) { var s = new Ship(sector, u); var noun = shelf[id]; s.ID = id; s.RealName = noun["name"][0]; s.Owner = u.Players.SingleOrDefault(p => p.TeamName == noun["owner"][0]); if (s.Owner == null) { s.Owner = u.AddPlayer(noun["owner"][0]); } if (shelf.Contains("Vnotes")) { if (shelf["Vnotes"].Contains("Name:" + s.ID)) { s.DisplayName = shelf["Vnotes"]["Name:" + s.ID][0]; } else if (s.RealName.StartsWith(s.Owner.TeamName + " ")) { s.DisplayName = s.RealName.Substring(s.Owner.TeamName.Length + 1); } else { s.DisplayName = s.RealName; } } else if (s.RealName.StartsWith(s.Owner.TeamName + " ")) { s.DisplayName = s.RealName.Substring(s.Owner.TeamName.Length + 1); } else { s.DisplayName = s.RealName; } s.IsMothership = noun["isMothership"][0] == 1; s.PreviousSector = u.Sectors.SingleOrDefault(sec => { if (noun.Contains("previousLocation") && noun["previousLocation"].Count > 0) { return(noun["previousLocation"][0] == sec.ID); } return(false); }); if (s.IsOurs) { s.Components = new Resources { Hull = noun["hull"][0], Weapons = noun["weapon"][0], Thrusters = noun["thruster"][0], Shields = noun["shield"][0], }; s.Size = (int)(Math.Sqrt((int)noun["hull"][0] + (int)noun["weapon"][0] + (int)noun["thruster"][0] + (int)noun["shield"][0])); s.IsArmed = noun["weapon"][0] > 0; s.IsMobile = noun["thruster"][0] > 0; s.ShieldCharge = noun["shieldcharge"][0]; } else { s.Size = noun["sizeClass"][0]; var status = noun["status"][0]; s.IsArmed = status == "normal" || status == "immobile"; s.IsMobile = status == "normal" || status == "unarmed"; } return(s); }
private void MainForm_Load(object sender, EventArgs e) { if (GameFileName == null) { var dlg = new OpenFileDialog(); dlg.Filter = "Savegames (*.gam)|*.gam|All files|*.*"; dlg.Title = "Please select a savegame to load"; var result = dlg.ShowDialog(); if (result != DialogResult.OK) { Application.Exit(); return; } GameFileName = dlg.FileName; } var fname = Path.GetFileNameWithoutExtension(GameFileName); plrFileName = Path.Combine(Path.GetDirectoryName(GameFileName), fname) + ".plr"; try { var splitFileName = fname.Split('_'); gameName = splitFileName[0]; turnNumber = int.Parse(splitFileName[1]); playerName = splitFileName[2]; Text = string.Format("Event Horizon - {0} turn {1}", gameName, turnNumber, playerName); } catch { Text = "Event Horizon"; } try { var doc = XDocument.Load(GameFileName); var root = doc.Root; var shelf = NounCollection.FromXml(root); Library.NounLibrary.Add(shelf); // TODO - try to load commands from existing plr file? universe = Universe.Load(shelf["Vroot"]); map.Universe = universe; LoadEvents(); dgvShips.AutoGenerateColumns = false; selectedShips = new List <Ship>(); foreach (var sector in universe.Sectors) { foreach (var ship in sector.Ships) { var bmp = new Bitmap(128, 128); var g = Graphics.FromImage(bmp); g.Clear(Color.Black); ilShipsBig.Images.Add(ship.ID, bmp); ilShipsSmall.Images.Add(ship.ID, bmp); } } map.HighlightedShips = universe.Sectors.SelectMany(s => s.Ships); dgvIncome.AutoGenerateColumns = false; dgvIncome.DataSource = universe.IncomeSectors.ToArray(); dgvConstruction.AutoGenerateColumns = false; foreach (var design in universe.Designs) { colDesign.Items.Add(design.Name); } bsConstruction.DataSource = universe.Construction; dgvConstruction.DataSource = bsConstruction; UpdateTreasury(); dgvBattles.AutoGenerateColumns = false; dgvBattles.DataSource = universe.Battles; dgvEmpires.AutoGenerateColumns = false; foreach (var dir in Directory.GetDirectories(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Shipsets")).Select(dir => Path.GetFileName(dir)).Union(universe.Players.Select(p => p.ShipsetName))) { colShipset.Items.Add(dir); } dgvEmpires.DataSource = universe.Players.ToArray(); allShips = SortShipsDefault(universe.Ships).ToArray(); } catch (System.Xml.XmlException ex) { MessageBox.Show(GameFileName + "\nis not a valid Event Horizon savegame."); Application.Exit(); } catch (FileNotFoundException ex) { MessageBox.Show($"{GameFileName} does not exist."); Application.Exit(); } catch (DirectoryNotFoundException ex) { MessageBox.Show($"{GameFileName} does not exist."); Application.Exit(); } // load settings Settings.Default.Reload(); // start music if we have any audio devices to play on Music.CurrentMood = Mood.Strategic; // TODO - make a settings dialog! Settings.Default.Save(); }