This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtually_Invisible_Communications_Protocol.cs
91 lines (80 loc) · 3.52 KB
/
Virtually_Invisible_Communications_Protocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//Script made by Lelebees: Last update at 26-03-2022 18:43 UTC+1 (Central European Time)
//Virtually Invisible Communications Protocol V1.0
string antennaName = "Antenna";
IEnumerator<bool> stateMachine;
IMyRadioAntenna antenna;
public Program()
{
// Initialize the script here
Runtime.UpdateFrequency = UpdateFrequency.Update1;
// Initialize an antenna so we can use the script
antenna = GridTerminalSystem.GetBlockWithName(antennaName) as IMyRadioAntenna;
}
public void RunStateMachine()
{
// This is the code from the MDK Wiki. If you want to learn more about coroutines (and scripting in general), go there!
if (stateMachine != null)
{
bool hasMoreSteps = stateMachine.MoveNext();
if (hasMoreSteps)
{
Runtime.UpdateFrequency |= UpdateFrequency.Once;
}
else
{
stateMachine.Dispose();
stateMachine = null;
}
}
}
public void Main(string arg, UpdateType updateType)
{
// If we've told the system to run a statemachine
if ((updateType & UpdateType.Once) == UpdateType.Once)
{
RunStateMachine();
}
else
{
// Run the normal script
// This is an example on how to run the send method. you need both lines.
stateMachine = Send("BroadcastChannel", "MessageText", TransmissionDistance.TransmissionDistanceMax);
Runtime.UpdateFrequency |= UpdateFrequency.Once;
// The line above will tell the system to run once more in the next tick, so we get to activate the if statement above
// PLEASE NOTE: The code above will run every tick. You probably don't want that, but this is a working example.
}
}
public IEnumerator<bool> Send(string broadcastChannel, string messageText, TransmissionDistance transmissionDistance)
{
// Do something here. You want to minimize the amount of things the script is doing while the antenna is on, so the signal is on for as little time as possible.
//Everything has been prepared, we can now open the broadcasting channel
antenna.EnableBroadcasting = true;
yield return true; //wait* a tick
//Send the message
IGC.SendBroadcastMessage(broadcastChannel, messageText, transmissionDistance);
yield return true; //wait* a tick
yield return true; //wait another tick so this fucking thing actually sends
//Close the broadcast so detection is now impossible
antenna.EnableBroadcasting = false;
// Message has been sent and broadcast has stopped.
// *The program doesn't actually wait. It stops and runs from that point on the next tick.
// this lowers the instruction count, making the script more preformance friendly, however,
// we're using it to make a delay inbetween actions.
}
public void Receive(string broadcastChannel)
{
// We create some IGC stuff here so we can receive stuff
IGC.RegisterBroadcastListener(broadcastChannel);
List<IMyBroadcastListener> listenerList = new List<IMyBroadcastListener>();
IGC.GetBroadcastListeners(listenerList);
if (listenerList[0].HasPendingMessage)
{
MyIGCMessage receivedMessage = new MyIGCMessage();
receivedMessage = listenerList[0].AcceptMessage();
// Take out the message data
string receivedText = receivedMessage.Data.ToString();
string receivedChannel = receivedMessage.Tag;
long messageSender = receivedMessage.Source;
// Do something with all of this stuff!
}
}