I got mugged today

I got mugged today, in Kista, Stockholm, Sweden. They took my phone, credit cards, asked for credit card passwords (sic!), and started looking how to look inside my backpack. They held a knife to my throat when I said I won’t give them the PINs, and when I screamed for police they beat me up a little and run away. Few interesting observations:

1) I would’ve had a knife in my pocket like I always do if it wasn’t for airport security that doesn’t let you take one. I would still be in a 3v1 at a severe disadvantage, but all of the chances of defending myself were taken away in the name of security. Thanks, law.

2) They run away as soon as I started making noise, but possibly because they already had some loot; it’s possible that they’d silence me and search properly if I started screaming right away. I’ll miss the phone numbers and pictures from the phone, but I take some comfort in the fact that nobody’s going to buy a used, worn-out Jolla phone, and that they didn’t manage to take anything from the credit cards in the 15 minutes I needed to block them.

3) Right after that my first priority was blocking the credit cards, changing my email password and revoking my ssh keys. The latter two are hardly recoverable from the phone that you don’t know the passcode to, not in a matter of minutes anyway, but with the police, ambulance etc it took at least an hour for me to get to a safe place with the internet and take call of all the things that I actually still had. Also, Google is not smart enough to terminate your existing IMAP sessions after you change the password to your account (I received the “your password has been changed” email while logged in with the old one). Pathetic.

4) Swedish police was quite interested in possible fingerprints at first, but then lost all interest after a dog couldn’t sniff out the attackers. Either they gave up on the case as soon as the dog did, or realised that it won’t be much use anyway. Clearly, taking fingerprints off your citizens isn’t worth fuck all. Swedish ambulance figured that a hit on the head didn’t kill me and declared that even if my toes are broken (they hurt quite a lot) you don’t put toes in casts anyway, so I can go get myself an xray at the hospital if I feel like it. Sure, I’m gonna call a taxi with my stole phone and pay for it with my stolen money. Good riddance.

5) One of the first things police asked for is “Do you have «find my iphone?»” If you have an iphone, get yourself a “find my iphone” I guess.

Bottom line, I’m okay, but don’t expect me to answer my phone for a while (not that I was very good at answering it anyway :))


New game: Space Invaders

It’s been a while since I published RetroRacer, but a lot of new things happened in Steroids too! So many things that I backported the old games to the new engine; I’ll be testing them each time to see if I’m introducing any breaking changes. But! back to Space Invaders.

Image

(brave ship fighting off alien hordes)

 

The game is now available at https://github.com/tadzik/steroids. Below I’ll outline some of the new features in the engine itself.

Animations

It is now possible to load animations from spritesheets (example here), and tell Steroids to animate them over time.

self.load_spritesheet(‘invader’, ‘assets/invader.png’, 72, 32, 7);

my $invader = self.add_sprite(‘invader’, $x, $y);

self.add_animation($invader, Any, 200, True);

In order: load a spritesheet of seven 72×32 images, put it on screen an animate all its frames (Any), changing a frame every 200 miliseconds, and play it in a loop (True). The ships will rotate and look nice :)

Gamepad support

method update($dt) {
    my $pad = self.gamepads[0];
    my $analog = $pad.analog_percentage($pad.analog_left_x);

    if self.is_pressed(“Left”) or $pad.dpad_position(“Left”) {
        $!player.x -= 15;
    } elsif self.is_pressed(“Right”) or $pad.dpad_position(“Right”) {
        $!player.x += 15;
    } elsif $analog.abs > 0.1 {
        $!player.x += Int(15 * $analog);
    }

    …

}

    

New steroids features gamepad support! At this point the only supported one is the Xbox controller (I accidentally used the old SDL joystick API instead of a new, shiny gamecontroller API), so it’s all a little bit experimental. But, as you can see, it works pretty well and is quite useful indeed!

Game states

class Main is Steroids::State {

    …

}

 

class Menu is Steroids::State {

    …

}

 

given Steroids::Game.new {
    .add_state(‘menu’, { Menu.new });
    .add_state(‘main’, { Main.new });
    .change_state(‘menu’);
    .start;
}

What’s going on here? We have to separate game states (one for the menu and one for the actual game), and we can switch between them at any point using the change_state() method. For example, somewhere in Menu’s code:

method keypressed($k) {
    if $k eq ‘S’ {
        self.reset_state(‘main’);
        self.change_state(‘main’);
    }

    …

}

The states themselves are passed in as code references for the sake of the reset_state() method shown above. You can think of them as factories. The reset above is necessary, so each time you start a new game, it actually starts anew instead of continuing the old one (which is probably either lost or won by that time).

I probably forgot about something, so if anything is unclear just write it in the comment section. Go try out Space Invaders, and don’t forget about the soundtrack!

I’ll be talking about Steroids next weekend at this year’s Polish Perl Workshop; make sure to stop to find out about the latest developments and future plans.


New Perl6 game: RetroRacer

(I’m really sorry for the name; I couldn’t think of anything better :))

Image

 

This game, apart from (obviously) being a showcase for a new Steroids iteration, is all about switching lanes on a high traffic road in a fast car. Yay!

It’s really no rocket science compared to ThroughTheWindow from the last post – even code even looks similar. One obvious improvement (beside finally using proper PNGs instead of silly BMPs – timotimo++!) is a built-in collision detection:

my $s self.add_sprite(‘othercar’$_0);

# …

$s.when({ $_.collides_with($!player}, {

    # …

});

No more cheating with collisions like I did with ThroughTheWindow. The existing solution uses the entire image sprite as a hitbox; I’m hoping to make it customizable one day (it’s a simple thing really, code-wise).

All in all, the game isn’t all that much more sophisticated than the last one; I was really just looking for a good excuse to write a new game (and add some new stuff to Steroids), and I sort of came up with a nice theme to follow: ThroughTheWindow used just one key (spacebar), so the next step was to use two (thus RetroRacer) uses left and right arrow keys. What will the next game use? 3 keys? 4 keys? Is it an arithmetical or geometrical series? Oh my, I can’t wait to find out myself.

Now go and grab it at https://github.com/tadzik/RetroRacer, and don’t forget about the soundtrack!

 


Perl 6 on steroids: Through The Window

Image

 

I got into programming because I wanted to write games. I played games as a kid (wolfenstein 3d, putt-putt, I don’t remember the rest of the names), and I thought “when I grow up, I’m going to write games!”

I never really did; at some point I realized I’ve written more compilers than games: whatever happened to the childhood dream? So I thought I’ll write some, to try and learn something new.

With two friends from work I went to javascript game programming conference – it was the only game conf I’ve ever heard of, so I thought “javascript or not, let’s see how gamedev geeks party”. The universal “let’s create idiotic games and make a lot of money on ads and In-App-Purchases” attitude of the startup crowd discouraged me a little bit, but I tried to ignore that bit and focus on the technical content. I never liked javascript, I didn’t really want to use it for any kind of programming, and frankly, working in Perl and Python I grew tired of dynamic typing altogether, but I thought “hmm, maybe if I created a superset of JS that has nice type annotations, that the compiler checks and then drops, emitting vanilla JS, that wouldn’t be so bad to write code in”. I consulted a friend of mine, and, as it usually happens, it turned out that such thing already exists: it’s called Typescript, and Microsoft created it long ago. Oh well, let’s give it a try.

Why am I writing about all this? Where does Perl 6 come in? Thing is, when I started programming in Typescript, I got annoyed. It’s severly underdocumented, undersupported, development is not pleasant, because you get some errors from the compiler and then different errors from a browser, but the worst thing is: it was slow! It was so slow it was unbearable, and I thought “ah, screw it. I’ll be better off with Perl 6”.

I chose Perl 6 for performance reasons! Ain’t that something to tell my grandkids about.

Of course, creating games in Perl 6 is not so trivial: I’ll have to write the engine/framework/whatever myself. Time to roll the sleeves up and get to work.

I got quite motivated by http://lessmilk.com. This guy creates a new game every week to learn game development. Cool thing! He was describing phaser.js in one of his articles, so I created Steroids, and modelled it after Phaser.

Why steroids? Well, at some point I ported my C Asteroids to Perl 6, as a proof of concept, to see if it can indeed handle 60fps games (it can), and the “steroids” bit somewhat got stuck in my mind. Also, being on steroids gives you much more flexibility than, say, being on the rails. Don’t worry, nothing bad about being on Steroids: Just ask Duke Nukem, he got by just fine.

So, Perl 6 on Steroids was born. I started writing a running-jumping game, and abstracting the commonly used bits to a module as I went on. Why a running-jumping game? Well, you asked for it: it’s time for another backstory:

Ever wrote in a backseat of the car as a kid, looking out the window? Did you imagine a person running along the car, jumping over obstacles? I did, and from what I’ve heard I am not the only one. Thus, “Through the Window” was born: a game where a man runs along the horizon, jumping over trees and cows, being the first showcase for Steroids, and a reason for it to exist.

The post is getting lenghty enough, and there’s much to announce still, so I’ll run through the 80 lines of code really quickly to show you what Steroids gives you. You can read the entire source code here

class Game is Steroids::Game

You create a class that inherits from Steroids::Game. You need to define at least two methods for it to make any sense: create() and update(). The former initializes the game, and the latter is called 60 times per second to update the game state.

Some of the things you may want to do in the create() method:

self.load_bitmap(‘background’, ‘assets/background.bmp’);

self.add_sprite(‘background’, 0, 0);

Pretty self-explanatory. Steroids handles loading bitmaps from disk for you, and putting them in a scene.

self.load_bitmap(‘runner’, ‘assets/runner.bmp’);

$!runner = self.add_sprite(‘runner’, 50, GROUNDLEVEL);

$!runner.y -= $!runner.h;

$!runner.gravity = 1;
$!runner.when({ $_.y > GROUNDLEVEL – $_.h }, {
$!runner.y = GROUNDLEVEL – $!runner.h;
    $!runner.velocity[1] = 0;
})

;

Plenty of things here: we add a runner to the scene. We move him up a little bit, so he’s actually standing on the ground, rather than having his head on the ground level, we give him a gravity (so he’s falling down all the time), and we add an event, so when he hits the ground with his feet we stop him, so he doesn’t fall any further down. This should probably be handled by a collision detection at some point, but I didn’t get around to write collision detection yet.
 
That’s the interesting part from create(), now let’s look at update() really quickly.
 
method update {
        if @!obstacles
        and @!obstacles[0].x < ($!runner.x + $!runner.w) < (@!obstacles[0].x + @!obstacles[0].w)
        and $!runner.y + $!runner.h > @!obstacles[0].y {
            say “===========================================”;
            say ” Game over! You traveled $!distance pixels “;
            say “===========================================”;
            say “”;
            self.quit;
        }
Here’s our half-assed collision detection: if the front foot of the runner is inside the first obstacle (so, the leftmost), then the game is over.
 
How do the obstacles get there in the first place?
 if $!distance > $!last-obstacle + 40 and rand > 0.9 {
     my $s = self.add_sprite(<cow tree>.pick, self.width, GROUNDLEVEL);
     $s.y -= $s.h;
     $s.velocity[0] = -12;
     @!obstacles.push: $s;
     $s.when({ $_.x < 0 – $_.w }, {
     self.remove_sprite($_);
         @!obstacles.shift;
     });
     $!last-obstacle = $!distance;
}
If enough time has passed since we put an obstacle on the road (we don’t want the road to be impossible to travel), we add either a tree or a cow on the ground, as far on the right as we can. We make it slowly move to the left, add it to the list of obstacles, and add an event so when it reaches the left edge of the screen we remove it from the scene.
This part features a hack: @!obstacles.shift removes the first element of the list, and it just so happens that the object disappearing from the scene will always be the first one on the list: we don’t need to look through @!obstacles to find which one it is.
 
if self.is_pressed(“Space”) and $!runner.y == GROUNDLEVEL – $!runner.h {
     $!runner.velocity[1] = -22;
}
Pretty obvious: if Space is pressed while the runner is on the ground, he bounces upwards.
 
That’s just about all that it’s there to describe. Go play around with it, and remember about the soundtrack (in the README) – it’s a very important part of the game :)
 
(I was informed that the build process can be a bit more complicated on OSX; the entire Steroids development team is working hard to fix it, but if you have a good and ready solution, please send me a pull request).
 
But wait, there’s more! To celebrate the best game I’ve ever written, I’m announcing a contest: The task is to write a game using Steroids (with as many patches to it as you want). Let’s see how much can we squeeze out of those 120 lines of code to create something fun. One week from now, next sunday, I’m going to pick a winner and reward the author with a game that I like, and the author doesn’t yet have. Have an inappropriate amount of fun!

Polish Perl Workshop status update

Exactly two months remain until Polish Perl Workshop this year. In case you didn’t know, I’m shamelessly reposting the status update from our ACT website.

We’ve got the first sponsor offers, and about 15 talk submissions so far. Awesome! However, as we are well aware that most of you will wait to submit talks until there’s less than 2 days until the deadline (or later :)) I’m hereby announcing: The talk submission deadline for Polish Perl Workshop 2013 is 14 of April. That’s about two weeks to finally make up your mind and submit something. Please do! There’ll be only one chance to talk at the First Polish Perl Workshop: there’ll be no other :)

Of course, if you’d prefer to organize more of a hands-on event, we have the entire sunday dedicated to hackathon, BoFs, classes etc. If you want to submit something more lenghty, that’s the right way to do it.

In other interesting news, we started looking at conference t-shirts (yes, they’re free for all attendees). We have something on our mind that never happened before on a Perl conference (or at least I’ve never seen or heard of something like this). We’ll keep you informed.


Threads for Rakudo Perl 6

So it has come to this. Threads.pm is up and running, bringing the ever-wanted threaded execution to the most popular Perl 6 implementation.

You’re looking for TL;DR, aren’t you? Here’s what it’s capable of:

use Threads;
use Semaphore;

my @elements;
my $free-slots = Semaphore.new(value => 10);
my $full-slots = Semaphore.new(value => 0);

sub produce($id) {
    my $i = 0;
    loop {
        $free-slots.wait;
        @elements.push: $i;
        $i++;
        $full-slots.post;
    }
}

sub consume($id) {
    loop {
        $full-slots.wait;
        my $a = @elements.shift;
        $free-slots.post;
    }
}

for 1..5 -> $i {
    async sub { produce($i)  }
}

for 5..10 -> $i {
    async sub { consume($i) }
}

Doesn’t look that awesome. I mean, it’s just a producer-consumer problem, what’s the big deal? Let me repeat:

OMG, RAKUDO HAS WORKING THREADS.

So, once we’re done celebrating and dancing macarena all around, there’ll always be someone to ask “hold on, there’s gotta be a caveat. Something surely is missing, explain yourself!”

I’ll be delighted to say “nope, everything’s there!”, but that’d make me a liar. Yeah, there are missing pieces. First, those aren’t really native threads – just green threads. Native OS threads are already implemented in Parrot VM, but NQP (the language that Rakudo is based on) still doesn’t support them, so before some volunteer comes along to fix them, you’ll still have to build parrot --without-threads (which means: use green threads, not OS threads) for Threads.pm to work. But fear not! The API is exactly the same, so once native threads are there, both Threads.pm and the code you write with it should work without any changes.

But green threads are fine too! Except for one minor detail: whenever any of them blocks on IO, the entire Parrot comes to a halt. The plan is for Parrot threads scheduler to handle it nicely, but it’s not there yet, so if you expected nice and easy async IO, sorry, but you’re stuck on MuEvent :)

Yep, we’re not really there yet. But I claim it’s closer than ever. We have working threads implementation. You can write code with that, and it’s not a PITA. Go for it! There’s a lot of room to improve it. I didn’t try really hard for Threads.pm to follow the concurrency synopsis (in my defense, I think niecza doesn’t follow it either :)), and I think that once we unleash a wolfpack of developers which can work towards something that we’ll all love to use.


“Looks like I pissed off the internet”

A friend messages me: “is that you on the picture? I found this on Hacker News”

„Haha, hey, that’s me indeed!” … „Wait, wtf”

TL;DR: I don’t fancy being used as a Bad Example and an advertisement for your Brilliant Startup. Also, using somebody’s picture without permission and censoring his comment is Not Very Nice.

After readinng the article linked above I left the following comment below:

> You guys need help getting dressed in the morning so you don’t leave the house in a Hackathon shirt and cargo pants.
Oh, how convincing: fetch my picture from the hackathon and tell me to not dress as if I was dressing for a hackathon. Nice.
As somebody pointed out already, it may be nice to ask for someone’s permission before publishing a picture in this context, especially since the original website, from where I believe you took that picture from, has my name is written down below the image. Oh by the way, ISTR it had a watermark as well, just outside the frame you included; it’s very nice of you to not credit the photographer who values his work.
As far as I like the idea of helping geeks (please, no “nerds”. Respecting each other is not _that_ hard) to dress better, your “advertisment” is not particularly sophisticated. I hope that the rest your ideas are more interesting than this one.
For some reason the comment didn’t make it through moderation. Whoops. So I took the liberty to respond to the post on my own blog.
Now that I read this I think I could’ve reacted in a less angry way (although it was still not „the internet angry”, as a friend of mine pointed out), but the points stand.
So, a little backstory: On the Perl 6 hackathon in Oslo there was a picture taken with all the participants, myself included. A strictly programming event, who would be surprised that people would be wearing t-shirts from previous conferences? Apparently somebody would, and thus will use us as an example of guys who „need help”.
That’s not very nice.
Disregarding the legal aspects of publishing a picture of people, after carefully removing the watermark, and portraying them as anti-social and helpless, I find it hard to imagine how an advertising of this sort could appeal to the very same people you’re targeting with your post. Apparently, reading the comments which did make it through moderation, some people included on the pictures actually liked the idea, so it may just be that I know nothing about marketing. My bad.
A few hours after posting the article to hacker news, @jordynclee posted on her twitter: “Looks like I pissed off the internet”. Woo, how did that happen? Mayhaps insulting people whose photos you use without their permission is not the best idea of all?
Now, dear @jordynclee, I’ll continue on listening to opinions of people who respect other people, and keep on doing business which doesn’t involve ridiculing others to achieve my goals. Thank you for your constructive criticism.

On ignoring TODOs

A while ago I was looking for a way to organize myself. I read GTD for Hackers, and then eagerly split my TODO into Next Actions, Projects, Waiting For and Someday/Maybe. It all looked nice and organized, and I felt like I’m really doing something to get organized and stuff.

Except that my problem wasn’t really about forgetting to do stuff. It was about not wanting to do stuff. You know what procrastination is? No? Well, I’ll explain it later.

Then I noticed that I usually get motivated just before the deadline of some sort. My calendar was full of deadlines, and a day before I usually sat there and did what was to be done. So I thought, how about artificial deadlines?

Since then, instead of putting stuff on a TODO, I put it in a calendar for a random free day in the near future. Then if I don’t have time to do something, I can postpone it, depending on how much time I still need to complete it.

It appears to be working fine. It’s more about getting motivated than getting organized, but I feel that I cannot be the only one who is looking for “how to get organized” when I really need “how to get motivated to actually get stuff done”.

Hope this helps.


Back from Oslo

And so the hackathon is over. I haven’t experienced anything this awesome since I discovered how well bacon tastes with maasdamer.

I seem to be much more productive during hackathons if I have no talk to prepare. I’ve had lots of plans about it (see previous post), and while I haven’t started even half of them I think, I’m really satisfied with what was achieved.

On friday I looked a little bit into how Rakudo loads precompiled modules and why doesn’t it always work as expected. The easiest one to notice was Test.pm, which for some unknown reason was installed only as a source file, not a precompiled module. A simple fix in the Makefile, and suddenly every test file you ever run runs about 3.5 seconds faster. Suddenly it makes module development a lot less painful.

The other thing about module loading was that a lib/ directory was always at the very beginning of the @*INC array – that means that precompiling your modules (which usually puts them in blib/) before running your tests gave you… right. Absolutely nothing. How significant is that? Well, tests of Panda take just under 7 seconds when you have the modules precompiled, and almost 23 seconds when they aren’t. So that’s about 3 times faster test runs for your modules. How cool is that?

That solved pretty much all the issues and wtf’s I’ve experienced with module precompilation. It took me another commit on saturday to teach panda to precompile modules again. Then my mind wandered again around the idea of emmentaler, a service for automatic smoke testing of Perl 6 modules. A quick and dirty 100-lines long script is now able to download all the modules, test them and present the results in a bit silly, but definetely useful way. There’s still lots of things to be done about it, but the fact that you can test all the modules in about 20 minutes is something that wasn’t quite possible before. Huge success; I hope I can turn it into something that the whole community will benefit.

Saturday also brought a couple of fixes to Pod handling and the –doc command line switch. After a quick discussion with Damian Conway, sort of a “what does the spec really expect me to do”, I taught –doc to Do The Right Thing whatever argument you pass to it. So –doc=HTML will use Pod::To::HTML for Pod formatting, and whatever module you write is available to be used this way without changes neither in Rakudo nor in the module code itself. A small thing, but something that waited much too long to be done after GSoC. It should now be possible to extract documentation from modules in a simple, automatic way, possibly for inclusion on modules.perl6.org or somewhere? We’ll see about that.

The same day Marcus Ramberg worked on Pod::To::Text, in particular the .WHY handling part. He also contributed some patches to Rakudo itself, and thanks to him what –doc prints for documented subs and methods is actually Something Useful and not Something That’s Good That It Exists. Much appreciated!

Sunday brought some new fixes to Bailador::Test, which now does almost everything that Dancer::Test does, so some brave soul could actually start porting tests from Dancer to Bailador to see what blows up. I’ve also spent plenty of time (mostly compilation time) chasing small things like a few-months-old panda bug, which actually turned out to be a Rakudobug, making perl6 –doc behave a bit more like perldoc, and less like “what the hell does this beast do”, and some other small things.

Besides hacking there was also plenty of opportunities to walk around Oslo, try some delicious food and beer and meet people mostly known from IRC before. Social aspect of the Hackathon was probably the best part of it, even given the fact that the coding aspect was probably the best one I’ve ever experienced. This wouldn’t have been possible without the work that the organizers have put into the event. It was absolutely perfect. Thank you, Oslo.pm! And thank you, sjn for giving me the opportunity to be there. It was a great hackathon and a time well spent. I hope to see you guys sooner than later. And if later than sooner, let it be the next hackathon in Oslo, for there’s no way that I’ll let this one happen without me.


Plans for the Perl 6 Hackathon in Oslo

The Perl 6 Patterns hackthon in Oslo is happening next week, gathering most of the Rakudo developers. Awesome!

I’m still looking for the thing I’d like to work on during the event. The first obvious thought is Bailador, the Perl 6 port of Dancer web framework. It’s getting more and more in shape, and I’ve recently started looking through the code of Dancer 2 to see how it all should be done. I really don’t want Bailador to share a fate of neutro (ie reaching a state of being a horrible, unmaintainable mess), and I feel that it’s time for it to be refactored a bit. It came to life as a 10-line module written on a piece of paper during my Spanish classes, and simply adding more and more features to it is not going to end well.

Moritz Lenz is planning to hack on database stuff, which combined with a useful and usable web framework could create a nice platform for web development in Perl 6. I’ve already heard people saying “well, I could write Perl 6 apps if it had a web framework and some database access”, so I guess it’s time to show off something suitable for web development, even simple, but usable and covering most usage scenarios.

While we’re at the Web-y stuff, one thing that we really miss is a good (or at least good-enough) templating engine. Bailador has been using Ratel for some time, yet for one it stopped working on Rakudo recently (it was probably relying on some buggy or incorrect behaviour), and it suffers the problem of having no maintainer (no one really claims ownership of it). Hitomi was to be a templating engine for the new age, so maybe it’s about time to bring it back to life in all its glory? One could definitely find something to hack on.

From the new things, but still related to web development, zby has proposed porting WebNano to Perl 6 as one of the hackathon projects. It’s not something very big, and it should be totally possible to rewrite it in Perl 6 to run on Rakudo.

Or maybe there is something you particulary want to see in Perl 6? I’m sure we’ll not lack people with tuits; what is there that could possibly make Perl 6 a useful tool for you, something that you were missing when you last looked at it?