Saturday, February 15, 2014

Connecting to the Creatures DDE interface from python

We've recently seen how easy it is to connect to the C1 and C2 DDE interfaces using VB.

This time we will show how easy it is to connect to those interfaces using python, which will prove useful in upcoming articles.

Connecting to a DDE server from inside python is can be a quick and easy process.
The hardest part is in acquiring the correct modules to do so, (which can be a real pain if you take it from the wrong side), there are also a couple specifics that make debugging the first DDE connections unreliable.
Fortunately I've sorted those perks out for you,and will show you the single quickest and easy way to connect to the Creatures 1 and 2 DDE interface from python.

The "python for windows extensions" aka "pywin32" is the module that holds the necessary DDE connectors for us.

To install it be sure to grab a prepackaged binary copy  of pywin32 for your operating system that will save you from the painful process of having to manually install obscure dependencies.
That's it :)

From on now, connecting to the Creatures DDE interface becomes as simple as :

import win32ui
import dde

def SendCommandToGame(command,ExpectedResult=False):
    This function sends a CAOS command to a C1 or C2 game and returns the result
    You have to specify whether or not the executed command expects output to be returned, as both kinds of commands require different calls
    server = dde.CreateServer()
    server.Create("TestClient") # This can be anything or even empty.
    conversation = dde.CreateConversation(server)

    conversation.ConnectTo("Vivarium", "Anything") # Creatures DDE application is "Vivarium", yours can be anything.
    conversation.Poke("Macro", command+"\x00") # Rembember Creature expects C-style \0 terminated strings as DDE inputs
    if ExpectedResult==True:
        # If the command returns a result use the Request() method
        # If the command doesn't a result use the Exec() method
    return rep

# Now we can run arbitrary CAOS commands :
print SendCommandToGame("dde: getb cnam",True)

Easy wasn't it ?
You can grab a working example copy of this file on the SLK examples github page.

The only things you should keep in mind are :
- Use the binary (unofficial) packages to install the DDE module
- Be sure to terminate the strings you send to the DDE "Macro" topic with \0 bytes.
- Unfortunately due to limitations in the python DDE module of pywin32, you'll have to use two distinct methods to send your CAOS commands depending on if they output something back or not.

In upcoming articles we will use this ability to automate most of the tasks we've seen so far, along with a couple others such as :
- Automatically mapping out a Norn's internals using graphviz ( a list of all organs along with all the chemical reactions happening in them :)
- Drawing a fully detailled realtime brain activity map
- Drawing realtime C2 ecology maps
- Many others

Drop me a line if any if there'are other things you'd like to see

No comments:

Post a Comment