Skip to content

A bonzAI Story: Prologue

bonzaiferroni edited this page Jul 26, 2017 · 12 revisions

This is the history of the bonzAI codebase. I enjoy reading narratives like this from other players. I stole the idea from Lexxicon:

I would love to read more on the code designs of the popular code bases, why they chose them, how well they've worked, area's that have been problematic with the design; additionally how their code base has evolved, especially so with the popular open source projects such as bonzAI or TooAngel.

Prologue: Before bonzAI

My First Room

I'm a terrible showoff. I constantly pester my friends with gifs from whatever project I'm working on. As a result, I have gifs starting from day 1 playing Screeps. Here is my first room:

first room

This is not a history view, it shows you how fast the ticks were at that time (early June 2016).

Screeps has supplied me with a steady stream of challenges ever since the beginning. In my Community.push() survey it asks about "eureka moments". If I had taken that survey, I think I would have mentioned figuring out that I could completely harvest a source using a 5-WORK creep, provided that creep could always stand by the source. You can tell how excited I was by my cursor movements:

efficient harvest

Of course, in order to let your 5-WORK harvester stand at the source the whole time, you need to break up your harvesting roles into harvester and carrier. Here's my first attempt at that system:

harvester roles

It's a good thing I had all those roads, right? Judging by my cursor movements, I also seem to hold the belief that hovering my mouse over the creep allows it to perform more efficiently. Behold, the secret to my success!

My First Neighbors

Here's a birds-eye-view of my neighborhood:

neighborhood

I was super jealous about my neighbor to the southwest, Marcel2508. Look how close his controller is to the sources!

Marcel2508

Of course, neither of us had any idea how much room would be required for structures as RCL increases. He eventually ran out of room and had a pretty hard time compensating. We were friendly rivals up until he decided to stop playing. We would share ideas and talk about strategies.

After he stopped playing, I totally used all the energy in the storage that he left behind.

My First Raid

I decided my neighbor to the north was going to help me test the first iteration of my raiding code. Brace yourself for a dose of brilliance:

first raid

Here's what I recall from how it worked:

The healing role was initially called Paladin but I eventually renamed it to the infinitely more eloquent HealStump. Because I only had enough spawn energy to build a creep with maybe 4-5 HEAL parts, I knew I would need several of these in order to survive the tower. They were supposed to stand together near the location that I manually set for them, heal each other, and heal nearby attacker creeps. Look how well they manage to stay together while traveling!

If you do the math, the only reason I survived is because his tower wasn't targeting me for some reason. I also had no idea that he wasn't an active player, I sent him plenty of unanswered apologies.

My First Source Keeper Solution

Source Keepers were a fun challenge right from the beginning. Back when I started playing, SK sources yielded more energy and the NPC invaders were also easier to deal with. My first SK solution had just as much elegance as my first raiding code:

First SK 1 First SK 2

I was unable to spawn 50-part creeps, so I had to make do with several smaller creeps. They would idle nearby and block the SK when it spawned, together delivering an unsportsmanlike beatdown. Since they trap the SK on its spawn, I gave them the role name Trapper, which I still use for my SK killers. One of the best parts about SK harvesting is stealing their lunch money. The solution on the right allowed my scavenger to approach the spawn and retrieve the dropped energy.

Eventually I realized that some SKs would require 4-5 creeps and that seemed inefficient, so I refactored to allow just a single creep:

Second SK

Adopting TypeScript

I should describe the evolution of my codebase that was happening over this time. Like everyone else, I started out with the boilerplate javascript from the tutorial. By the time I decided to tackle multi-room functionality, this code was starting to feel a little restrictive.

Before Screeps, I hadn't written a line of javascript. I was (and still am) relatively new at programming. I started learning Java the year before and also had a few months of C# under my belt. Both of those languages have a strong Object-Oriented foundation, and OOP was really the only coding style that I was familiar with. The prototype-based polymorphism in javascript was very confusing at first. I was also used to the safe, warm, fuzzy blanket that is static typing. For this reason, I adopted TypeScript fairly early on. I'm very glad I did, viewing the javascript that would be transpiled from my TypeScript code helped me to understand JS much better.

I had a pretty basic plan that surprisingly hasn't changed a great deal over time (I'll give more details in the next chapter). I ran this plan by one of the veteran players, asking whether they thought it would work. Since I started about 3 weeks before the steam release, I was one of the only novice players at the time. I had all the veterans to myself! I pestered them with many questions. I was given enough encouragement with my plan that I thought it would be worth a try.

My First Alliance

Of course, novice walls don't last forever! Soon after they dropped, I had my first run-in with a veteran player. It was none other than the legendary taiga. I believe he was ~GCL10 at the time. He managed to snag a room that I had my eye on, and I was deciding whether or not to cause a ruckus over it. Here is our first exchange:

first alliance

Eventually we decided to team up, with the understanding that we could still battle it out over that room. A year later, I have yet to take it from him, taiga is still the proud owner of E27S15. Although it hasn't been for lack of trying, that was the room that I tried to tag during the Screeps Warfare Championships:

taiga attack

Of course that didn't happen until a long time afterward, I'm getting ahead of myself.

The Next Chapter

This seems like a good place to end our prologue. Joining the ThunderKittens added a whole new layer to screeps. We shared many ideas and eventually came up with a spec for a codebase that we could share: KClub. This would eventually be forked to create bonzAI. But more about that in the next chapter!