Howto (Hello-World) Skirmish AI?

Howto (Hello-World) Skirmish AI?

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

Moderators: hoijui, Moderators

Post Reply
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Howto (Hello-World) Skirmish AI?

Post by abma »

Hi,

i'm trying to write an ai, but i'm really unsure what functions i should use, there are to few docs for the new c/c++ interface. (Or i haven't found it?)

This is, what i have:

SpringAI.h

Code: Select all

EXPORT(int) handleEvent(int teamId, int topic, const void* data);

EXPORT(int) init(int teamId, const struct SSkirmishAICallback* callback);


class AI{
	const struct  SSkirmishAICallback * cb;
	public:
		void InitAi(const SSkirmishAICallback* callback);
		void HandleEvent(int teamId, int topic, const void* data);
};
SpringAI.cpp

Code: Select all

#include <map>
#include <stdio.h>
#include "SpringAI.h"
#include "ExternalAI/Interface/SSkirmishAICallback.h"
#include "ExternalAI/Interface/AISEvents.h"
#include "ExternalAI/Interface/AISCommands.h"

const struct  SSkirmishAICallback *teamId_callback;

AI *ai;

EXPORT(int) handleEvent(int teamId, int topic, const void* data) {
		ai->HandleEvent(teamId, topic, data);
        return 0;
}

EXPORT(int) init(int teamId, const struct SSkirmishAICallback* callback){
	ai=new AI();
	ai->InitAi(callback);
	return 0;
}

EXPORT(int) release(int teamId){
	delete(ai);
	return 0;
}

void AI::InitAi(const SSkirmishAICallback* callback){
	cb = callback;
}

void AI::HandleEvent(int teamId, int topic, const void* data){
	switch (topic){
		case EVENT_UNIT_FINISHED:{
				SMoveUnitCommand cmd;
				SUnitFinishedEvent *tmp;
				tmp=(SUnitFinishedEvent*)data;
				cmd.unitId=tmp->unit;
				cmd.groupId=0;
				cmd.options=0;
				cmd.timeOut=0;
				cmd.toPos.x=1;
				cmd.toPos.y=1;
				cmd.toPos.z=1;
	cb->Clb_Engine_handleCommand(teamId,0,0,COMMAND_UNIT_MOVE,&cmd);
			}
			break;
	}
}
Makefile

Code: Select all

SPRINGSRC=/path/to/spring/src
INCLUDE=-I${SPRINGSRC}/AI/rts/System -I${SPRINGSRC}/AI/Wrappers/CUtils -I${SPRINGSRC}/AI/Interfaces/C -I${SPRINGSRC}/rts

all:
        c++ -shared -fPIC SpringAI.cpp ${INCLUDE} -o libSkirmishAI.so
It works, the commander moves to position 1.1.1, but this implementation is currently very bad (and should be only used once for a game).

are the include-files ok?

I've read http://springrts.com/wiki/AIInterface:C but there was an error in the first sentence... (i've fixed it).

I think the usage of cb->Clb_Engine_handleCommand() is wrong...?!

Any ideas what function to use, to send commands to the engine or was it right?
KAIK and RAI are using ../Wrappers/LegacyCpp/ as include, this is confusing...

Every hint is welcome :-)
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Howto (Hello-World) Skirmish AI?

Post by imbaczek »

probably the easiest way to write an ai now is to use Java or a Lua mutator. if you want to do it in C/C++, you need to include several files from the engine. you can check out my KP AI to see which files are needed (they're specified in the VS solution and in the waf wscript.)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Howto (Hello-World) Skirmish AI?

Post by zwzsg »

If your goal is write an simple AI for Spring without too much hassle, a Lua gadget is the easiest way. See LuaAI.lua and /LuaRules/Gadgets/KP_AI_31.lua inside Kernel_Panic_3.3.sd7 for an exemple (if you don't know, .sd7 are just renamed 7z files).

If you want to do some real C++ coding, then yeah, look at the other KP AI, the one imbaczek made.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: Howto (Hello-World) Skirmish AI?

Post by abma »

In http://github.com/imbaczek/baczek-kpai/ ... Export.cpp "LegacyCpp/AIGlobalAI.h" is included.

Will this file be deprecated in the future?
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Howto (Hello-World) Skirmish AI?

Post by imbaczek »

i don't think so. there's a lot of code that depends on it; it's called legacy because it used to be the only interface available, and now it's been modified to work on top of the C interface.

also, AIExport.cpp is basically stolen from a noop NullLegacyCppAI.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: Howto (Hello-World) Skirmish AI?

Post by abma »

how do i use directly the c interface, or is the c-interface incomplete?
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Howto (Hello-World) Skirmish AI?

Post by imbaczek »

it is complete, but not really user-friendly. you'll need to look around, i'm afraid. see how legacycpp or java ai interface do things.

btw some of the needed files may be autogenerated - you may need to compile spring to see them.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Howto (Hello-World) Skirmish AI?

Post by hoijui »

there is also a new C++ AI Interface in the workings, but its not yet complete.
i would also say that the easiest way is to write a Java Skirmish AI, or a Lua AI (i dont really know about that, but i guess it is easy).

The idea is, to make the new C++ itnerface ready, and then the AI Devs could migrate to this interface, and when nobody uses the Legacy one anymore, it would be deprecated.
but well...
that may very well be never ;-)

if you have more questions, you may also join the #ai channel on the lobby. often there is nobody in there, but when i am on, i am there.. so you may just autojoin it.
User avatar
LoidThanead
Posts: 58
Joined: 27 Feb 2008, 16:16

Re: Howto (Hello-World) Skirmish AI?

Post by LoidThanead »

If you do decide to start writing an AI in Java, I'd be happy to help. I've been working on an AI of my own over the past couple of months, and made plenty of mistakes of my own, that I might help you avoid.
Post Reply

Return to “AI”