Scripting OCTGN

As hinted previously, I am spending all my efforts into integrating a full scripting language into O2. The possibilities it enables are so amazing that I’m stopping any other development until it’s completed. It provides new solutions to a lot of current problems / challenges. I’ll surely show a few awesome examples in the weeks that come.

A few questions some of you may ask:

What about the XML scripts?
I have to say that I was never satisfied with the XML scripting. Way back when we started O2 (was that 2004?) we considered ECMAScript (aka as javascript in browsers, or actionscript in Flash). It’s a nice standardized scripting language.

But O2 has some hard technical requirements. I won’t go too much into details but scripts have to be suspended during distributed operations (such as shuffle, reveal, or random) and resumed later with the result. Moreover a lot of operation have to take place on the main thread (for reasons I won’t go into here).

Until today I didn’t envision a technical solution to those problems, other than running my own interpreter. Because interpreting a full language such as javascript is hard and time-consuming, XML scripts were born. A specific language created explicitly to manipulate cards.

So what’s the problem with XML script now? Doesn’t it work?
Yes, it does! It works, but I wouldn’t say it doesn’t have problems.

The most important one being that it’s very limited in what you can do. Of course, I could extend it (and I do) to expose more functions.. but it takes a lot of time and it will never be as good as javascript, ruby, python or perl.

The second problem is already mentioned in the first: it takes me too much time to maintain my own scripting language. I’d like to focus on O2 gaming features, rather than creating a language (which is fun, but that’s not O2’s main point).

Third, XML is awkward to write. It’s kinda OK inside the game definitions, but I don’t really see that widely spread. I now have a scripting console inside my version of O2 and I can’t tell you how wicked cool this is. With xml it would just have been impossible.

Finally, it’s something people have to learn and it’s neither fun nor really easy. There’s lot of online documentation for existing languages and some people may already know Python. Yeah Python is the language that’s going to make it.

Why Python and not XYZ?
Depends on XYZ. I may have used another language, indeed. But Python satisfies my own requirements:

1. It has a compact syntax, relatively easy to read and understand.

2. It has an extremely rich set of features, especially generator expressions. This is going to be useful when manipulating lists of cards (i.e. the equivalent of xml card selectors). E.g. to select all cards from the table controlled by me and that are Creatures (this is probably not the real final syntax, just an idea):
(card for card in table
      if card.controller == me
      and card.type == ‘Creature’)

3. It’s easy to integrate with .NET. ‘nuff said

4. It lends itself very well to interactive evaluation. This point is important because I have added an interactive scripting console to OCTGN. This is probably one of the coolest things happening to O2 this year.  I’ll show you awesome examples of this later.

Is there really no drawback to that? Can’t I use the console to cheat?
You won’t be able to cheat with that console. You wouldn’t be able to cheat even if you modified O2 source code. The protocol between your client and your opponent’s is designed in such a way that cheating is impossible without you noticing (as long as you use the official client, at least).

But I am concerned by your security. Python is a full-featured language, which can be used to do anything. Steal your personal information, install a virus on your machine…

I appreciate that you trust me when you use OCTGN, but I don’t expect you to trust every game definition or Python script you are going to download. So to protect you I am executing Python scripts in a very restricted sandbox where they can do no harm. This is good for you, but the sandbox restrictions are still causing me a few problems with O2’s own legitimate use of scripting… That’s the main technical point remaining to fix.

What about existing games? Should they be redone?
In the end, yes. I am going to ease the transition by allowing both scripting engines to run side-by-side for a while. But later I’ll completely remove the XML scripts. So authors have to update their games. I am pretty sure they’ll be happy to when they see the full potential of the new system.

Next time: a demonstration of the console!

Explore posts in the same categories:

21 Comments on “Scripting OCTGN”

  1. beast Says:

    As a fan of OCTGN2 and Python I can’t wait to try this out. Good choice.

  2. Richard Jarrell Says:

    I know a bit of python. I learned it to mod CIV4. I am excited about the change.

  3. pkt-zer0 Says:

    Huh, I guess I’ll scan through a Python tutorial in the meantime. Proper scripting will be quite handy, I imagine. Subroutines alone should make life a lot easier. Partial rule enforcement seems way less crazy an idea now than it did when it involved massive XML tree structures.

  4. brine Says:

    once you get the new language up and running, I’ll try and get the magic game def converted to it as soon as possible.

    I’m just a little worried about the set files though. Having to have everyone go through the entire MTG patching process again, especially after the craziness that ensued from the current one. It’s not as big a problem with the game def, but it may be a good idea to keep a portion of XML available for the set files (if possible) until another mass-patching is required

    • jods Says:

      Today sets contain no code. They aren’t affected by that change.

      Notice that I’m still calling O2 an alpha precisely because I think some mass changes may be ahead of us.

  5. Bruno Cardoso Says:

    Are you going to try script all cards so you can have a rules enforcing engine ?

    • kristijanH Says:

      I’m all for progress but… there is always a but 🙂
      The state of o2 curently is not ok. We had a major update in may and almost 2 months later still not all sets are updated. If you start another mayor change when can we expect the sets to get updated? 6 months from now? A year? Call me pessimistic but I’m worried about another big change.

      • jods Says:

        This is a big new feature, not so much of a big breaking change.

        Sets are NOT impacted and no update is required.

        Game definitions will be compatible and continue to work. They will need to be updated to take full advantage of the new possibilities. But updating games isn’t as big a problem as updating sets is.

        Regarding the previous big change… I know all sets haven’t been updated with complete information, but they have been made compatible with the new game definition.

        What I mean is that the situation isn’t worse today than it was 3 months ago. Some sets are undeniably better, the rest is exactly the same. (Although it’s more obvious today that the data quality isn’t great.)

      • brine Says:

        only the extended sets (minus time spiral) and a few others are compatible so far

      • Oen Says:

        The sets are maintained by another group of people, not jods. You are welcome to redo all the sets yourself, then they will be done as soon as you get them done. 🙂

    • jods Says:

      No, rule enforcing isn’t a goal of O2.

  6. byakuichi Says:

    Sorry I’m new here and i have some questions :
    I wanted to know whether there was a place or find the source code of the software?
    If the version I have now is good (
    Other editions are planned for Magic? Because I can do if necessary I looked at the structure. O8s and it’s pretty simple. Just asking how we generate the ID of publications and maps that make up these editions?

    • jods Says:

      Hi and welcome.

      Yes, there is. The source code is available on sourceforge.

      0.7.6 is the latest release. The software updates itself automatically anyway…

      Regarding Magic I don’t really know, but I think there’s support for all (or nearly) editions. There’s some inconveniance right now because they’re reworking all the old sets, but in the end it should be OK.

      IDs are GUID (globally unique identifiers). You can use any tool, database or programming language of your choice to create them (but please don’t make them up, they wouldn’t be guaranteed to be unique).
      If you don’t know where to start, try that website:
      It can generate lots of them quickly.

      If you have more questions I suggest you register on our forums:
      There’s a lot of nice and helpful people there.

  7. urikkiru Says:

    Hrm, I have to admit I’m a little uncertain about the choice of allowing embedded python scripts to run in the app. There have been a lot of vulnerabilities in recent years involving such things that *should* have been secure/sandbox’d properly, but weren’t.

    • jods Says:

      I wouldn’t worry too much. The isolation is built-in .NET, as the scripts run in a distinct and restricted AppDomain.

      This is the way .NET was made safe for running untrusted code since 1.0 (2002) and I haven’t never read about security failures in that design.

  8. Quaeras Says:

    Thank you for your work on this. I don’t believe this gets said enough.

  9. Kurt Says:

    Is there going to be any releases soon for OCTGN to fix some bugs? I’m getting the one I posted in the bug tracker constantly now. =(

  10. Stephen Says:

    This is fantastic. Thank you jods. You’ve outdone yourself yet again.


  11. Ben Says:

    It’s been a while since the last post. How are things going?

  12. No updates in over three months!! I feel like trying to implement this killed the project.

    The program is really solid and well coded. Is there any chance any remaining bugs/missing features can be ironed out completely and the project can atleast be upgraded to a beta status.

    I would love to see a final version of the program be released in an year or so with all the features Magic Workstation offers but none of the crap/hassle.

  13. Sorry, I should explain my username.

    I frankly think the current OCTGN logo, the use of a slanted octagon shape looks ugly and offputting in an otherwise beautiful program.

    It would be awesome if the program used a regular symmetrical octagon shape instead of a slanted octagon as it’s official logo.

    It should be fairly easy to do.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: