[solved] first steps in developing a C++ AI

[solved] first steps in developing a C++ AI

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

Moderators: hoijui, Moderators

Post Reply
Shaitan
Posts: 16
Joined: 04 Jan 2014, 11:54

[solved] first steps in developing a C++ AI

Post by Shaitan »

hello,

i'm stuck in the very beginning. how can i give orders?
Last edited by Shaitan on 10 Jan 2014, 16:11, edited 1 time in total.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: first steps in developing a C++ AI

Post by abma »

http://springrts.com/wiki/AI:Development
http://springrts.com/wiki/AIWrapper:Cpp

minimalistic example ai:

http://springrts.com/wiki/AI:CppTestAI
source code:

https://github.com/spring/spring/blob/d ... TestAI.cpp

full-featured ai:

https://github.com/Tarendai/Shard

shard is mostly a wrapper which allows lua-scripts to handle the logic.

sorry, can't directly give instrucations as there are mutliple possibilities. which example did you use to start?

giving orders should work sth. like this:

callback->GiveOrders(unitid, &Command);

but not sure, depends on the wrapper used.
Shaitan
Posts: 16
Joined: 04 Jan 2014, 11:54

Re: first steps in developing a C++ AI

Post by Shaitan »

yes, i've seen all those but i'm still lost.
abma wrote:sorry, can't directly give instrucations as there are mutliple possibilities. which example did you use to start?
i started with the CppTestAI.
abma wrote:giving orders should work sth. like this:
callback->GiveOrders(unitid, &Command);
i guess you meant GiveOrder(unitid, &Command). but what command ID should i set to build something?
abma wrote:but not sure, depends on the wrapper used.
i thought that it should be the C++ AI Interface Wrapper, shouldn't it?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: first steps in developing a C++ AI

Post by abma »

yep, should be the "good" one.

maybe this file from shard is interesting:
https://github.com/Tarendai/Shard/blob/ ... ring_api.h

-> in this folder, the engine relevant is:
#include "AI/Wrappers/Cpp/src-generated/

files are automaticly generated so you require to compile spring (at least once).

hope this helps. hoijui is the guru here...
Shaitan
Posts: 16
Joined: 04 Jan 2014, 11:54

Re: first steps in developing a C++ AI

Post by Shaitan »

okay, let's say i want the commander to build a solar collector at the commander's current position. there's a build member function for a unit:

Build(UnitDef* toBuildUnitDef, const springai::AIFloat3& buildPos, int facing, short options = 0, int timeOut = INT_MAX)

correct me if i'm wrong, in my case UnitDef* toBuildUnitDef must be the unit definition for a solar collector. so, how can i get it? or do i misunderstand the whole thing?
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: first steps in developing a C++ AI

Post by abma »

sth. like:
UnitDef* def = callback->GetUnitDef()...

but idk, as i never wrote a c++ ai from scratch.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: first steps in developing a C++ AI

Post by AF »

Using the new OO C++ API, commands are easy for the most part:

e.g.

https://github.com/Tarendai/Shard/blob/ ... t.cpp#L108

Code: Select all

this->unit->Stop();
const springai::AIFloat3 pos(p.x, p.y, p.z);
this->unit->Fight(pos);
Constructing things:

Code: Select all

springai::UnitDef* ud = ....
const springai::AIFloat3 pos( xcoord..., ycoord..., zcoord... );

if(this->game->Map()->CanBuildHereFacing(t,p,facing)){
        try {
                this->unit->Build(ud, pos, facing, 0, 10000);
        } catch(...){
                return false;
        }
        return true;
} else {
        return false;
}
Where this->unit is of type springai::Unit*, and facing is an integer, most likely of the value UNIT_COMMAND_BUILD_NO_FACING.


But all of the above could have been discovered using common sense and basic deducation combined with basic C++ knowledge. A cursory glance at the forums would have told you to look at Shard and not the other AIs ( it has been mentioned in various forms, either as complaints the other AIs are uninteresting and dead, or that Shard is the only one active, in the top and most recently active threads ). So perhaps you should have looked harder.

Moving on,

The new OO C++ API is magically autogenerated by awk scripts written by Hoijui, you will not find it on github, but you can generate them via cmake. Try to build any of the existing AIs and they will be auto-generated.

The old legacy C++ API is not auto generated for the most part, and is what most of the AIs are built in ( because at the time it was the only API available ). Commands in this API are done on the main callback object, and take a unit ID, a command ID, and an array of parameters in float form.

These commands are closer to the engine form, in that command IDs are defined in a header file, and construction commands have an ID that is the unit type ID, but negative. So to build a solar, you take the solar unit def ID, say 20, and give a command ID of -20.

There is also a C API. Nobody uses the C API, but the other APIs are built on top of it.


My recommendation would be to take a copy of Shard, remove the lua dependency, and use the existing C++ classes as a base to build your AI. The C++ components of Shard delegate everything to the internal lua environment, so instead, delegate it to your own C++ objects
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: first steps in developing a C++ AI

Post by AF »

I've updated the Shard readme and will be making more updates. This update explains the folder structure and adds some useful pieces of information, some mentioned in this thread. I'll expand on it later in the day once I've finished work

https://github.com/Tarendai/Shard/blob/master/README.md
Shaitan
Posts: 16
Joined: 04 Jan 2014, 11:54

Re: first steps in developing a C++ AI

Post by Shaitan »

AF wrote:springai::UnitDef* ud = ....
i'm sorry, but since i don't have enough of "common sense and basic deducation combined with basic C++ knowledge", would you please explain to me how to get UnitDef for a solar collector from the C++ AI Interface Wrapper? it would be so tolerant of you.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: first steps in developing a C++ AI

Post by AF »

Yet you missed an entire class called unit with helpful methods that demonstrate what you wanted, including how to get a unitdef for a unit type given the name of the unit type. Sure you'd need to read back through the code but you can be forgiven for asking questions about that.

Put simply I got the impression you'd done no research. However I gave you the benefit of the doubt and posted useful information, and I was blunt too, but we all have thick skins, this is the tinternets! I'm happy to continue helping, but not if you're going to be sarcy about it, get to the point else I'll spend my time elsewhere.
Shaitan
Posts: 16
Joined: 04 Jan 2014, 11:54

Re: first steps in developing a C++ AI

Post by Shaitan »

all right, that's how:

Code: Select all

...
springai::Unit* commanderUnit = callback->GetTeamUnits().front();
...
commanderUnit->Build(callback->GetUnitDefByName("armsolar"), commanderUnit->GetPos(), UNIT_COMMAND_BUILD_NO_FACING);
...
thank you guys for your help. now i can proceed to the most exciting part.
Post Reply

Return to “AI”