Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Sandbox.Game;
- using Sandbox.Game.Entities;
- using Sandbox.ModAPI;
- using Sandbox.ModAPI.Contracts;
- using VRage.Game;
- using VRage.Game.Components;
- using VRage.Game.ModAPI;
- using VRage.Utils;
- using VRageMath;
- namespace Axebeard.CargoContractGenerator
- {
- class CargoContractGen
- {
- [MySessionComponentDescriptor(MyUpdateOrder.NoUpdate)]
- public class Klime : MySessionComponentBase
- {
- public override void BeforeStart()
- {
- MyAPIGateway.Utilities.MessageEntered += MessageEntered;
- //MyAPIGateway.Entities += OnEntityAdd;
- MyVisualScriptLogicProvider.BlockBuilt += ContractBlock;
- }
- private void ContractBlock(string typeId, string subtypeId, string gridName, long blockId)
- {
- //throw new NotImplementedException();
- }
- private void MessageEntered(string message_text, ref bool sendToOthers)
- {
- if (MyAPIGateway.Session.IsServer && message_text == "/roll")
- {
- IMyPlayer player = MyAPIGateway.Session.Player;
- if (player != null && player.Character != null)
- {
- Random rnd = new Random();
- int chance = rnd.Next(0, 100);
- int chanceForContract = 35; //chance a cargo contract is generated
- int chanceStation = 70; // chance destination is a station
- int chanceEasyDest = 50; // space > space or planet > planet contracts
- bool easyContract = false; //did we get an easy contract?
- int resetTime = 3600; //time to allow new contract to possibly generate on same contract block
- bool startOnPlanet = false; //is the contract block on a planet?
- bool contractMade = false; // was a contract successfully created?
- //rnd = new Random();
- chance = rnd.Next(0, 100);
- string strchance = "Roll for contract chance - " + chance;
- sendToOthers = false;
- MyVisualScriptLogicProvider.SendChatMessage(strchance);
- if (chance < chanceForContract)
- {
- //rnd = new Random();
- chance = rnd.Next(0, 100);
- //sendToOthers = false;
- //strchance = "Roll for chance of easy contract - " + chance;
- //MyVisualScriptLogicProvider.SendChatMessage(strchance);
- if (chance < chanceEasyDest) //should this be an easy contract?
- {
- sendToOthers = false;
- MyVisualScriptLogicProvider.SendChatMessage("Easy contract!");
- easyContract = true;
- }
- //rnd = new Random();
- chance = rnd.Next(0, 100);
- //strchance = "Roll for chance of station - " + chance;
- //sendToOthers = false;
- //MyVisualScriptLogicProvider.SendChatMessage(strchance);
- ////////////////////////////////Generate contract////////////////////////////////////////////////////////////////////////////////////////////////
- if ((chance < chanceStation) && (!easyContract)) //station and NOT easy
- {
- //sendToOthers = false;
- //MyVisualScriptLogicProvider.SendChatMessage("Station contract!");
- //station based contract
- //try to find an eligible station
- //if no eligible stations found, go to random contract
- //if suitable destination found: contractMade =
- ////////////////////////////Find all stations with contract blocks///////////////////////////////////////////////////////////////////////////
- HashSet<IMyEntity> _startUpEnts = new HashSet<IMyEntity>();
- MyAPIGateway.Entities.GetEntities(_startUpEnts);
- HashSet<string> UniqueStations = new HashSet<string>();
- foreach (var entity in _startUpEnts)
- {
- var grid = entity as MyCubeGrid;
- if (grid?.Physics?.IsStatic == true)
- {
- ////////////////////////////////////////////////Get friendly/neutral stations//////////////////////////////////////////////////////////////////////
- var ownerID = grid.BigOwners[0];
- var Relationship = MyIDModule.GetRelationPlayerBlock(ownerID, player.IdentityId, MyOwnershipShareModeEnum.Faction);
- MatrixD player_worldmatrix = player.Character.WorldMatrix;
- Vector3D playerpos = player_worldmatrix.Translation;
- if ((Relationship != MyRelationsBetweenPlayerAndBlock.Enemies) && (Vector3D.Distance(grid.WorldMatrix.Translation, playerpos) > 3000)) ; //only get stations at least 3km away
- {
- //check if name already exists, if not, add it to list
- string stationName = grid.DisplayName;
- if (UniqueStations.Add(stationName)) ;
- {
- sendToOthers = false;
- string stationfound = "Possible destination: " + stationName;
- MyVisualScriptLogicProvider.SendChatMessage(stationfound);
- contractMade = true;
- }
- }
- }
- }
- _startUpEnts.Clear();
- UniqueStations.Clear();
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- if (!contractMade) //if no contract has been made up to this point...
- {
- //random contract
- if (easyContract)
- {
- sendToOthers = false;
- MyVisualScriptLogicProvider.SendChatMessage("Easy contract 2!");
- //Easy contract - space to space or planet to planet
- }
- sendToOthers = false;
- MyVisualScriptLogicProvider.SendChatMessage("Random contract!");
- }
- }
- }
- }
- }
- protected void UnloadData()
- {
- //Important to unreigster from events
- MyAPIGateway.Utilities.MessageEntered -= MessageEntered;
- //MyAPIGateway.Utilities.MessageRecieved -= Utilities_MessageRecieved;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement