Galaxia

Scripting in Galaxia Reborn

Oliver Brown
— This upcoming video may not be available to view yet.

Introduction to XGS

This won’t sound that impressive since noone has seen any of GR working but I have scripting working at a minimal level in Galaxia Reborn.

I gave a ship the following script:

<xgs> <script event="entersector"> <savetomemory name="w" value="($w+1)%4" /> <setcourse target="$waypoints.$w" /> </script> </xgs>

$waypoints is simply an array of four coordinates. The function savetomemory just saves data to the ship which is read back in when the script next executes.

Working user scripting

Oliver Brown
— This upcoming video may not be available to view yet.

The user scripting I mentioned before I went to Brussels is now basically done. The most amazing part is that I did most of it while I was in Brussels on a computer without PHP (amazing because apart from a few minor typos it actually worked).

The whole thing is actually simpler than I expected it to be and as such it can do stuff I didn’t plan. The first is maths expressions. This is something that may cause a problem however since it just just evals the code (after substituting variables and stripping possible harmful stuff).

The coolest feature I think though is handling aggregate data types. When you call a script you also pass it an array of variables. These variables can be of any type from integers or strings to arrays or objects (or even resources); it’s upto the functions you define to actually handle them. Now different elements of arrays and properties of objects can be accessed using a slghtly suspect-looking dot notation. So (from a working example I wrote) you could use something like $fleets.1.name which would access the name of the second element of the fleets array.

It’s worth pointing out that I decided to stick with numbering arrays from 0 since as a programmer that’s what I’m used to.

Anyway when I say that is accesses the name of the second element, it checks the type of the element and accessed it as an array ($fleets[1]['name']) or a property ($fleets[1]->name) as appropriate.

You can also do $fleets.$n.name to access the name of the nth element (although be warned that if n doesn’t reference a key of the fleets array then it won’t work obviously). Which brings me to biggest problem with it at the moment… everything fails silently if something isn’t right.

The way the scripting is called is also more flexible than I originally planned. The first way is:

$xgs->parseXGS('event', 'fleetentersector', $script, $vars);

$script is the a well formed XML script and $vars is an associative array of variables. Any script tags with an attributes of event equal to fleetentersector (<script event="fleetentersector">). This is was done mainly with Galaxia in mind so players could create different types of scripts other than ones designed to respond to events (i.e. use it just to create a batch command system).

The second far more flexible way to use it is:

$xgs->parseXPath($xpath, $script, $vars);

Where $xpath is an XPath expression selecting the root node of the script. Just to point out the first example actually just calls that with XPath expression "script[@$type='$att']" ($type is the attribute name and $att its value).

User scripting in Galaxia Reborn

Oliver Brown
— This upcoming video may not be available to view yet.

Something I’ve wanted to put into Galaxia for a long time is user scripting. i.e. allowing users to attach scripts to objects that are triggered by certain events. Ultimately this could lead to interesting computer players (but that’s a long way off).

Well I’ve been playing around with it a little and the easiest complex data to parse seems to be XML. So the scripting engine will be XML based. The following is how a script will (hopefully) look:

<onFleetEnterSector>
  <IsEnemy target="$fleet">
    <ExectureOrder order="attack" target="$fleet" />
  </IsEnemy>
</onFleetEnterSector>

Just in case you can’t tell what it does, when a fleet enters the same sector as your own it checks to see if is an enemy and if is, attacks it.

Firstly a bunch of variables the script can read are passed to it. After that all the processing works using callbacks. When each tag is encountered, it just calls some specified function with the attributes passed as arguments (probably as an associative array to easily handle a variable number). If it’s a conditional (like “IsEnemy”) the function must return true or false. If it’s an action (like “ExecuteOrder”) then it returns nothing. There is also another type - one that returns an arbitrary value (i.e. a function - but I’ve used the word function too much already).

I’ll put the code up once it’s tested a bit more.

I quite like JavaScript

Oliver Brown
— This upcoming video may not be available to view yet.

For a long time I hated JavaScript. I think I’m beginning to like it now. The following works perfectly in Firefox and works slightly less well in IE6. http://brownab1.miniserver.com/reborn/demo.php.

To use just click and drag. Or in IE6 press the button, drag then press again.

Ooooh, pages…

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve added the second chapter of my story, Reactions.

The story by the way is my attempt to write a time travel short story with no paradoxes (or should that be paradocies?) in it.

I’ve also added some info about Galaxia Reborn. I must remember, whenever I lose the urge to write Galaxia, just go look at Planetarion and think “I could so do better”.

Objects

Oliver Brown
— This upcoming video may not be available to view yet.

Almost everything in Galaxia is an object. In hindsight this was a bad choice of name (from a programming standpoint I have objects of type Object), but I’m basically stuck with it. And I still can’t think of a better name.

The most interesting type of objects in the game will be vessels. The reason I chose vessel over ship is because not all vessels will actually be ships. Space stations for instance will be vessels that don’t have engines.

Vessels will have components and the components will add most of the functionality. I can’t tell you exactly what these will be or how they will work since this is the next big feature I need to implement. But basically, each component will allow a vessel to be given certain orders. For example engines allows a vessel to move somewhere, guns allow it to fire on other vessels.

The exact makeup of each vessel can be chosen when you build it. This also answers a question that has been causing me problems ever since the original Galaxia: “Why build multiple shipyards?” Well building some of the more exotic components will require some extra facility (Widget Factory for instance) therefore it would be better not to build one with every shipyard (you won’t have room anyway).

Galaxia Reborn

Oliver Brown
— This upcoming video may not be available to view yet.

This will hopefully become a nice place for all the info I currently have on Galaxia Reborn.

I realised putting stuff in normal blog posts was silly since the GR info isn’t really supposed to be time sensitive and once it falls off the front page, former Galaxia players who stumble over here won’t be bothered to find it.

The current version is now available: Galaxia Reborn Login with “Q” as your username and “q” as your password.

Should Galaxia use Ajax?

Oliver Brown
— This upcoming video may not be available to view yet.

I used JavaScript once succesfully and it goes to my head…

I read a book on doing XML stuff with JavaScript and thought “Very clever. But Why?”. Well apparently it’s taking off. It’s how Google Maps works.

And if you didn’t know (I didn’t; reading blogs is actually helping me) Ajax stands for Asynchronous Javascript and XML.

It would mean you’d just drag the map to scroll in Galaxia… :D

In hindsight. No. Too much effort and too many other things I need to do…

Users in Galaxia

Oliver Brown
— This upcoming video may not be available to view yet.

Well one can now create an account and login. So far it doesn’t give you anything but it does respect users with regards to what object orders you can perform. i.e. you can’t rename objects that aren’t your own.

An idea for Galaxia

Oliver Brown
— This upcoming video may not be available to view yet.

It just occurred to me (not strictly true but we’ll let that go) that I may not get the current version of Galaxia finished. This time it really would be a shame since the code in it is actually quite good. So I’ve come with an idea I would like feedback on (which will be an achievement considering the attention this site gets).

I might open up Galaxia at least partially. Nothing fancy like a public CVS, but the generated documentation complete with annotated source listings. This will obviously impact security of the game once it’s available to play but it might be worth it since it opens up lots of possibilities:

  • People actually writing stuff for Galaxia. There are lots of places people could write little bits of stuff for me that aren’t hard to do but do take time.
  • The very fact that people will be able to see the code will make me write it better and clearer and with better documentation.
  • If people are interested I might finally write a tutorial on some MPOG-writing-in-PHP topic which I’ve wanted to do for ages.

Anyway if I by any chance you are reading this and are interested (of course you’re reading this) post a reply…