Wednesday, November 11, 2009

Blogging velocity has slowed somewhat. Probably because there was so much code to post on my Rx Framework attempt I ran out of gas, then we had a baby. So bugger it, I wont post nothing on it and continue as if I wasn't going to.

So, I'm on the deck bbqing some chicken with a beer and laptop.  What to post?

I'll reflect on work. Fun times.

Over the last several years I've been building an Ajax UI framework for porting Windows applications to the web.  The first version only worked in IE and I've been working on the next version which will, theoretically, work in all browsers. But now a client is paying to have the first version work in FireFox and Safari as well.

I can't say I mind this too much and it (touch-wood) is turning out to be easier than I though.  Although I realise the devil is in IE6.

First step: Consistent look in IE7+, FF and Safari. Get everything looking the same- it doesn't have to be pretty but if all browsers start with the same foundation that isn't too bad, it can be improved from there.

This turned out easier than I thought by using the strict doctype and a good CSS reset. Removing any -moz- styles and the odd JavaScript fix for FF.

Second step: Consistent behaviour.

Third step: Carefully get it working in IE6.

Fourth step: Make it pretty.

My chicken is burning.

Sunday, July 19, 2009

.NET Reactive Framework

In our game, stories are first-class beings -  invisible meddlers. They move around, observe and manipulate the world according to their plot. They wait for triggering conditions or patterns of events to occur, and then spring to life to advance the story.

In our case, we have an evil story waiting for the betrayal of a paladin. When this happens, the story will unravel a series of events, involving a horror, a child's toy and a man with a poisoned name.

How does the story listen out for the betrayal? Our game follows an event-driven design so we'll need to do some event-processing.

I recently saw Erik Meijer's presentation on .NET's up and coming Reactive Framework (Rx), which looks pretty cool and the right tool for the job. There are already a couple of other reactive programming frameworks out there, like Continous LINQ and Reactive LINQ, but I thought it would be interesting to see what's involved in writing one, so here we go.

Rx is the mathematical inverse of IEnumerable, dubbed IObservable. So instead of being able to iterate over a stream of objects you get to listen to a stream of events.

No big deal really, but the cool thing is that it supports Linq, so you can query over event streams.

IObservable<ICombatCommand> paladinFleeCmds =
  from cmd in CombatMediator.CombatCommands
  where cmd.CommandType == CommandType.Flee &&
        cmd.Subject.CharacterType == CharacterType.Paladin
  select cmd;

paladinFleeCmds.Subscribe(cmd =>
  {
    IStoryPoint betrayal = new Betrayal(cmd.Subject);
    story.RegisterStoryPoint(betrayal);
  });

I'll post how to implement this kind of Linq-to-Events shortly.

Sunday, June 28, 2009

db4o - an Object-Database


Versant have come up with an object-database that is very easy to use.


In some cases, performance might not be there when you need it, but when this isn't a problem the benefits easily stack up.


const string dbFile = @"C:\FearlessOnes\db\Monsters.db";

IMonster monster = new Horror("Atlach-Nacha",
"Spinner in Darkness");

// If the database doesn't exist, it will be created
using (IObjectContainer db = Db4oFactory.OpenFile(dbFile))
{
db.Store(monster);
}

Nice and easy. Linq is also supported,


using (IObjectContainer db = Db4oFactory.OpenFile(dbFile))
{
monster = (from IMonster m in db.Query(typeof(IMonster))
where m.Name == "Atlach-Nacha"
select m).Single();

monster.Mode = MonsterMode.Hungry;
db.Store(monster);
}

Just reference one DLL, or two if you want to use Linq, and you're smiling.


A great tool for developing the domain layer, even if you plan to use a relational database later. Download it here. The documentation is also very good.


Inversion of Control (IoC)

In our game, a monster wakes up because some players have entered its domain. It's in a dark corner, so it waits for the players to pass by and then lunches them.


Assuming the flow of control starts with the monster object, how does it communicate its intentions to the combat system?


1) The direct approach would be for the monster to create and reference the combat system.



public class Monster : ICreature
{
private DefaultCombatSystem combatSystem =
new DefaultCombatSystem(); // Bad

protected void GetUpToNoGood()
{
//...
ICommand command = new LunchPlayers(players);
IResponse response =
combatSystem.HandleCommand(this, command);
//...
}
//...
}

But this comes with a raft of problems. The least of which is when the combat system changes, the monster will need updating.


2) A better approach is to refer to an interface for combat systems rather than a particular implementation. This will save on the refactoring. The monster can also ask a factory for the combat system that implements this interface. This adds a dependency on a factory however, which is not ideal.


So instead we inject the combat system into the monster. This is known as dependency injection and removes the monster's dependency on a concrete combat system or factory.




public class Monster : ICreature
{
private ICombatSystem combatSystem; // Better

// Constructor injection
public Monster(ICombatSystem combatSystem)
{
this.combatSystem = combatSystem;
}
//...
}

This would be perfect for things a monster does depend on, like its stomach, but the combat system doesn't belong here.


3) An even better approach is to expose an event that the monster fires. This frees the monster from combat system concerns altogether, so it can concentrate on what monsters do best.




public delegate void CommandHandler(ICommand command);

public class Monster : ICreature
{
public event CommandHandler Command; // Best

protected void GetUpToNoGood()
{
//...
ICommand command = new LunchPlayers(players);
Command(this, command);
//...
}
//...
}

Wiring up the event,




public class DefaultCombatSystem : ICombatSystem
{
private IList<ICreature> creatures;

public void AddCreature(ICreature creature)
{
creatures.Add(creature);
creature.Command += OnCommand;
}

public void OnCommand(ICommand command)
{
// Handle the command
}
//...
}

This is the observer design pattern using events.


Control has shifted to the combat system now. It references the monster and will make the required calls to resolve the combat. This is known as inversion of control (IoC) or the Hollywood principle - "Don't call us, we'll call you".


In hindsight it's easy to see we had things backwards from the start, but the direct approach is all too common. It's a hangover from procedural programming where the programme's flow of control acts directly rather than setting up an object model and then handing over control to one of these objects.


In this way, the combat system and the monster are much easier to maintain, test and debug.

Friday, June 26, 2009

Disputes Tribunal Ruling

I found the ruling from the tribunal. Here's the guts of it (I don't have a scanner):


The Tribunal hereby orders

Sony Computer Entertainment New Zealand Ltd and Electronics Boutique Australia Pty Ltd are jointly and severally liable to pay $375 to Mr Stevens within ten days of the date of this order.


Reasons


[snip]


It was proved that the Mr Stevens' playstation was not of acceptable quality:

  1. An article from the Consumer magazine (a respected organisation) in July 2006 was reliable evidence (based on surveys, information from manufacturers and retailers and their own experience) that a reasonable life expectancy for appliances was five years for computers and 8+ years for DVD players (both playstation functions).
  2. No appliance had a life expectancy of less than two years and there was no reason to suppose that playstations were less durable than other appliances, in particular when playstations were used for games and received hard treatment from children.
  3. Mr Stevens' playstation, by failing within two years, was therefore not fit for all of its purposes, not free of minor defects and not durable.
  4. It was not an answer that (as stated in the article) the life expectancy of appliances was getting shorter... because this did not account for difference between the stated life expectancy (eg computers, five years) and the failure in Mr Stevens' case (under two years).
  5. It was not an answer that the technological complexity of playstations meant that failure of a small percentage of units was "inevitable" (letter to Mr Stevens) if there was nonetheless a failure of the guarantee of acceptable quality.
  6. It was also not an answer that Mr Stevens' warranty was limited to one year as it was not possible to contract out of the Consumer Guarantees Act in non-business transactions (section 43).

[snip]


There were five arguments I raised, but only two were accepted. I can post them if anyone's interested.

Friday, May 29, 2009

PlayStation 3 Durability - The Court's Ruling

My PS3 broke down outside of warranty, but by New Zealand law, the Consumer Guarantees Act says that goods should last for a "reasonable" amount of time. I didn't think 21 months was reasonable for a PS3 and since Sony refused to pay for the repair, I took them to court and won.


What won it for me is the Consumer article, which said that computers should last for 5 years and DVD players for 8+ years, both PS3 functions.


They tried to argue that the PS3 wasn't classified as a computer, however there is no reason to suppose that PS3s are any less durable since they are used for games and receive hard treatment from children.


So the Disputes Tribunal ruled that my PS3 was not of acceptable quality and ordered Sony to reimburse me the repair cost.


Anyone is more than welcome to use this case as an example if they have a similar dispute. The case number is CIV-2009-094-000642. Take 3 print outs of the Consumer article.


It cost me $30 to have the hearing and all information you need is on the Disputes Tribunal website. Note that you will need the name and address of the legal entity you're taking to court, which can be found at the Companies Office. You can either take Sony or the company you purchased the thing from. I recommend the latter because it's just easier. The Sony offices are in Australia and they don't have a clue about NZ law.


Note that warranties aren't very important in New Zealand. People selling extended warranties are basically criminal, because we're already covered. See the Consumer article about this.

Thursday, May 21, 2009

PlayStation 3 Durability - My Dispute with Sony

I use to be a Sony fan-boy. Sad but true. My PS3 was my pride and joy until it broke down 21 months after purchase and Sony refused to fix it.


New Zealand consumers are protected by the Consumer Guarantees Act, which states that goods must last for a reasonable amount of time. Warranties tend to be a lot less than this, so most of them are a pointless. Some companies, like Noel Leeming, try to sell you extended warranties which is criminal - consumers are already covered.


So I'm in the court room with Sony on Monday.


What's a "reasonable amount of time"? An excellent article from Consumer, lists reasonable times for many appliance and can be used in hearing such as mine. Computers should last 5 years according to this.


It's a bit of a hassle really over $375 but I've had less that professional dealing with Sony over this dispute and it's become a matter of vengance. I'm also asking for an apology for a breach of the Fair Trading Act when one member of their staff told me that the terms and conditions of purchase overrides the Consumer Guarantees Act. The staff all had Australian accents, which might explain things a little.


Will post how it goes... fingers crossed.

Wednesday, May 20, 2009

Yayoi Kusama

I saw Yayoi Kusama's exhibit at Syndey on the weekend (while on honeymoon with my lovely wife). I haven't been so moved in an art gallery before.


The most striking piece was an ordinary living room scene you walk into. It was dimly lit and all surfaces where a dull grey. Upon each surface where many bright, primary-coloured dots and the room was flooded in blacklight. Dots everywhere really. The living room could only be recognised as such using depth perception. I felt like I was trapped in a magic-eye picture.


There was a hall with mirrored semi-spheres hanging on the walls. A floor groping with tenticles, black and white repetitive doodlings, themselves repeated to fill the room. There was a mirrored room where you saw yourself repeated ad nauseum.


Her theme was repetition and Yayoi was asking me why I'm unique. She didn't ask me in any casual sense though, she first challenged my understanding of identity.


If she were at the exhibit at the time, I would have kissed her.

Monday, January 5, 2009

Mumbo-Jumbo

Take homeopathy. It can be phrased to sounds plausible but it has not been proven to work. All that is meant by "proven" here is that there are positive results which can be reliably reproduced. It's not much to ask really - if something works then it should be demonstrable.

Disbelief in homeopathy has been suspended however, along with the orgy of opathys and ologys out there. So I presume their proponents believe more in the ideas they represent than the ideas themselves.

This new-age magic is packaged with recycled ideas like "alternative", "choice" and "nature". They're sold with free-range language like "holistic", "energy" and "karma". Belief in magic doesn't survive because there is magic, it survives because there's a demand for it.

Perhaps it comes from a distrust in our scientific and pharmaceutical institutions. Maybe people don't want their health and well being in the hands of statistics. They want hope in the face of overwhelming odds and freedom from reliable prediction. People want to believe in magic, they want to believe in mumbo-jumbo.

Sunday, January 4, 2009

The End of Philosophy

What is Philosophy?
Young children incessantly ask, "but why?" to any answer offered by their parent. This continues until the parent tires and says, "go ask your Father." Philosophy is the same. It's the art of inquiry but its art has a unique mode: it is de-creative. Philosophy breaks down concepts through their understanding until the understanding itself is questioned. The understanding is that of the inquirer though, which shows the profound hunger of philosophy's "why?" - the inquiry feasts on its subject matter and then turns on the inquirer.

But it doesn't stop there. Philosophy continues to question until it arrives at its very own activity, "why the why?", where it questions its own motives. So philosophy is always "negative towards itself." Its goal is to remove the want for philosophy. Philosophy's consummation is its own consumption, a serpent eating its tail. Its final act is the unearthing of the ultimate foundation or under-standing, the place where all questions are answered. The Garden of Eden, as it were, where the tree of knowledge is planted.


Paradise Lost
But there is no such garden. This idea is a remnant from a time we believed the world was spoken into existence - when we thought God declared, "Let there be light"; and there was light. "The Word" was identified with what is; with being. Since then, we've assumed the world is made up of sentence-like things called "facts" and the sentences which can be bought into correspondence with these facts are true sentences. The sentence, "there is beer in the fridge" corresponds to the fact that there is beer in the fridge and, because of this the sentence is true.

Language connects us to the world, and through it, we have access to the truth. It's a light that reveals the world to us. Species that command language are thus touched by the divine and are privileged over other species. We're like God. We can speak and have the power to create. We can know the world, ourselves, other people, good and evil. We have access to the tree of knowledge. But why should we believe the world is made up of bits of language or language-like structures? It seems quite ludicrous that my coffee is, in metaphysical fact, the divine word "coffee". What is clear, is that we've mistaken language, the tool we use to communicate about the world, for the world itself. We've mistaken the nail for the hammer.

If we reject the idea that language corresponds to the world, the idea of this metaphysical truth is left out in the cold. In this picture, truth is no longer discovered by us, but made. Philosophy's ultimate foundation bottoms out, not at language as being, but at our cultural language game. There is no ultimate foundation for knowledge, our ideas are a product of time and chance.

There is no more want for philosophy. It's at an end and its death bears no fruit.

Life without Truth
Since there is no good reason to think that language transcends the physical or that truth comes from a resounding harmonic between the two, in a capital-T kind of way, the word "truth" is reduced to the status of an occasionally useful term. Knowledge is reduced to useful belief.

It can be seductive to say this idea is true after the old manner, but it's not true by its own rubric. This doesn't mean it's false though. The idea is either useful or it isn't, and to me, it's been very useful.