July 18, 2011

Grorld - a CivWorld bonus resource grinding application

I really like CivWorld and here is my proof!

The CivWorld gameplay has one really amusing/annoying feature, the bonus resource bubbles. If you hover the mouse over one of these bubbles you will get some extra resources as a bonus. If found that those extra bonuses can be really helpful, at least during the early eras! And you can also win on of the contests by grabbing those. I believe most player find those annoying, but I really like these bonuses as a way to thank dedicated players!

I really need this extra edge that the bonus can supply! I might be the god of all CivWorld players, but my friends ain't. Thats why I ain't winning, right? So I cheat – I'm developed Grorld.

Grorld is a software that automatically finds bonus bubbles and hover the mouse over those, even while your at sleep.

This is a blog about coding (and gaming), so I'm going to tell you how I created it! Enjoy the lesson!

Warning:
This application only works on recent linux system (like Ubuntu 11.4). Windows user can preferable stop reading right now, because there will never ever be a port (or maybe a bribe could change me...)!

I realized that I needed three component to achieve my goal to automatically grind the bonus resources.
1) A screen scraper/reader/grabber to be able to 'see' the game board inside my application.
2) A 'tool' to identify bonus bubbles on the game board.
3) A mouse simulating component to automatically perform the hovering.

The mouse moving was the simplest, so let's start with that one. The Xlib library supplies a protocol to communicate with the X server. It really helped me with ALL the dirty work. For example: The Xlib API have the XWarpPointer() function to move the mouse pointer a distance relative current pointer position. However, as I need to set the mouse position to an absolute point I to retrieve the current position, via XQueryPointer() , calculated the deltas and set those with XWarpPointer().

Fully working C code:


The screen grabber component was almost as simple. But as the Xlib function XGetScreen() is way to slow I had to go and look for alternatives. Fortunately the MIT-SHM extension allows both client (my app) and the server (X) to share memory. Hence you can skip all huge image buffer copying that XGetScreen() is using. This XShmGetScreen() tutorial saved my day.

The tool to identify bonus bubble is the most interesting. I decided to use a so called template matching technique. I've used the OpenCV library before and I like it. Quickly I converted the Xlib pixmap into a OpenCV cv::Mat structure. This image was used as the so called search image. Then I loaded a home made image looking like the bonus bubbles as the so called template image. Thereafter I justed performed a cv::matchTemplate() on the search image. The result is a vector with scores. The item in the vector with the lowest score represent the pixel on the search image that has the highest probability to contain the template image, it can be retrieved with cv::minMaxLoc(). Afterwards I calculated a relative score with help of standard deviation. Fortunately the OpenCV library have helper function for that as well, cv::meanStdDev(). If the score is 3 sigma or above, we have a 99.7% probability that we have a correct hit. And in that case, I simply hover the mouse over that area...

Problem solved! And thanks for all the fish!

Please go and download it and tell me how it's working...

5 comments:

  1. Maqibooy - Thank a ton for this tool. It is working ok for me atm. However, I seem to get stuck on the workers'/farmers' houses after it gathers a bonus resource and ends up there. For some reason the bonus resources stop spawning if you are hovered over a house... ... ... I'm not great with code, but if you can find a way to fix this I would sure appreciate it :) (or explain to me how I am wrong if that is the case)

    Thanks a ton!

    ReplyDelete
  2. Sure, I will look at it!

    Details here, some help from you would be appreciated:
    https://github.com/maq777/Grorld/issues/1

    ReplyDelete
  3. Is this tool still working?

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. It works!!! Thanks a lot!

    ReplyDelete