14 Aug 2016

SimCity BuildIt Crafting Bot

When a task is repetitive, why would we want to do it ourselves, if someone or something else could do it equally well or better? Computers are really good at doing repetitive tasks because they are accurate, don’t get bored, complain or need to be paid. Tasks which run within a system are the easiest and cheapest to automate, others can be cost effective if there is enough benefit. For example robots on a factory production line, automated driving or flying an aircraft.

SimCity BuildIt

Certain computer games are a time sink, requiring constant attention to make progress and maintain attention.

alt text

SimCity BuildIt (a mobile game which runs on the Android or IOS platform) is one such game and has 2 obvious repetitive areas:

  • Crafting: It requires you to manufacture raw materials and then use them to create other items which are more valuable. These items have an in-game value and can be sold or used to complete tasks.
  • Global Trade: Buying non craft able and time expensive craft-able items. Selling items I have crafted.

Because I want to do the fun things in the game without suffering the pain, I wrote a bot in C# to do the crafting.

Rather than writing the code native on the platform I use an android emulator (Memu) running on my PC and I send commands to it from my C# code to automate the play.

The basic iterations between the bot and the game are:

  • Touch and Swipe on the screen.
  • Read text from the display.
  • Read the colour of text on the display.

Bot Demo

Bot Detail

To build a crafting bot I needed to be able to do the following:

Select a building

This can be done by touching the centre of the screen using ADB (Android Debug Bridge), a building must be located at the centre of the screen when the bot is started. It helps to turn on the developer options for ‘pointer location’ and ‘showing touches’.

alt text

Know which building I am in

The name of the building is shown at the top of the screen, this is read using OCR (Optical Character Recognition) using Tesseract Open Source OCR Engine. A screen shot of part of the MEMU application is taken and OCR’d.

alt text

There are buttons on the side of the building name which will cycle around the factories or commercial buildings.

Move between a factory and a Commercial building

This is done by positioning selected buildings (one commercial and one factory) in a known configuration and then clicking above or below to switch. For example the Hardware store has a Mass Production factory above it and both of these are unique buildings, so when we are at either in the building cycle we can move to the other.

alt text

Build an Item in either a factory or commercial building

The craft-able items are laid out on buttons in specific places. So we can swipe from the button to the crafting location to build the item. Determining if we need to craft raw materials for craft-able items is found out by clicking on the item’s button and looking to see the colour of the required materials. Red means we don’t have enough.

alt text

Collect an item

Collecting a crafted item requires touching the building.

The things I learn’t writing the bot:

  • You can’t use User32.dll methods to click on the Memu application, you need to use ADB.
  • ADB Input commands for tap and swipe don’t work reliably. Instead I used ADB ‘sendevent’ to send a series of commands codes to do each tap and swipe.
  • A seeming complex workflow can be simplified into a working automated process with surprisingly few building blocks.
  • I need to extend the bot to sell the items on the global trade as this is a pain after the bot has been running for a few hours.
blog comments powered by Disqus

About Me

My first computer was a Commodore VIC-20, I had great fun trying to code text adventures and side scrolling shoot ‘em ups in BASIC. This helped me lead the way as the first in my school to pass a computer exam.

Currently I work as a Senior Software Engineer in Bedford for a FTSE 100 Company. Coding daily in C#, JavaScript and SQL. Outside of work I work on whatever is interesting me at that time.