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

Game engine in C++

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

Re: Game engine in C++

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

Albert,
as for me, the question is "does it worth to port playgame.jar to c++" ?
I'm just too lazy to port to c++ by myself ( especially now when you already did this :) )

so, could you please show benchmarks of playgame.jar vs c++ engine (tested on your c++ bots) ?

regarding speed of viewer, thats really cool but i'm rather interested in thousands of battles of my bot vs other bots :)
igner
Lieutenant
 
Posts: 10
Joined: Sun Sep 12, 2010 10:54 am

Re: Game engine in C++

Postby albert » Tue Sep 14, 2010 8:50 pm

Haven't you seen my previous post? I posted some benchmarks about that exactly.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby igner » Tue Sep 14, 2010 9:02 pm

Oops :?
igner
Lieutenant
 
Posts: 10
Joined: Sun Sep 12, 2010 10:54 am

Re: Game engine in C++

Postby RebelXT » Wed Sep 15, 2010 1:08 am

albert, excellent work re-writing the engine!! it's faster in my tests as well. here are the numbers for 100 games

* Java engine, Java DualBot vs custom Python bot: real 3m46.067s, user 4m54.422s
* C++ engine, Java DualBot vs custom Python bot: real 3m0.848s, user 2m48.183s
* C++ engine, custom Python bot vs itself: real 2m53.789s, user 4m13.308s

one question for you - is the fight resolution math in your implementation the same as in PlayGame.jar version 1.2? results are slightly different:


c++ engine:
Code: Select all
won against example_bots/DualBot.jar : 94/100, avg turns: 99
lost against example_bots/DualBot.jar lost : 6/100, avg turns: 11


java engine:

Code: Select all
won against example_bots/DualBot.jar : 93/100, avg turns: 95
lost against example_bots/DualBot.jar lost : 7/100, avg turns: 13

[/code]
RebelXT
Colonel
 
Posts: 81
Joined: Fri Sep 10, 2010 2:05 pm

Re: Game engine in C++

Postby albert » Wed Sep 15, 2010 1:49 am

I don't know exactly what SVN revision version 1.2 is. My code is based on SVN rev 295. Esp. here. The important code should be only this function:

Code: Select all
    private void FightBattle(Planet p) {

      Map<Integer, Integer> participants = new TreeMap<Integer, Integer>();

      participants.put(p.Owner(), p.NumShips());

      Iterator<Fleet> it = fleets.iterator();
      while (it.hasNext()) {
        Fleet f = it.next();
        if (f.TurnsRemaining() <= 0 && GetPlanet(f.DestinationPlanet()) == p) {
          if (!participants.containsKey(f.Owner())) {
            participants.put(f.Owner(), f.NumShips());
          } else {
            participants.put(f.Owner(), f.NumShips() + participants.get(f.Owner()));
          }
          it.remove();
        }
      }

      Fleet winner = new Fleet(0, 0);
      Fleet second = new Fleet(0, 0);
      for (Map.Entry<Integer, Integer> f : participants.entrySet()) {
        if (f.getValue() > second.NumShips()) {
          if(f.getValue() > winner.NumShips()) {
            second = winner;
            winner = new Fleet(f.getKey(), f.getValue());
          } else {
            second = new Fleet(f.getKey(), f.getValue());
          }
        }
      }

      if (winner.NumShips() > second.NumShips()) {
         p.NumShips(winner.NumShips() - second.NumShips());
         p.Owner(winner.Owner());
      } else {
         p.NumShips(0);
      }
    }


And in my version:

Code: Select all
void Game::__FightBattle(Planet& p) {
   std::map<int,int> participants;   
   participants[p.owner] = p.numShips;

   for (Fleets::iterator it = fleets.begin(); it != fleets.end(); ) {
      Fleet& f = *it;
      if (f.turnsRemaining <= 0 && &planets[f.destinationPlanet] == &p) {
         participants[f.owner] += f.numShips;
         it = fleets.erase(it);
      }
      else ++it;
   }

   Fleet winner(0, 0);
   Fleet second(0, 0);
   for (std::map<int,int>::iterator f = participants.begin(); f != participants.end(); ++f) {
      if (f->second > second.numShips) {
         if(f->second > winner.numShips) {
            second = winner;
            winner = Fleet(f->first, f->second);
         } else {
            second = Fleet(f->first, f->second);
         }
      }
   }

   if (winner.numShips > second.numShips) {
      p.numShips = winner.numShips - second.numShips;
      p.owner = winner.owner;
   } else {
      p.numShips = 0;
   }
}


