Skip to content

Instantly share code, notes, and snippets.

@ChuckSavage
Created December 6, 2018 10:36
Show Gist options
  • Save ChuckSavage/65e31355a4474beb94366a86c941bd3c to your computer and use it in GitHub Desktop.
Save ChuckSavage/65e31355a4474beb94366a86c941bd3c to your computer and use it in GitHub Desktop.
Space Engineers Pirate Raiders Script Modification
// Script written by Rich_27, Designed by Splitsie
// Modified by Dune to check for no antenna, and to echo antenna name & radius
// CHANGE ME: Name of the antenna responsible for spawning drones in the pirate base
const string ANTENNA_NAME = "Antenna name";
// CHANGE ME: The time interval between antenna increments, in seconds
const float TIME_INTERVAL = 120.0f;
// CHANGE ME: The minimum and maximum antenna distances
const float ANTENNA_MINIMUM = 5000.0f;
const float ANTENNA_MAXIMUM = 15000.0f;
// CHANGE ME: If true, the antenna increments before reseting
// If false, the antenna is set to a random distance each update
const bool INCREMENT_AND_RESET = true;
// CHANGE ME: The minimum and maximum antenna increments
const float MIN_INCREMENT = 50.0f;
const float MAX_INCREMENT = 250.0f;
AntennaController controller;
public Program()
{
IMyRadioAntenna antenna = GridTerminalSystem.GetBlockWithName(ANTENNA_NAME) as IMyRadioAntenna;
controller = new AntennaController(antenna, INCREMENT_AND_RESET);
Runtime.UpdateFrequency = UpdateFrequency.Update100;
}
// Make sure this value matches the "Update###" above
const double ticksPerRun = 100.0;
const double secondsPerTick = 1.0 / 60.0;
const double secondsPerRun = secondsPerTick * ticksPerRun;
double timeElapsed = 0;
public void Main(string argument, UpdateType updateSource)
{
// Every time the code runs, we keep track of the amount of time since
// we last adjusted the antenna
timeElapsed += secondsPerRun;
string name = controller.antenna?.CustomName ?? "No antenna found";
Echo("Monitoring: " + (name.ToLower().Contains("antenna") ? name : name + " antenna"));
if (controller.antenna != null)
{
Echo("Radius: " + ((int)controller.antenna.Radius).ToString());
if (timeElapsed >= TIME_INTERVAL)
{
controller.AdjustAntenna();
timeElapsed = 0;
}
}
}
public class AntennaController
{
public readonly IMyRadioAntenna antenna;
private bool incrementAndReset;
private Random random = new Random();
public AntennaController(IMyRadioAntenna antenna, bool incrementAndReset)
{
this.antenna = antenna;
this.incrementAndReset = incrementAndReset;
}
public void AdjustAntenna()
{
if (incrementAndReset)
{
// If the radius is below maximum, increment
// Otherwise, reset to minimum
if (antenna.Radius < ANTENNA_MAXIMUM)
{
float randomIncrement = GetNextFloat(random, MIN_INCREMENT, MAX_INCREMENT);
// Shorthand for: antenna.Radius = antenna.Radius + randomIncrement;
antenna.Radius += randomIncrement;
}
else
{
antenna.Radius = ANTENNA_MINIMUM;
}
}
else
{
// Set the radius to a random distance
float randomDistance = GetNextFloat(random, ANTENNA_MINIMUM, ANTENNA_MAXIMUM);
antenna.Radius = randomDistance;
}
}
private float GetNextFloat(Random r, float min, float max)
{
return (float)(r.NextDouble() * (max - min) + min);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment