Skip to main content

Large scale ambitions

Learning new things is important for every developer. I've mentioned this before, and in the spirit of doing just that, I've started a somewhat ambitious project.

I want to do a large-scale simulation, using Elixir and Go, coupled with a physics simulation in C++. I've never done anything in Elixir before, and only played a little bit with Go, but I figure, how hard can it be?


I've dubbed this project exsim - it's a simulation done in Elixir. Someday I'll think about a more catchy name - for now I'm just focusing on the technical bits. Here's an overview of the system as I see it today:

exsim sits at the heart of it - this is the main server, implemented in Elixir.
exsim-physics is the physics simulation. It is implemented in C++, using the Bullet physics library.
exsim-physics-viewer is a simple viewer for the state of the physics simulation, written in Go.
exsim-bot is a bot for testing exsim, written in Go.
exsim-client is the game client, for interacting with the simulation. I haven't actually started this yet - more on that later.

Note that this is very much work in progress - I'm not describing a completed project, my plan is to post a series of blogs about this project as it evolves, warts and all. I fully expect to experience some failures and may do stupid things because I'm learning the language - I'll write about those things as well.

So what am I simulating? Well, I've been working on EVE Online for quite some time now, so of course I'm going to do a space simulation. The purpose of this exercise is to learn two new programming languages and get familiar with the physics system so I don't want to spend time on an original game design. I'm not saying I'm going to reimplement EVE, but I am going to take some aspects of it and see how they could work in Elixir and Go.

The server

The core of this project is exsim - this is the game server, done in Elixir. It's what players (bots) connect to - it maintains the state of the world and each ship in it and distributes the state to game clients (bots) as appropriate.

To begin with, all communication between components is done in JSON format. This may not be the most efficient format, but I've opted to at least begin with a text based format. This allows me start up the server and connect to it with a simple telnet client, typing in the commands on the fly. I chose JSON because it is well supported in Elixir, Go and C++ so I don't have to spend time on writing a parser.

The physics server

The physics server is really just a thin wrapper around the Bullet physics library. The physics server runs a solar system, with a collection of ships in it. Eventually there will be a sun, planets, space stations and jump gates (to travel to other solar systems) in there as well.

The physics server has a master socket connection to the server, and can have one or more viewer connections as well. The master connection can issue commands to the server, to add/remove ships, set ship properties, step the simulation and get the state. The viewer connections get a copy of the state, to enable a debug view of the state of the simulation.

The commands are again in JSON format (as well as the responses), using RapidJSON for parsing (and emitting) the JSON strings.

The physics viewer

The physics viewer is a simple 2D viewer implemented in Go, using the Pixel 2D game library. It opens a socket connection to the physics server and listens for state packets. It draws a shape for each ship listed in the state, giving a quick visualization of the state of the solar system. For this to work, I'm limiting all movements to the x-y plane - eventually this will have to be replaced with a 3D viewer.


Botting in a live game is bad, but running bots for testing is awesome. I don't want to have to run multiple clients connecting to the server to test things - I want to run one program that simulates dozens of players. Eventually I want to use bots to load test the server, not with dozens, but thousands or tens of thousands of simulated players.

So far, I've only implemented one extremely simple bot. It connects, sets a random target location to fly towards, waits a few seconds and sets a new target location, and so on.

Current status

The server accepts connections from the bots, adding ships to the physics scene, ticks the simulation and broadcasts the state. There is no partitioning of the solar system - every ship is notified of the state of every other ship. There is also no error handling, not even of closed connections. I need to harden that up somewhat soon, if only to make it easier to iterate on things. 

The next step is to see how many ships I can throw into the solar system and see where the bottlenecks are. In some very preliminary tests I'm seeing indications that JSON may not be feasible to use in any large scale communications, but I need to run some more tests before I decide to toss it out.


Popular posts from this blog

Mnesia queries

I've added search and trim to my expiring records module in Erlang. This started out as an in-memory key/value store, that I then migrated over to using Mnesia and eventually to a replicated Mnesia table. The fetch/1 function is already doing a simple query, with match_object. Result=mnesia:match_object(expiring_records, #record{key=Key, value='_', expires_at='_'}, read) The three parameters there are the name of the table - expiring_records, the matching pattern and the lock type (read lock). The fetch/1 function looks up the key as it was added to the table with store/3. If the key is a tuple, we can also do a partial match: Result=mnesia:match_object(expiring_records, #record{key= {'_', "bongo"}, value='_', expires_at='_'}, read) I've added a search/1 function the module that takes in a matching pattern and returns a list of items where the key matches the pattern. Here's the test for the search/1 function: search_partial_…

Working with Xmpp in Python

Xmpp is an open standard for messaging and presence, used for instant messaging systems. It is also used for chat systems in several games, most notably League of Legends made by Riot Games.

Xmpp is an xml based protocol. Normally you work with xml documents - with Xmpp you work with a stream of xml elements, or stanzas - see for the full definitions of these concepts. This has some implications on how best to work with the xml.

To experiment with Xmpp, let's start by installing a chat server based on Xmpp and start interacting with it. For my purposes I've chosen Prosody - it's nice and simple to install, especially on macOS with Homebrew:

brew tap prosody/prosody
brew install prosody

Start the server with prosodyctl - you may need to edit the configuration file (/usr/local/etc/prosody/prosody.cfg.lua on the Mac), adding entries for prosody_user and pidfile. Once the server is up and running we can start poking at it to get a feel for h…

Expiring records in Erlang

I'm continuing my experiments with Erlang - this time trying out gen_server with a simple key/value store with a twist - the values have an expiration date. As a first iteration I'm simply using a dictionary to store the values, and only expiring records when they are looked up. My plan is to extend this later on so that this can be a global key/value store across multiple Erlang nodes but for now I'm focusing on two things - get something going using gen_server, and try out the common_test testing framework. The code is here: Let's first take a look at a couple of the test functions, to show the usage of this: get_non_expired_record(Config) ->Pid=?config(pid, Config), Record= {"bingo", "bongo", erlang:system_time(second) +3600}, ok=gen_server:call(Pid, {add, Record}), {ok, "bongo"} =gen_server:call(Pid, {fetch, "bingo"}). get_expired_record(Config) ->Pid=…