1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

[MOD] Inter-Grid Communications

Discussion in 'Programming Released Codes' started by JoeTheDestroyer, Dec 24, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    Inter-Grid Communications
    Workshop: Inter-Grid Communications mod, Inter-Grid Communications demonstration world
    *** WARNING *** This is very experimental, use at your own risk!

    IGC is a mod that expands the programmable block API to allow communication with other grids.

    The primary interface operates through custom block properties (using SetValue/GetValue).

    The basic idea is a lossy packet interface. Each radio antenna or laser antenna may be set to receive on some number of "channels". Messages are sent to specific channels and only those listening on that channel will receive that message.

    Something similar to the game's antenna network system has been reproduced. Non-enemy (to the originating antenna) antennas will relay messages forming a network.

    Finally, antennas can be used to scan for other radio sources (antennas, beacons and players).

    Commands

    Setting channels for listening
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.SetValue<string>("IGC.listen","example channel1\0example channel2");
    block.GetValue<string>("IGC.listen");
    
    This command is for configuring the set of channels that the radio or laser antenna should listen on.

    It's format is a string containing a nul-separated list of channel names. Each channel name can contain any character (except nul) and must be 128 characters or shorter.

    Receiving messages
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.GetValue<string>("IGC.recv");
    
    The is the command to take one received message from the radio or laser antenna. The message is permanently removed from the incoming message queue.

    The message is passed unaltered from when it was sent (so it includes the target channel tag). See below for more.

    If no message is waiting, null will be returned.

    Sending messages
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.SetValue<string>("IGC.send","example channel\0data");
    
    This is the command to send a message.

    The format is a string consisting of 1) the channel to send on, 2) a separating nul character, and 3) the data to send. Any characters are allowed (including nuls) in the data portion.

    Keep in mind that this is a lossy communication method so the message may not be received at all. Plan accordingly. (Note that sending very many or too large of messages increases the chance it will be dropped.)

    Incoming message callback
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.SetValue<IMyProgrammableBlock>("IGC.callback",Me);
    block.GetValue<IMyProgrammableBlock>("IGC.callback");
    block.SetValue<string>("IGC.cbarg","radio");
    block.GetValue<string>("IGC.cbarg");
    
    Because polling for messages can be wasteful, a callback mechanism has been provided.

    When a programmable block is set using the above commands, the antenna will run that programmable block whenever it receives a message (or when the game is loaded and messages are waiting).

    This callback will happen on the next tick after the message was received and can happen atmost once per tick.

    The string set for "IGC.cbarg" will be provided as the run argument for the PB so that the callback can be recognized.

    WARNING: A callback PB must be on the same grid as the antenna. This is a limitation imposed to prevent a certain exploit.

    Unique identifier
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.GetValue<string>("IGC.unique");
    
    Because this communication method is essentially multicast, a unique identifier is needed to establish one-to-one communication. This command returns a string that is unique and persistent for a given antenna and so can be used for the previous purpose.

    (The intended purpose of this is for generating unique channel names that only one antenna is listening on.)

    Scanning for radio broadcasters
    Allowed blocks: IMyRadioAntenna, IMyLaserAntenna
    Code:
    block.GetValue<string>("IGC.scan");
    
    This command allows scanning the surrounding area for any radio transmitting objects.

    The returning string's basic format is a series of nul-separated fields. Each set of six fields corresponds to one detected object, with fields:
    1. (string) Name of the object
    2. (string) Set of character flags describing the object
      • 'A': Is a radio antenna
      • 'B': Is a beacon
      • 'P': Is a player
      • 'F': Is friendly (to the owner of the scanning block)
      • 'N': Is neutral
      • 'E': Is enemy
    3. (string) The configured broadcast message of the object (see below)
    4. (number) The X coordinate of the object
    5. (number) The Y coordinate of the object
    6. (number) The Z coordinate of the object

    Broadcasting a message to scanners
    Allowed blocks: IMyRadioAntenna, IMyBeacon
    Code:
    block.SetValue<string>("IGC.broadcast","message");
    block.GetValue<string>("IGC.broadcast");
    
    Sometimes you may want to provide more information to others that scan your antenna/beacon. For example, you may want to provide a channel name which they can contact you on. The CustomName of the object could be used for this, but this is messy and clutters the HUD,etc.

    Instead use this command to configure a string that will be provided to any scanners of your object. This string is limited to 128 characters and must contain no nul characters.

    Determining if this mod is loaded
    Allowed blocks: IMyProgrammableBlock
    Code:
    block.GetValue<bool>("IGC");
    
    Finally, a script may wish to optionally support this mod when present, so this command provides the means of detecting if the mod is loaded.

    Use the following function:
    Code:
    public bool isIGCLoaded()
    { return Me.GetProperty("IGC")!=null && Me.GetValue<bool>("IGC"); }
    
    Limitations/Known Issues
    One significant limitation involves the antenna network feature. When relaying between radio/laser antennas on the same ship, I can only detect when they are on the same grid. I could find no way in the modapi to find how grids are connected by pistons/rotors/connectors/etc. So, messages are only relayed to other blocks on the same grid. If you want multiple grids on the same ship to be connected, they must all have antennas. (Update: I did find a way to access the GridTerminalSystem, but I'm worried about performance as using it requires iterating of every block in every attached grid. My current method is much cheaper. That said, I'm still thinking about it...)

    Due to space concerns (both in-game and save file), the amount of message space any antenna can hold is limited to 64k. If an incoming message would cause this limit to be exceeded, the oldest messages are dropped until under the limit again.

    Due to speed concerns, range on antennas are only checked periodically. So you will occasionally be able to send/receive messages to antennas that are technically out of range. This can only happen for a second or two before a refresh happens, so I don't believe it's a significant problem.

    The mod api doesn't allow actions to be performed when a mod is removed, so the state that this mod saves will persist even after it is removed. To completely remove, you will need to edit "Sandbox.sbc" in your save game and look for "<Key xsi:type="xsd:string">InterGridComms</Key>". Remove everything between (and including) the preceding "<item>" and following "</item>" tags.

    Due to changed in the ModAPI whitelist I no longer have access to MyLaserAntenna.GetOther() and had to use a hack to and find connected laser antennas. One downside to this hack is that I can't detect when antennas disconnect (e.g., when and object obscures the connection).

    One thing I don't know about is multiplayer. I'm not clear at all what runs server-side and what runs client-side. I have no multiplayer environment to test on so I'll be curious to see if it works or not. (With the multiplayer rewrite coming, I must admit I probably won't be terribly motivated to fight bugs from the old code either way.)

    Debugging
    When reporting bugs, it will help me significantly if you can provide me a debug log. To enable logging, go to the folder "C:\Users\<user>\AppData\Roaming\SpaceEngineers\Storage" (create it if it's not there). Then create an empty file and name it (exactly) "InterGridComms.debug".

    You will find the log file at "C:\Users\<user>\AppData\Roaming\SpaceEngineers\SpaceEngineers.log". Be warned, debugging generates a lot of text and the log file will fill quickly. It would be wise to disable debugging (by exiting the game and deleting the "InterGridComms.debug" file) as soon as you have triggered the bug.

    Even more helpful would be to link a world where the bug is happening. Please try to keep the mod count down, best is only those needed to trigger the bug.

    Acknowledgements
    Thanks to @StanH for demonstrating how to extend the in-game scripting API this way.

    Thanks to @rexxar, whose pr made it possible for me to get this mod functioning again after the disastrous changes in 1.119.
     
    Last edited: May 27, 2016
    • Like Like x 2
  2. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    Getting Started
    As a simple example to demonstrate the basic concepts:
    1) Build two platforms, each with one antenna (named "Antenna"), one reactor and one programmable block.

    2) At the first platform, paste the following code into the programmable block:
    Code:
    IMyRadioAntenna ant=null;
    void Main(string argument)
    {
    	if(ant==null)
    	{
    		ant=GridTerminalSystem.GetBlockWithName("Antenna") as IMyRadioAntenna;
    		//Set up to recieve
    		ant.SetValue<string>("IGC.listen","myChannel");
    	}
    
    	string pkt=ant.GetValue<string>("IGC.recv");
    	if(pkt!=null)
    	{   //Got a message
    		string[] msg=pkt.Split('\0');
    		Echo(msg[1]);
    	}
    }
    
    3) Now run that PB once (to set the antenna up for reception).

    4) On the second platform, past the following code:
    Code:
    void Main(string argument) 
    { 
    	var ant=GridTerminalSystem.GetBlockWithName("Antenna") as IMyRadioAntenna; 
    
    	string channel="myChannel";
    	string msg="Mr. Watson – Come here – I want to see you";
    
    	ant.SetValue<string>("IGC.send",channel+"\0"+msg);
    }
    
    5) Run this PB

    6) Return to the first platform and run that PB again. The message you just transmitted should now be shown in the detailed info pane. (There is currently a bug that causes the detailed info to not update. You may have to re-select the programmable block to see the message.)

    Demonstration world
    There is also a small demonstration world which shows both communication and scanning in operation.

    If you just want to see the code used in that world, it can be seen here:
     
    • Like Like x 1
  3. Krutchen

    Krutchen Apprentice Engineer

    Messages:
    159
    Man, I've got to play around with this, and I love that you have some form of detection code included. This mod is straight to the point without a ton of bloat, and I wish more "detection" mods were like that. Thinking of putting together a short range radar & target finder to go with this later, Rnchodon's mod seems like it would work but it's overly bloated with unneeded stuff. BlueG's radar mod is good and all, but it's not being maintained, and unlike Rnchodon's radar it doesn't take line of sight into account. Bummer.
     
  4. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    Thanks! Please do let me know what kinds of things you play around with. I'm very curious about what people will think of to do with this!

    While writing this, it occurred to me that alot of the ideas would also apply to a radar-type mod (active sensing instead of passive). On the other hand, with this my goal was merely to replicate the behavior of the radio network that players have access to, so most of the questions were technical (how to do it). A radar is new to the game so there would be a lot of gameplay questions to answer too.
     
  5. Krutchen

    Krutchen Apprentice Engineer

    Messages:
    159
    Well, I could honestly see myself using this in conjunction with a lightweight radar mod to make border patrol drones & a satellite network. If one of the satellites gets attacked, or someone moves into my "sphere of influence", it would trigger an alarm/message to be routed all the way back to base, and have patrol craft respond. I've had a decent amount of the programming done already but the lack of inter-grid communication mods that work reliably was really holding it back. The alert broadcast could simply include the outpost's coordinates, and upon entering the area, the patrol ships could scan for the hostile target. I think the only problem that might arise is flight time, because as of yet I don't believe you can automate jump drives, but when we can, you can expect to see me making wings of fast response battleships for border defense.

    Speaking of which, I think after I mess around with some radar stuff after I pick apart Rhyn's ARM mod (https://steamcommunity.com/sharedfiles/filedetails/?id=363880940&searchtext=Radar) (Yes, the author is giving people permission to do this), i'll probably work on some fighters as well. One thing that I have found sorely lacking in detection mods is the ability to get a target craft's orientation remotely, so that you can trigger the appropriate attack runs and docking maneuvers. Hopefully this lack of functionality can be rectified with a bit of poking about.
     
  6. Wicorel

    Wicorel Senior Engineer

    Messages:
    1,263
    For non-mod inter-grid communications, I have https://forums.keenswh.com/threads/wico-antenna-network-wan.7372286/
     
  7. Krutchen

    Krutchen Apprentice Engineer

    Messages:
    159
    I'm not sure, but we might still lack a method of retrieving another grid's orientation, Bummer, dude. But i'm still fully intending on making some automated patrol & response vessels, regardless!

    Keenswh pls gib orientation fetching though, without it we're not going to get much along the lines of truly effective automated fighter craft. Being able to get the orientation would allow for smaller drones to attack from angles relative to the target entity, and could use player input, or if possible, data collection on each "orientations" number of turrets, to decide which angle of attack would have the lowest risk or resistance of AA fire.

    I mean, sure, you could assume that velocity = forward orientation, but that's not always the case because of maneuvers with your dampeners off. Just because we *may* know the fore and aft orientation of a ship, it still doesn't help much, because all information regarding lateral and vertical orientation is lacking, and because of the prevalence of fuckloads of turrets on capital ships along their spines, that lack of data can be deadly to any form of light drone.
     
    Last edited: Jan 10, 2016
  8. Wicorel

    Wicorel Senior Engineer

    Messages:
    1,263
    There are scripts here that give orientation of the local grid. This information can be passed to another grid using... inter-grid communication...


    Here's the code I use in my miner:
     
    Last edited: Jan 11, 2016
  9. Krutchen

    Krutchen Apprentice Engineer

    Messages:
    159
    And what of fetching the orientation of not so friendly grids? That's mostly what i'm on about. Sure, you can transfer data from one ship to another because a ship can get its own orientation, but what if you're trying to get the orientation of an enemy you're wanting to blow out of the sky? They're not exactly gonna want to give that data up willy nilly like that.
     
  10. Riverey

    Riverey Trainee Engineer

    Messages:
    5
    Hi, JoeTheDestroyer! I has an idea about modifying sound block and adding ability to change songs in it with the prog. block.
    I was talking with Digi about it and he gave me link to your mod since he believe that it's something related with what I wish to achieve.
    I copy my comment here since you wrote that you check this topic more frequently.
    Can we talk a bit about it in a PM?) I sent you a friend request on Steam.
     
  11. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    I'm sorry to say, it looks like as of today Keen has irreparably broken this mod. (Specifically, they are now denying access to Sandbox.Game.Screens.Terminal.Controls.MyTerminalValueControl).

    As far as I know, the actual radio communications component of the mod should still function. It might be possible to rework the IGC api to work through text panels instead, but I am not personally interested in such a kludge. If someone else wants to take up the gauntlet, then feel free to reuse any code in this mod that is useful.

    In the meantime, my only hope is that KSH will someday start accepting pull requests again so that an inter-grid communication capability can be added to the vanilla game without being broken every other week. Towards that end, I ask that users of the mod post their experiences to aid the design of such a system. Which parts of the mod did you use? Which parts would you have done differently? What kinds of things did you accomplish with the mod?
     
    • Informative Informative x 1
  12. DoubleCouponDay

    DoubleCouponDay Apprentice Engineer

    Messages:
    123
    This is crazy cool! Lots to read guy put in a lot of work. Sad to hear your method got blocked ;_; KSH wtf :(
    I'm guessing they don't want wizards swooping to harass noobs with their block ownership, enemy antennas global position anywhere at any time, etc. I was so cut to hear hacking got cancelled when PB first came out but ultimately it's for the best (dev's current situation).

    Working with lazertenna communication myself, the whole bits and dropped packets thing confused the jimmies outa me so I dropped it.

    Timeout is needed seperately on both ends because one antenna doesn't know the other has no LOS; only itself. I hope that makes sense!
     
    Last edited: Feb 1, 2016
  13. DoubleCouponDay

    DoubleCouponDay Apprentice Engineer

    Messages:
    123
    if it's not in the api its not moddable. Soundblock is under that category im afraid. If you're truly dedicated to the cause, fly to Prague and buy them drinks! :woot: but don't be pushy.
     
    Last edited: Feb 1, 2016
    • Informative Informative x 1
  14. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    currently only plugins can make this(without direct patching)
     
  15. Valygar

    Valygar Trainee Engineer

    Messages:
    3
    It is sad this does not work. I get that it may be dangerous, but when I started to learn SE programming and saw there was no way to access other grids... Sadness.

    It is a bit silly since you can even read LCD panels on other ships and do a sort of basic communication, but it has to be done manually.
     
  16. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    looks like you can revive this project with new terminal propertyes
     
  17. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    I have been looking into this, actually. Unfortunately, I haven't gotten a chance to play with the new mod api functions yet. It appears Keen broke a bunch of other things in the interim (they really clamped down on their whitelist) and I'm still working on figuring out workarounds for some of them.
     
  18. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    i see, i don't mod yet, so idk problem here.
    cuz i need somehow forward game first to my home PC from my own linux-server.(may be i will write small software to do this...cuz UDP forwarding not easy thing)
     
  19. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    Good news for anyone still following this project, I've managed to use @rexxar's mod api changes to get this project up and running again! There were some unfortunate changes with laser antennas which means they don't work as accurately as they should anymore, but everything else should function just as it did before.

    The main problems were getting access to the information I needed. For example, I used to use MyLaserAntenna.GetOther() to find the other end of a laser antenna connection. It was cheap and I could rely on it to return null if, for any reason, the antenna disconnected. Unfortunately, sometime since Feb they blocked access to MyLaserAntenna. Now I have to use a hack where I use TargetCoords from one antenna and look for a grid/block at that location (and check that the laser at that location is pointed back). Plus there's no way for me to query whether the connection is actually established or know when it has been interrupted.

    It would help if there were a good list of what's in the whitelist. I did at one time manage to find the code that handles the whitelist, but couldn't make heads or tails of what was trying to do.
     
  20. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    i can look into insides to say that
     
  21. Harrekin

    Harrekin Master Engineer

    Messages:
    3,077
    Use FTP or SSH...

    Easy as pie.
     
  22. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    FTP is file access.
    SSH can forward only TCP traffic.
    not so easy.
    but i already done it via VPN.
     
  23. Harrekin

    Harrekin Master Engineer

    Messages:
    3,077
    I was under the impression that you were looking to transfer files.

    I meant SFTP, not just encrypted communication.
     
  24. FangFu

    FangFu Trainee Engineer

    Messages:
    1
    Hello,
    it is possible that you remove the massage when the modes are loading before the game starts?
    Your mod works grade in stable !!!!
     
Thread Status:
This last post in this thread was made more than 31 days old.