Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Sandbox.Game;
  4. using Sandbox.Game.Entities;
  5. using Sandbox.ModAPI;
  6. using Sandbox.ModAPI.Contracts;
  7. using VRage.Game;
  8. using VRage.Game.Components;
  9. using VRage.Game.ModAPI;
  10. using VRage.Utils;
  11. using VRageMath;
  12.  
  13. namespace Axebeard.CargoContractGenerator
  14. {
  15.     class CargoContractGen
  16.     {
  17.         [MySessionComponentDescriptor(MyUpdateOrder.NoUpdate)]
  18.         public class Klime : MySessionComponentBase
  19.         {
  20.             public override void BeforeStart()
  21.             {
  22.                 MyAPIGateway.Utilities.MessageEntered += MessageEntered;
  23.                 //MyAPIGateway.Entities += OnEntityAdd;
  24.                 MyVisualScriptLogicProvider.BlockBuilt += ContractBlock;
  25.             }
  26.  
  27.             private void ContractBlock(string typeId, string subtypeId, string gridName, long blockId)
  28.             {
  29.                 //throw new NotImplementedException();
  30.             }
  31.  
  32.             private void MessageEntered(string message_text, ref bool sendToOthers)
  33.             {
  34.                 if (MyAPIGateway.Session.IsServer && message_text == "/roll")
  35.                 {
  36.                     IMyPlayer player = MyAPIGateway.Session.Player;
  37.                     if (player != null && player.Character != null)
  38.                     {
  39.                         Random rnd = new Random();
  40.                         int chance = rnd.Next(0, 100);
  41.                         int chanceForContract = 35; //chance a cargo contract is generated
  42.                         int chanceStation = 70; // chance destination is a station
  43.                         int chanceEasyDest = 50; // space > space or planet > planet contracts
  44.                         bool easyContract = false; //did we get an easy contract?
  45.                         int resetTime = 3600; //time to allow new contract to possibly generate on same contract block    
  46.                         bool startOnPlanet = false; //is the contract block on a planet?
  47.                         bool contractMade = false; // was a contract successfully created?
  48.  
  49.                         //rnd = new Random();
  50.                         chance = rnd.Next(0, 100);
  51.                         string strchance = "Roll for contract chance -  " + chance;
  52.                         sendToOthers = false;
  53.                         MyVisualScriptLogicProvider.SendChatMessage(strchance);
  54.                         if (chance < chanceForContract)
  55.                         {
  56.                             //rnd = new Random();
  57.                             chance = rnd.Next(0, 100);
  58.                             //sendToOthers = false;
  59.                             //strchance = "Roll for chance of easy contract -  " + chance;
  60.                             //MyVisualScriptLogicProvider.SendChatMessage(strchance);
  61.  
  62.                             if (chance < chanceEasyDest) //should this be an easy contract?
  63.                             {
  64.                                 sendToOthers = false;
  65.                                 MyVisualScriptLogicProvider.SendChatMessage("Easy contract!");
  66.                                 easyContract = true;
  67.                             }
  68.                             //rnd = new Random();
  69.                             chance = rnd.Next(0, 100);
  70.                             //strchance = "Roll for chance of station -  " + chance;
  71.                             //sendToOthers = false;
  72.                             //MyVisualScriptLogicProvider.SendChatMessage(strchance);
  73.                             ////////////////////////////////Generate contract////////////////////////////////////////////////////////////////////////////////////////////////
  74.                             if ((chance < chanceStation) && (!easyContract)) //station and NOT easy
  75.                             {
  76.                                 //sendToOthers = false;
  77.                                 //MyVisualScriptLogicProvider.SendChatMessage("Station contract!");
  78.                                 //station based contract
  79.                                 //try to find an eligible station
  80.                                 //if no eligible stations found, go to random contract
  81.                                 //if suitable destination found: contractMade =
  82.                                 ////////////////////////////Find all stations with contract blocks///////////////////////////////////////////////////////////////////////////
  83.                                 HashSet<IMyEntity> _startUpEnts = new HashSet<IMyEntity>();
  84.                                 MyAPIGateway.Entities.GetEntities(_startUpEnts);
  85.                                 HashSet<string> UniqueStations = new HashSet<string>();
  86.  
  87.                                 foreach (var entity in _startUpEnts)
  88.                                 {
  89.                                     var grid = entity as MyCubeGrid;
  90.                                     if (grid?.Physics?.IsStatic == true)
  91.                                     {
  92.                                         ////////////////////////////////////////////////Get friendly/neutral stations//////////////////////////////////////////////////////////////////////
  93.                                         var ownerID = grid.BigOwners[0];
  94.                                         var Relationship = MyIDModule.GetRelationPlayerBlock(ownerID, player.IdentityId, MyOwnershipShareModeEnum.Faction);
  95.                                         MatrixD player_worldmatrix = player.Character.WorldMatrix;
  96.                                         Vector3D playerpos = player_worldmatrix.Translation;
  97.  
  98.                                         if ((Relationship != MyRelationsBetweenPlayerAndBlock.Enemies) && (Vector3D.Distance(grid.WorldMatrix.Translation, playerpos) > 3000)) ; //only get stations at least 3km away
  99.                                         {
  100.                                             //check if name already exists, if not, add it to list
  101.                                             string stationName = grid.DisplayName;
  102.                                             if (UniqueStations.Add(stationName)) ;
  103.                                             {
  104.                                                 sendToOthers = false;
  105.                                                 string stationfound = "Possible destination: " + stationName;
  106.                                                 MyVisualScriptLogicProvider.SendChatMessage(stationfound);
  107.                                                 contractMade = true;
  108.                                             }
  109.                                         }
  110.                                     }
  111.                                 }
  112.                                 _startUpEnts.Clear();
  113.                                 UniqueStations.Clear();
  114.                             }
  115.                             //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116.                             if (!contractMade) //if no contract has been made up to this point...
  117.                             {
  118.                                 //random contract
  119.                                 if (easyContract)
  120.                                 {
  121.                                     sendToOthers = false;
  122.                                     MyVisualScriptLogicProvider.SendChatMessage("Easy contract 2!");
  123.                                     //Easy contract - space to space or planet to planet
  124.                                 }
  125.                                 sendToOthers = false;
  126.                                 MyVisualScriptLogicProvider.SendChatMessage("Random contract!");
  127.                             }
  128.  
  129.  
  130.                         }
  131.  
  132.                     }
  133.                 }
  134.             }
  135.             protected void UnloadData()
  136.             {
  137.                 //Important to unreigster from events
  138.                 MyAPIGateway.Utilities.MessageEntered -= MessageEntered;
  139.                 //MyAPIGateway.Utilities.MessageRecieved -= Utilities_MessageRecieved;
  140.             }
  141.  
  142.         }
  143.        
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement