nim-screeps icon indicating copy to clipboard operation
nim-screeps copied to clipboard

A module for writing Screeps programs with the Nim programming language!

Nim-Screeps

A module for writing Screeps programs with the Nim programming language!

MIT License - Copyright 2016 Hans Raaf

Why Nim? Because I can!

What is Screeps?

Screeps - The world's first MMO strategy sandbox game for programmers

In Screeps you basically need to write a game AI such that you can sustain control over land in a virtual world. The "World" is shared by all players which fight over land control.

Screeps Screenshot

If that sounds vaguely interesting to you go and try the free website based live demo which does not need registration. Following the Tutorial (in JavaScript) is also highly recommended even if you want to use Nim later on.

What is Nim?

Nim is a statically typed, imperative programming language with major metaprogramming possibilities.

What Wikipedia thinks what Nim is!

Nim can be used as a systems programming language and offers different code generator backends.

The primary code generator is C which then gets automatically compiled to native code for your system. Nim compiles very fast and can even cross compile to embedded devices.

What makes Nim special is also that there are a lot other code generator backends: C, CPP, Objective-C, JavaScript. It even has an experimental PHP backend which is used by the folks in my company METATEXX GmbH. We also develop a Nim based Zend extension Module.

What makes Nim viable for Screeps?

Don't fear! Nim is also very capable of rapid development and this makes something like Nim-Screeps a viable and fun option for Screeps development.

Since some time Nim also has a JavaScript backend. Which means that you can write in the same language but targeting NodeJS, Servers or execution in a Browser.

The native language for Screeps is JavaScript. This Nim module use the magic of the Nim compiler and its JavaScript backend so you can write all your Screeps code directly in Nim!

Compiling Nim live to a remote Server!

Since Screeps hit Steam it is trivial to setup a system which deploys Nim generated JavaScript to the Screeps "World" or "Simulator".

This module makes it easy to setup the whole system and even gives you some working examples for the Screeps Simulator!

Linking the compiler generated code into the Screeps application is merely seeing a path (or using a softlink).

Let's get startet:

Attention: The module and the documentation is WIP (but fun to try I think!)

Example: sim1

WIP (just some OSX hints for now)

First: Install Screeps with steam on OSX

Then add one/some of the examples to the local screeps script folder. For example like this:

cd ~
git clone [email protected]:oderwat/nim-screeps.git
cd ~/Library/Application\ Support/screeps/scripts/screep.com
ln -s ~/nim-screeps/deploy/sim1/ som1

Here the example code from "sim1":

# nim build --verbosity:1 --hints:off
# run done
#
# sim1 example for Screep
#
# (c) 2016 by Hans Raaf of METATEXX GmbH

import screeps

screepsLoop: # this conaints the main loop which is exported to the game

  # Spawn creatures endlessly as soon as enought energy is available
  discard game.spawns.Spawn1.createCreep(@[WORK, CARRY, MOVE], nil, nil)

  # Let them harvest energy at the first source and transfert it to the spawn
  for creep in game.creeps:
    # > Do what you are told, the whip!
    if creep.carry.energy < creep.carryCapacity:
      var sources = creep.room.find(Source);
      # harvest energy if near the source
      if creep.harvest(sources[0]) == ERR_NOT_IN_RANGE:
        # move to the source
        creep.moveTo(sources[0]);
    elif game.spawns.Spawn1.energy < game.spawns.Spawn1.energyCapacity:
      # our spawn needs more energy. transfer it there
      if creep.transfer(game.spawns.Spawn1, RESOURCE_TYPE_ENERGY) == ERR_NOT_IN_RANGE:
        # uh not near the spawn. move there!
        creep.moveTo(game.spawns.Spawn1);

Compile the example:

cd ~/nim-screeps/examples/sim1/
nim build main.nim

Select sim1 from the branches in the Screeps client inside the Simulator (Training Mode) and create the Spawn1.

After some time it should look like this:

Screeps Screenshot

Using scup as uploader

Recently I added a small tool for uploading the compiled code onto the screeps server. This works without the Steam client running. You need to set your password in your account.

The tool uses the same config file as the screeps-console, which also is supported for extended logging.

Basically you need a yaml file in your home ~/.screeps_settings.yaml which contains at least this infomation:

# Screeps account info
# Your username is your full email address.
screeps_username: [email protected]
screeps_password: yourpassincleartext

Using scup is kinda mandatory right now, as it automatically gets build and is being used if you nim build main.nim in examples/world1

Scup is used like this: scup <jsfile> <branch>. The name of the file and the name of the branch which should get uploaded. Notice that this is only uploading a single file on the branch. This is good enought because nim-screeps compiles everything in one file anyway.

Example: world1

world1 is very experimental and used for testing the Nim module development. Even if it was just made for testing the implementation it works quite well at let sustain playing with 2+ rooms already for some weeks.

There will probably more examples soon. Feel free to contribute.

What else to know?

Later in time the Screeps engine is announce to be public available and local servers are announced too.

Have fun... thats why I made it!