New C++ AI Interface

New C++ AI Interface

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

New C++ AI Interface

Post by hoijui »

The alpha~beta version of the new C++ AI Wrapper is now available under the cppintnew branch at the main repo:
http://github.com/spring/spring/commits/cppintnew

The Wrapper:
http://springrts.com/wiki/AIWrapper:Cpp
AI/Wrapper/Cpp

The test AI:
http://springrts.com/wiki/AI:CppTestAI
AI/Skirmish/CppTestAI

main thing missing: in some places an OO object Unit is used, and in others still unitID (same wiht other ID'ed objects).

sidenote:
I also did various changes to the AI related pages in the Wiki.
User avatar
AF
AI Developer
Posts: 20686
Joined: 14 Sep 2004, 11:32

Re: New C++ AI Interface

Post by AF »

Is there an overview of the C++ OO api? I'm at work on lunchbreak so github visit isn't really possible.

As a note, its useful to have Ids so I wouldn't conceal them completely.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New C++ AI Interface

Post by hoijui »

mmm...
someone could but the javadoc of the Java interface (as the structure is identical) or the doxygen of this online, but other then that, there is no overview for you.

The Java OO AIs never have to use IDs (except for groupIds, and possibly some other relatively hidden stuff). IDs are only visible through the toString() method there.

I hate having an OO interface where i have to do C like stuff. But if it is really needed, it will be no problem making the protected or private function(s) GetId() public. As it is all genreated code, it has to be changed in one place only.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: New C++ AI Interface

Post by imbaczek »

it's generally a good idea to give read only access to the engine id IMHO.
User avatar
LoidThanead
Posts: 58
Joined: 27 Feb 2008, 16:16

Re: New C++ AI Interface

Post by LoidThanead »

The Java interface has getters for all ID's available. I use them for logging.
User avatar
AF
AI Developer
Posts: 20686
Joined: 14 Sep 2004, 11:32

Re: New C++ AI Interface

Post by AF »

Its just quicker and faster to pass around a unit ID rather than a pointer to a structure or class.

For example in NTai I have an array of pointers to unit definitions and unit objects of type CUnit of MAX_UNITS. They're idnexed by their unit ID, and when not in use zero-ed to null. This means that I can use this array for all sorts of things, such as finding out if a unit still exists, and because I pass around unit IDs I never end up with references to de-allocated memory, I can always do a check on the array index to see if it holds a null pointer and abort.

It can vastly simplify things, and an ID integer acts as a means of decoupling objects in an OO system, making things much more flexible.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New C++ AI Interface

Post by hoijui »

as ids are readable, you can still do that AF, even doh its clearly bullshit to use ids instead of objects in any OO design. when communicating with other code, eg written in an other language, this may be a different thing, but this is for AI internal communication only. With a healthy idea of OO programming, you do not need ids here for more then debugging.
User avatar
AF
AI Developer
Posts: 20686
Joined: 14 Sep 2004, 11:32

Re: New C++ AI Interface

Post by AF »

Agreed in a pure OO system, but Object oriented design isn't always the best idea.

I don't want to tightly couple my class types where possible. I would rather pass around an integer ID reference than a pointer to somewhere in memory that may or may not be valid. Its easier memory management, and it means I can have routines where there is no need to be aware of certain classes at all since an integer is a basic type.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New C++ AI Interface

Post by hoijui »

...mmm
guess i dont want to see that, as i am not really a C++ dev.. which sometimes prefer doing things the uglier but but faster way.
As it is now, the C++ interface can not be used purely OO style, with what i want, it can be used both ways, so it is not finished yet, as i am the coder, and i decide whne its ready.

(.. yeah, yeah, i am oppressed heavily IRL :/ )
User avatar
AF
AI Developer
Posts: 20686
Joined: 14 Sep 2004, 11:32

Re: New C++ AI Interface

Post by AF »

This isn't anything to do with the way C++ programmers do ugly things, its all about orthogonality and coupling. The same points ( well not the memory management points ) apply to C# and java.

Its not a language thing, its a design thing.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: New C++ AI Interface

Post by imbaczek »

java, c# and every other language have it easy, because the import/using mechanisms are painless. you can't say that about c++ includes.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: New C++ AI Interface

Post by Tobi »

You don't need #include hell to use pointers / references to objects instead of integer IDs. Just use forward declarations.

Code: Select all

class CUnit;
void SomeFunction(CUnit* unit);
Whether or not you're using IDs or pointers, you'll need to #include some file to be able to do anything with them.

Therefore I don't agree it reduces coupling either: you don't need anything more then a forward declaration when just passing the things around (a pointer is pretty much just an integer handle with a type after all), and you still have similar coupling at the places where you actually want to do something with the handle, independently of whether it's a pointer, reference, or integer ID.

Besides that a pointer/reference is stronger typed then an integer ID: the compiler can't help you catch passing projectileID instead of unitID, while it can catch passing CProjectile* instead of CUnit*. (A major bug in Spring 0.79 was caused by such an issue: team ID was being passed where allyteam ID was expected.)

The only real advantage of using IDs is the easier handling of dead units. This applies to all languages, even dynamic and/or garbage collected ones.

In the case of C or C++ when a unit dies and you don't clear all pointers to it you have a dangling pointer, and in a dynamic/garbage collected language, when a unit dies, you have a reference to a "dangling unit": depending on the implementation this could be a proxy object with dangling unit pointer inside, or maybe just a proxy class with a non-existing unit ID inside, or an object with a dead flag set to true and throwing exceptions on all method calls. In either case, you need to clear out all pointers/references to the object when it dies, which can be cumbersome without a framework (e.g. CObject with DependentDied.)

The advantage of consistently using IDs is that you can reduce the places where you store a pointer to the object to a single container (e.g. UnitHandler). This container can then provide a safe method to convert ID to pointer/reference to invoke methods on it. Safe, as in, throwing an exception when the unit died, and returning a valid pointer otherwise.

Of course this advantage lives or dies with the capability of the engine to delay ID reuse as long as possible. Not doing this may result in bugs like the well known "unit wanders off to reclaim something on the other end of the map when it's area-reclaiming" bug.

tl;dr:
IDs vs pointers: pointers are better cause of stronger type checking, and IDs are better because it's easier to handle objects that died/disappeared/vanished.



Best thing of course is to wrap the ID in e.g. a UnitReference class, with operator-> implemented so you can use the ID as if it's a real class ;-)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: New C++ AI Interface

Post by imbaczek »

...or use a weak_ptr (c++) or a weakref (anything sane).
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: New C++ AI Interface

Post by Tobi »

Oops, true yeah, I shouldn't post when I've a lack of sleep :-P

Keeping IDs is then probably only useful if there are no objects they refer too (e.g. allyteam (maybe making objects is better solution?)), or when dealing with networking / language boundaries. (e.g. can't pass weak_ptr<CUnit> to Lua directly. (userdatum could solve this))
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: New C++ AI Interface

Post by imbaczek »

not that i use weak refs in my ai where i should ^^ (to store links to Goals, if anyone's interested.)
gajop
Moderator
Posts: 3048
Joined: 05 Aug 2009, 20:42

Re: New C++ AI Interface

Post by gajop »

How does one use the wrapper? I managed to download cppintnew branch from git, but the only thing I found in the wrapper was a bin directory with 3 awk scripts. I'm not sure how to run them.
Also, is it actually usable? What would you suggest for a C/C++ programmer that can actually be tested - I'd like to see my AI in action, it was a royal PITA (and I still didn't make it work) to set up E323AI to work - as it requires the development version of spring (from what I understood).
Any other documentation links would be really appreciated. It's taking me days just to figure out how to move a unit.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New C++ AI Interface

Post by hoijui »

first of all.. i am not sure what you mean with
I managed to download cppintnew branch from git
I hope you used git (as in git clone/git fetch/git merge/...).
if not, you should do this, as it is not too hard to learn, and it will make your (and my) life a lot easier.

The way to test the new C++ interface is simply to compile spring.
this process will call the AWK scripts, generate the code, compile it, and compile the test AI.

Instructions for compiling spring can be found here:
http://springrts.com/wiki/Building_spring

How to get a unit to move can be found here:
http://springrts.com/wiki/AI:Development

i recommend you have a look at the Java tutorial, as it is the most compleete at the moment, and it works the same or very similar in all the languages. The Legacy C++ interface is quite different, the new C++ interface is much closer to the Java one.

yes, E323AI needs the development version of spring. if you are on windows, you can simply use one of the installers Error linked in his thread, on linux, you have to ocmpile spring yourself.
if you really want to code an AI, and you want to use the new C++ interface, you should set up your system to compile spring anyway. in general that is a good idea.
once you have everything setup (which is practically just a question of whether or not you have hte right packages installed (gcc, g++, boost, ...)), and you know how git works, you can try different versions/branches of spring quite easily, and make use of bug fixes right when they come out.
bbe
Posts: 10
Joined: 15 Nov 2008, 17:24

Re: New C++ AI Interface

Post by bbe »

Not sure that I'm doing this correctly or if there is a bug in the build system. I've checked-out the 'cppintnew' branch from git but building (running 'cmake .') gives warnings saying that none of the AI wrappers, interfaces or actual bots will be built:

Code: Select all

-- Found AI Wrapper: CUtils (sources only wrapper)
warning: Legacy C++ AI Wrapper will not be built!
warning: (New) C++ AI Wrapper will not be built!
warning: C & C++ AI Interface will not be built!
warning: Java AI Interface will not be built!
warning: NullAI Skirmish AI will not be built!
warning: NullLegacyCppAI Skirmish AI will not be built!
warning: NullOOJavaAI Skirmish AI will not be built!
warning: KAIK Skirmish AI will not be built!
warning: RAI Skirmish AI will not be built!
warning: AAI Skirmish AI will not be built!
warning: CppTestAI Skirmish AI will not be built!
warning: NullJavaAI Skirmish AI will not be built!
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: New C++ AI Interface

Post by hoijui »

wooops! :/ my bad, sorry :/
that was an error which happend during merging in master. it worked for me, cause i had the right variables in cmake cache, but failed for you cause you initialized the project just now.
sorry!
i pushed a fix, try with latest HEAD now.
bbe
Posts: 10
Joined: 15 Nov 2008, 17:24

Re: New C++ AI Interface

Post by bbe »

Thanks hoijui. Still appears something is amiss. I only have 2 warnings now, but it fails to build (see second block). Do I have to reset any cache? All I've done is a git pull to update. I'm new to git so I may have done this wrong.

Code: Select all

-- Found AI Wrapper: CUtils (sources only wrapper)
warning: Legacy C++ AI Wrapper will not be built!
warning: (New) C++ AI Wrapper will not be built!
-- Found AI Interface: C-AIInterface 0.1
-- Found AI Interface: Java-AIInterface 0.1
-- Found Skirmish AI: NullAI 0.1
-- Found Skirmish AI: NullLegacyCppAI 0.1
-- Found Skirmish AI: NullOOJavaAI 0.1
-- Found Skirmish AI: KAIK 0.13
-- Found Skirmish AI: RAI 0.601
-- Found Skirmish AI: AAI 0.9
-- Found Skirmish AI: CppTestAI 0.1
-- Found Skirmish AI: NullJavaAI 0.1

Code: Select all

Scanning dependencies of target CppTestAI
[ 86%] Building CXX object AI/Skirmish/CppTestAI/CMakeFiles/CppTestAI.dir/CppTestAI.cpp.o
In file included from /home/james/dev/spring/spring/AI/Skirmish/CppTestAI/CppTestAI.cpp:18:
/home/james/dev/spring/spring/AI/Skirmish/CppTestAI/CppTestAI.h:22:24: error: AICallback.h: No such file or directory
/home/james/dev/spring/spring/AI/Skirmish/CppTestAI/CppTestAI.cpp:25:18: error: Unit.h: No such file or directory
/home/james/dev/spring/spring/AI/Skirmish/CppTestAI/CppTestAI.cpp:26:21: error: UnitDef.h: No such file or directory
/home/james/dev/spring/spring/AI/Skirmish/CppTestAI/CppTestAI.cpp:27:20: error: Engine.h: No such file or directory
Post Reply

Return to “AI”