Trosnoth: Mercurial server

Myself and the other Trosnoth developers have been looking at moving from subversion to mercurial for our source code control. I’ll blog about some of my adventures doing the conversion later, because today I want to talk about the mercurial server I’m setting up.

I’m setting up a computer to host our central repository of Trosnoth code. For now it’s just running a simple mercurial server. What I want it to do eventually is:

  • run all our unit tests on the default branch whenever a developer pushes code (unless the tip of default doesn’t change—this allows developers to push broken code in branches but not in default);
  • if any unit tests fail, decline to accept the push;
  • generate code coverage reports for the test results;
  • if any source file has had its code coverage percentage decrease from the previous revision, decline to accept the push (with some minimum percentage for new files which are pushed); and
  • if a developer tries to push while another developer’s changes are being tested, queue the new changes and test them once the last test run is finished.

I’ve done some preliminary research and I think that this should be possible. Here’s how I plan to do it:

  • write a pretxnchangegroup hook in the mercurial repository, which:
    • uses hg bundle –base to create a bundle of the changes,
    • uses the bundle-testing script to queue testing of the bundle,
    • prints a message to the user, and
    • refuses to accept the changegroup transaction;
  • write a bundle-testing script, which goes through every queued bundle and:
    • clones the repository,
    • applied the bundle to the new test repository,
    • does an automated merge if needed (e.g. if two bundles were queued and one has been accepted, therefore the second one will create a new head),
    • build a source distribution from the code,
    • install the source distribution in a test environment,
    • run the unit tests,
    • if any of the above steps fails, send an email to the user who did the push, indicating the reason of the failure,
    • otherwise the bundle is accepted, so apply it to the real repository.

Overall, this should improve the code quality of the program.

Posted in midlength | Tagged , , , | Comments Off on Trosnoth: Mercurial server

Refactoring Reflections

I have been working recently on Piratenoth, a release of Trosnoth which defaults to using a pirate theme. For those of you who haven’t yet clicked on the Trosnoth link above, Trosnoth is a fun 2D team game. We’ve been updating the game so that rather than being hard-coded, things like game colours and graphics are different depending on what theme the user has selected.

This has been a fun exercise in refactoring, but it highlighted for me something which has always annoyed me about the Trosnoth code: attribute chains. For example:

self.gameInterface.detailsInterface.newMessage(msg)

Some (but not all) of these attribute chains come about due to the tree structure of the objects in the game: the player interface is the child of the game interface which is the child of the details interface and so on. And it got me thinking of a creative new not-quite-OO way of doing things.

Imagine a programming language where every function is executed in some context, where the context is represented by some object. The context is accessible within the function by some name like this, self or context. The context object is also allowed to have a parent object and a name associated with that parent object.

Now here comes the fun bit: within a function, you can access any ancestor of the context object, using the name of that ancestor. If there are more than one ancestor with the same name, the most recent ancestor would be returned.

So let’s apply that to the example from Trosnoth above. Suppose we have the following context objects:

  • app: represents the application
  • interface: child of app
  • detailsInterface: child of interface
  • gameInterface: child of detailsInterface
  • playerInterface: child of gameInterface

Now if a function is executed in the context of the player interface, it would be able to just say:

detailsInterface.newMessage(msg)

The name “detailsInterface” would be searched for up the ancestor chain, and we’d eliminate a whole bunch of the chained attributes. And it would eliminate the need to pass the application object to every constructor (as we currently do) because it can be accessed implicitly through the ancestor chain.

There would need to be some way of selecting the context in which to run a function. For example:

self.doSomething(parameter1, context=newContext)

And there would be other considerations too. The designer of such a language would have to decide whether ancestor names should have precedence over global variables or vice versa. And it might lead to a different programming approach which could be interesting.

I don’t plan to actually use this approach in Trosnoth. We already have a different approach for avoiding the ugliness of attribute chains, and we’re slowly updating our code to avoid such ugliness. But I may have a bit more of a play with this idea in the weeks to come. And it gives me all sorts of other related ideas about programming language design. So much fun to be had!

Posted in midlength | Tagged , , , , | Comments Off on Refactoring Reflections

Election Promises

I was thinking recently about politicians and election promises. It seems that no matter who gets elected, there’s always at least one promise which is made before the election, but gets neglected once the politician is in office. So I started to imagine what it would be like to live in a country where politicians are not allowed to make public election promises unless they’ve signed a written copy of the promise. If they break the promise they are personally liable for penalties due to breach of contract.

Of course such a system may end up causing politicians to never promise anything because if they do, a vindictive opposition could block legislation in the upper house just to penalise the promise-making politician. But it would encourage politicians to think twice before making rash promises.

Posted in short | Tagged , | Comments Off on Election Promises

Composition: First Steps

In my previous post, I wrote a bit about the conventions of western music and my desire to compose music which doesn’t follow an equal-tempered scale. Well I’ve had a bit of a go at doing this.

Approach

In composing this piece, I didn’t throw every convention out the window. I started by setting myself the goal of composing 8 bars of 4/4 music for two voices, one being the melody and one the accompaniment. I decided to make the accompaniment follow a fairly standard Alberti bass, but using just intervals. I stuck to a major triad in most bars, even when the convention would be to use a minor triad. I then composed the melody line, trying to make it fit nicely with the accompaniment. Of course when I say “fit nicely” I mean that I used just intervals once again. Where needed, I adjusted the frequency in the accompaniment slightly to make the frequency ratios slightly nicer. Beyond that, I pretty much went with what sounded nice to me and I made sure to finish with a perfect cadence for good measure.

I realise that by choosing a melody which sounds nice to me, my melody will be influenced by my own training in conventional western music, but I had to start somewhere, didn’t I?

Download: Synthesised Audio File

Creative Commons License
First Steps by Joshua D Bartlett is licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License. By downloading it, you are agreeing to this license. Click here to download the audio file (400kB zipped .mp3).

Download: Synthesiser Source Code

To generate the synthesised audio file above, I wrote a Python program. This program is licensed under version 2 of the GNU General Public License. By downloading it, you are agreeing to both this license and the license described above, which the work of art itself is licensed under. You will need Python in order to run the program. Click here to download the source code (18kB .tar.gz).

I’ll make a note of any updates I make to this piece, or any more compositions I make along the same lines on this page.

Posted in midlength | Tagged , | Comments Off on Composition: First Steps

“Impossible” Music

A recent discussion with a colleague reminded me of an idea I had some time back: composing non-equal-tempered music. This is not a new idea, it’s just something I’ve never delved into much before.

Quick Intro to the Physics of Sound

Sound is caused by vibrations. Whenever you make a sound, some object or part of an object has to vibrate. This could be, for example, a metal saucepan, a guitar string, or your vocal chords. For you to be able to hear a sound, the vibrations have to reach your ear drum. The vibrations travel as a wave from the source of the sound to your ear. Sound waves travel through air, but also travel through just about any other medium you can think of (ok, so a perfect vacuum doesn’t count as a medium ;-).

Quick Intro to Musical Notes

Musical notes are periodic sound waves. The volume of the note is related to the amplitude of the wave and the pitch of the note is related to the frequency of the wave. A greater frequency corresponds to a higher pitch and vice versa. The interval between any two musical notes is related to the ratio of the note frequencies. For example, the interval of an octave corresponds to a frequency ratio of 1:2. The note which many orchestras use for tuning, known as concert A, has a frequency of 440 Hz. So the A an octave higher has a frequency of 880 Hz and the A an octave lower 220 Hz.

The Equal-Tempered Scale

Most instruments used to play western music today are tuned to a scale known as the equal-tempered scale. Basically, this means that every octave is divided into 12 equal semitones. (For the mathematically minded out there, the interval of one semitone corresponds to multiplying a frequency by the twelfth root of two, or by about 1.059.) This method of tuning instruments results in a very versatile scale, where a piece of music may be transposed to a different key, and still sound essentially the same. The only drawback is that music doesn’t sound quite as nice as it could. Which doesn’t really have to be a drawback, unless you’re a purists.

Just Intonation

Two notes generally sound nice to the human ear if they are separated by an interval called a “just interval”. This means that the ratio of the frequencies of the two notes can be expressed using small numbers. For instance, the frequencies 440 Hz and 660 Hz are related by a ratio of 2:3. These notes sound nice together. In fact this interval has a name: a perfect fifth.

If you’ve followed everything so far, and you’re both mathematical and musical, you might have discovered a problem here. Musicians would be aware that a perfect fifth is equivalent to seven semitones. And mathematicians would be aware that if you multiply 440 Hz by 1.059 seven times you don’t get exactly 660 Hz. You’re off by about 2%. What’s going on?

The truth here is that the even-tempered scale sacrifices these just intervals in order to have the nice property of every semitone being equal. So when you play a perfect fifth on a piano, the interval you’re hearing is not quite a 2:3 ratio. But there are methods for tuning instruments which ignore the modern conventional scale in order to make things sound nice. And even putting tuning aside for a minute, I bet many musicians subconsciously adjust their pitch to fit with just intervals when singing or playing an instrument where small pitch adjustments are possible, such as a string instrument or the trombone.

“Impossible” Music

Which brings us back to where we started. Other musicians have already tried their hands at this. Basically, I want to compose music where I care more about how the piece sounds than about following a conventional scale. A computer will help me immensely by synthesising notes that are not constrained to the even-tempered scale. And when I’m done, musicians will probably find it easiest to listen to their part and play by ear. If I come up with anything decent, I shall post it on this site.

Posted in midlength | Tagged , , , | Comments Off on “Impossible” Music