private void PlayerDied(ZoneBlock zone, IMyPlayer player, IMyFaction faction) { if (zone.PointsRemovedOnDeath.Value == 0 || !MyAPIGateway.Multiplayer.IsServer) { return; } long facId = faction.FactionId; string planetName = zone.GetClosestPlanet(); if (!Planets.Any(description => description.Name == planetName)) { var world = new PlanetDescription() { Name = planetName, Scores = new List <ScoreDescription>() }; Planets.Add(world); } PlanetDescription planet = Planets.Find(p => p.Name == planetName); if (!planet.Scores.Any(s => s.FactionId == facId)) { planet.Scores.Add(new ScoreDescription() { FactionId = facId, FactionName = faction.Name, FactionTag = faction.Tag, Points = 1, PlanetId = planetName, GridName = (zone.Entity as IMyCubeBlock).CubeGrid.DisplayName, }); } ScoreDescription score = planet.Scores.Find(s => s.FactionId == facId); int original = score.Points; if (original - zone.PointsRemovedOnDeath.Value < 1) { score.Points = 1; } else { score.Points = original - zone.PointsRemovedOnDeath.Value; } string message = $"[{faction.Tag}] {player.DisplayName} Died: {score.Points - original} Points"; Network.Say(message); }
private void AwardPoints(ZoneBlock zone, IMyFaction faction, int enemies, bool displayHeader) { if (!MyAPIGateway.Multiplayer.IsServer) { return; } string planetName = zone.GetClosestPlanet(); if (!Planets.Any(description => description.Name == planetName)) { PlanetDescription p = new PlanetDescription() { Name = planetName, Scores = new List <ScoreDescription>() }; Planets.Add(p); } long facId = faction.FactionId; PlanetDescription planet = Planets.Find(w => w.Name == planetName); IMyCubeGrid kothGrid = (zone.Entity as IMyCubeBlock).CubeGrid; if (!planet.Scores.Any(s => s.FactionId == facId)) { planet.Scores.Add(new ScoreDescription() { FactionId = facId, FactionName = faction.Name, FactionTag = faction.Tag, Points = 1, PlanetId = planetName, GridName = kothGrid.DisplayName }); } int total = GetTotalScore(planet); ScoreDescription score = planet.Scores.Find(s => s.FactionId == facId); int current = score.Points; int points; if (zone.PointsOnCap.Value == 0) { points = (int)(((float)(total - current) / (float)total) * 5f * enemies) + 1 + enemies; } else { points = zone.PointsOnCap.Value; } planet.Scores.Find(s => s.FactionId == facId).Points += points; zone.PointsEarnedSincePrize += points; if (zone.AwardPointsAsCredits.Value) { faction.RequestChangeBalance(points * zone.CreditsPerPoint.Value); } if (zone.PointsEarnedSincePrize >= zone.PointsForPrize.Value) { zone.PointsEarnedSincePrize -= zone.PointsForPrize.Value; IMyCargoContainer prizebox = null; List <IMySlimBlock> temp = new List <IMySlimBlock>(); kothGrid.GetBlocks(temp, s => { if (prizebox == null && s.FatBlock != null && s.FatBlock is IMyCargoContainer && s.FatBlock.BlockDefinition.SubtypeId == "Prizebox") { prizebox = s.FatBlock as IMyCargoContainer; } return(false); }); if (zone.UseComponentReward.Value) { string prizeType = (zone.AdvancedComponentSelection.Value) ? zone.PrizeComponentSubtypeId.Value : zone.SelectedComponentString.Value; int amount = zone.PrizeAmountComponent.Value; MyDefinitionId definitionId = new MyDefinitionId(typeof(MyObjectBuilder_Component), prizeType); MyObjectBuilder_Component content = (MyObjectBuilder_Component)MyObjectBuilderSerializer.CreateNewObject(definitionId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, Content = content }; if (zone.SpawnIntoPrizeBox.Value) { if (prizebox == null) { Tools.Log(MyLogSeverity.Error, $"Could not find prize box on grid: {kothGrid.DisplayName} - {kothGrid.EntityId}"); } else if (prizebox.GetInventory().CanItemsBeAdded(amount, definitionId)) { prizebox.GetInventory().AddItems(amount, inventoryItem.Content); } } else { if (zone.Entity.GetInventory().CanItemsBeAdded(amount, definitionId)) { zone.Entity.GetInventory().AddItems(amount, inventoryItem.Content); } } } if (zone.UseIngotReward.Value) { string prizeType = (zone.AdvancedIngotSelection.Value) ? zone.PrizeIngotSubtypeId.Value : zone.SelectedIngotString.Value; int amount = zone.PrizeAmountIngot.Value; MyDefinitionId definitionId = new MyDefinitionId(typeof(MyObjectBuilder_Ingot), prizeType); MyObjectBuilder_Ingot content = (MyObjectBuilder_Ingot)MyObjectBuilderSerializer.CreateNewObject(definitionId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, Content = content }; if (zone.SpawnIntoPrizeBox.Value) { if (prizebox == null) { Tools.Log(MyLogSeverity.Error, $"Could not find prize box on grid: {kothGrid.DisplayName} - {kothGrid.EntityId}"); } else if (prizebox.GetInventory().CanItemsBeAdded(amount, definitionId)) { prizebox.GetInventory().AddItems(amount, inventoryItem.Content); } } else { if (zone.Entity.GetInventory().CanItemsBeAdded(amount, definitionId)) { zone.Entity.GetInventory().AddItems(amount, inventoryItem.Content); } } } if (zone.UseOreReward.Value) { string prizeType = (zone.AdvancedOreSelection.Value) ? zone.PrizeOreSubtypeId.Value : zone.SelectedOreString.Value; int amount = zone.PrizeAmountOre.Value; MyDefinitionId definitionId = new MyDefinitionId(typeof(MyObjectBuilder_Ore), prizeType); MyObjectBuilder_Ore content = (MyObjectBuilder_Ore)MyObjectBuilderSerializer.CreateNewObject(definitionId); MyObjectBuilder_InventoryItem inventoryItem = new MyObjectBuilder_InventoryItem { Amount = amount, Content = content }; if (zone.SpawnIntoPrizeBox.Value) { if (prizebox == null) { Tools.Log(MyLogSeverity.Error, $"Could not find prize box on grid: {kothGrid.DisplayName} - {kothGrid.EntityId}"); } else if (prizebox.GetInventory().CanItemsBeAdded(amount, definitionId)) { prizebox.GetInventory().AddItems(amount, inventoryItem.Content); } } else { if (zone.Entity.GetInventory().CanItemsBeAdded(amount, definitionId)) { zone.Entity.GetInventory().AddItems(amount, inventoryItem.Content); } } } } StringBuilder message = new StringBuilder(); if (displayHeader && zone.IsLocationNamed.Value) { if (zone.EncampmentMode.Value) { message.Append($"{kothGrid.DisplayName} on {planetName} Encampment Payout"); } else { message.Append($"{kothGrid.DisplayName} on {planetName} under attack"); } } byte[] bytes = Encoding.ASCII.GetBytes(message.ToString()); MyAPIGateway.Multiplayer.SendMessageToServer(8008, bytes); Network.Say(message.ToString()); message.Clear(); if (zone.AwardPointsAsCredits.Value) { message.Append($"{faction.Name} Scored {points} Points! ({points * zone.CreditsPerPoint.Value} credits)"); } else { message.Append($"{faction.Name} Scored {points} Points!"); } SaveData(); //NEXUS: if nexus is initialized, broadcast the message to nexus (and all other sectors in that way) //NEXUS: KotH should work normally if nexus is not initialized if (nexusInit) { //NEXUS: sends the message this sector displays in the chat to all the other sectors byte[] nexMessage = Encoding.ASCII.GetBytes(message.ToString()); Nexus.SendMessageToAllServers(nexMessage); //NEXUS: score is broadcasted to other sectors, this basically triggers the save not just on this sector but on all the others with the same score var serializedScore = MyAPIGateway.Utilities.SerializeToBinary <List <PlanetDescription> >(Planets); Nexus2.SendMessageToAllServers(serializedScore); } bytes = Encoding.ASCII.GetBytes(message.ToString()); MyAPIGateway.Multiplayer.SendMessageToServer(8008, bytes); Network.Say(message.ToString()); }