It is currently Tue Jun 18, 2013 10:31 pm Advanced search

Game engine in C++

Code won't compile? Found a bug? Post here!

Game engine in C++

Postby albert » Sun Sep 12, 2010 9:18 pm

Hi,

I have reimplemented the game engine (i.e. PlayGame.jar) in C++. You can get the code here:
http://github.com/albertz/planet_wars-cpp

The implementation is currently very close to the Java one. However, I probably will refactor and extended it a bit because I will use it in my bot implementation.

I also reimplemented the game viewer. The code is at the same place. More details here.

Maybe it is also useful for someone else.
Last edited by albert on Tue Sep 14, 2010 5:03 pm, edited 1 time in total.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby codeRage » Sun Sep 12, 2010 9:37 pm

It might be useful.. but is it faster than the Java one?
codeRage
Major
 
Posts: 30
Joined: Sat Sep 11, 2010 6:13 pm

Re: Game engine in C++

Postby albert » Sun Sep 12, 2010 10:11 pm

I didn't really do any performance comparison but I am pretty sure it is. It basically mostly the same code, just translated to C++. And in most cases, this results in faster code. Also, you save the huge overhead of the JVM.

Also, if you need some game simulation code in your C++ bot, you don't really have any other choice.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby DanielVonFange » Mon Sep 13, 2010 2:45 pm

Nice, albert.
DanielVonFange
Brigadier-General
 
Posts: 149
Joined: Wed Sep 08, 2010 1:15 pm

Re: Game engine in C++

Postby Stardragon » Mon Sep 13, 2010 3:36 pm

I think what I'd like best is if the game player were somehow merged with the visualiser: sometimes it can be a drag to play the game in one thing and load it into the next. I'd like to have more direct feedback on what my bot is doing. :-/
Stardragon
Cadet
 
Posts: 4
Joined: Thu Sep 09, 2010 9:41 pm

Re: Game engine in C++

Postby theli_ua » Tue Sep 14, 2010 3:47 pm

i sometimes get different match results with official java engine and this one

on a side note it would be nice to implement support for building natively on windows (it does not work atm due to some posix-specific things, i have it running in cygwin)
theli_ua
Lieutenant
 
Posts: 14
Joined: Tue Sep 14, 2010 7:45 am

Re: Game engine in C++

Postby albert » Tue Sep 14, 2010 5:02 pm

Uh, it gives different results? Can you post/attach two game replays which differ, one with Java and one with the C++ engine?

If it differs because of a timeout of one of the bots, that can be kind of random.

My code is based on the most recent Java code (from the Google Code project). I have read that the released Java tools may contain some bug when there are multiple fleets arriving at a planet at the same time. Maybe that is the case here. Check out if the current Java code produces also the same result.

I don't have Windows here, so I cannot really implement and test the code under Windows. You probably need to rewrite most of the Process class (see utils.{h,cpp}) (there is no fork under Windows but it may be work by just using CreateProcess instead; and then you must somehow get access to the stdin/stdout stream of that created process). Everything else should probably work.

If you do so, please send me a patch so I can include that.
Last edited by albert on Tue Sep 14, 2010 5:09 pm, edited 2 times in total.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby petesimard » Tue Sep 14, 2010 5:05 pm

Can you post the instructions for compiling this on Cygwin? I'm a bit of a linux noob.
petesimard
Lieutenant
 
Posts: 11
Joined: Sun Sep 12, 2010 9:07 am

Re: Game engine in C++

Postby igner » Tue Sep 14, 2010 6:00 pm

petesimard wrote:Can you post the instructions for compiling this on Cygwin? I'm a bit of a linux noob.

being compiled under Cygwin it will run even slower than Java.
here's my test result for 10000 turns (do not look to the numbers, just look to the time difference):
Java: 52 sec
C++ : 56 sec
IMO this happen because of using intermediate layer (well, what do you think Cygwin is? it's just a "wrapper" in some meaning).
It would be nice if Albert compare time "c++ vs Java" on Linux :)
I think it should be ported to win, to have speed increase on Windows...

well, if you still want to compile this and check by yourself ...
1) download and run Cygwin setup (don't forget to select packages: gcc, g++, make)
2) download Albert's source
3) browse in Cygwin to the folder with Albert's source and type "make playgame" - this will compile engine only, without GameViewer

actually I've edited makefile before compiling it under Cygwin, removing all mention of SDL and "showgame" procedure.
i've attached engine source with my changes in makefile.
Attachments
Albert.Engine.zip
(14.35 KiB) Downloaded 84 times
igner
Lieutenant
 
Posts: 10
Joined: Sun Sep 12, 2010 10:54 am

Re: Game engine in C++

Postby albert » Tue Sep 14, 2010 6:34 pm

For someone who is a bit common with C++ development on Windows, it should really be trivial to port this.

Btw., if you just want to compile the engine and not the viewer, just use `make playgame` instead of just `make`. That should work without editing the Makefile even if you don't have SDL.

Speed comparison for me with 58 turns, first bot is my own, second the rage example bot:

Code: Select all
%time java -jar tools/PlayGame.jar maps/map1.txt 10000 1000 log.txt "./MyBot" "java -jar example_bots/RageBot.jar" >/dev/null 2>/dev/null
3.80s user 0.66s system 121% cpu 3.670 total

%time .../playgame ...
1.03s user 0.15s system 105% cpu 1.113 total


Now 579 turns, original C++ starter pack bot against itself:

Code: Select all
%time java -jar tools/PlayGame.jar  maps/map1.txt 10000 1000 log.txt "./OrigMyBot" "./OrigMyBot" >/dev/null
2>/dev/null
10.41s user 1.21s system 110% cpu 10.551 total

%time .../playgame ...
0.38s user 0.07s system 128% cpu 0.354 total


So, 10 secs vs 0.3 secs :)

I think this also shows that the bots are the biggest bottleneck.

The difference is also even more notable if you use it together with the viewer, in my case the native C++ viewer (which just shows up immediately because it doesn't wait until the game ends).
Last edited by albert on Tue Sep 14, 2010 8:51 pm, edited 1 time in total.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Next

Return to Technical Issues

Who is online

Users browsing this forum: Google [Bot] and 0 guests

cron