This looks very much the same to me. Possible reasons for the different outcome:

* Maybe I am overlooking something. Or some other part in the code is different and causes different results (but it should not).
* Maybe version 1.2 is different to this latest SVN revision.
* Maybe one of the bots has some random behavior.
* Maybe one of the bots has timed out in some cases.

If you figure out the real reason for your different outcomes, please let us now! :)
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby Paskal_07 » Wed Sep 15, 2010 8:12 am

I got this message when i tryed to run $make:
Code: Select all
g++ -arch i686 -g -O2 -Wall PlanetWars.cpp -c -o PlanetWars.o
cc1plus: error: unrecognized command line option "-arch"

In Makefile i changed
Code: Select all
CFLAGS := -arch $(shell uname -m) -g -O2 -Wall

to
Code: Select all
CFLAGS := -g -O2 -Wall

and project was succefully built.
I hope it will help somebody.
Information about Google AI Challenge: Galcon in russian — usefull links, tools&tweaks — добро пожаловать!
Paskal_07
Cadet
 
Posts: 6
Joined: Tue Sep 14, 2010 5:47 pm
Location: Russia, Moscow

Re: Game engine in C++

Postby albert » Wed Sep 15, 2010 12:18 pm

@Paskal: Fixed that, it should work now without any changes.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby albert » Thu Sep 16, 2010 12:20 am

I figured out one difference in the game engine but I think that my engine is correct and the current official engine of the Java SVN code also behaves the same, i.e. the officially released engine is wrong. But it would be nice if we could clarify that.

Situation:
Code: Select all
neutral planet with 9, growth rate 5
next frame: fleet with 11
next frame: fleet with 8

c++:
next frame: 2
next frame: 15

java:
next frame: 1
next frame: 14


Edit: I tested with the PlayGame 1.2 (compiled by a1k0n) and PlayGame 1.2 gives the same result as my C++ engine. So the PlayGame 1.0 really seems to be wrong here.
albert
Lieutenant-Colonel
 
Posts: 44
Joined: Sun Sep 12, 2010 9:11 pm

Re: Game engine in C++

Postby igner » Thu Sep 16, 2010 1:30 am

okay, that's me again :)

Albert
first, coming back to your benchmark results...
Am I correct that you compare "X turns of simulation on Java engine" vs "simple run of your C++ engine, without any bots" ???
If so, "10 sec vs 0.3 sec" have absolutely no sense...

Here's my benchmark results (exact 10'000 turns, "echo c++ bot" vs "echo c++ bot"):

Windows
Java engine: 22 seconds
C++ engine: 15 seconds
so, C++ is 46% faster

Linux (Ubuntu 10.04)
Java engine: 77 seconds
C++ engine: 25 seconds
so, C++ is 208% faster

Well, don't look that in Windows i have better overall result, Linux is running on VMWare, I suppose that is the reason of slower run.

P.S. Results for "Java echo bot" are the same +/- 1 second (because echo bot only replies with "go", there is no logic inside for this test).
igner
Lieutenant
 
Posts: 10
Joined: Sun Sep 12, 2010 10:54 am

Re: Game engine in C++

Postby delt0r » Thu Sep 16, 2010 7:13 am

I don't want to descend into a flame war (nothing wrong with C++ or any other lang for that matter). But i just wanted to say that at least the java player i looked at is rather awful. Its basically C in Java clothing. The worst way to write java is like C. My reimplementation in java is faster (3m24sec for PlayGame.jar and 2m24 for my player over all 100 maps with my bots (also java and also restarting the whole jvm for each bot for each game)) but most of the time is not the game engine, its the bots and the default bots have the same problem. Also bench marking with a echo bot is not going to test any of the fleet and combat resolution code which is where the performance problems probably are. Finally this is mostly IO bound.

For my testing i have bots that don't need to be "restarted". The process can be kept alive and the game engine sends a restart message. This makes a big difference with batch jobs. Especially with java.

Generally C++ and java have similar performance with similar levels of code quality (baring jvm warm up time).

Also just a quick note. The worse part of the default bots etc in the original java is reading 1 char at a time and appending it to a string from all input streams. This is many many times slower than using a readline method. Just fix that and most of the performance difference will go away.
It was like that when I got here.
User avatar
delt0r
Colonel
 
Posts: 89
Joined: Sun Sep 12, 2010 8:03 am

PreviousNext

Return to Technical Issues

Who is online

Users browsing this forum: No registered users and 0 guests

cron