AI Toolkit est une bibliothèque C ++ en tête uniquement qui fournit des outils pour construire le cerveau des PNJ de votre jeu.
Il fournit:
Pourquoi ce projet? Eh bien, j'ai écrit à ce sujet ici.
Ajoutez le dossier include
de ce référentiel à vos chemins d'inclusion.
Ou ajoutez-le en sous-module:
$ git submodule add https://github.com/linkdd/aitoolkit.git
$ g++ -std=c++23 -Iaitoolkit/include main.cpp -o mygame
NB: Cette bibliothèque est compatible avec C ++ 20.
Ou en utilisant Shipp, ajoutez-le à vos dépendances:
{
"name" : " myproject " ,
"version" : " 0.1.0 " ,
"dependencies" : [
{
"name" : " aitoolkit " ,
"url" : " https://github.com/linkdd/aitoolkit.git " ,
"version" : " v0.5.1 "
}
]
}
Tout d'abord, incluez l'en-tête:
# include < aitoolkit/fsm.hpp >
using namespace aitoolkit ::fsm ;
Ensuite, créez votre type de tableau noir:
struct blackboard_type {
// ...
};
Ensuite, créez un type d'état pour chacun de vos États:
class state_dummy final : public state<blackboard_type> {
public:
virtual void enter (blackboard_type& blackboard) override {
// ...
}
virtual void exit (blackboard_type& blackboard) override {
// ...
}
virtual void pause (blackboard_type& blackboard) override {
// ...
}
virtual void resume (blackboard_type& blackboard) override {
// ...
}
virtual void update (blackboard_type& blackboard) override {
// ...
}
};
Créez votre machine d'état simple:
auto simple_bb = blackboard_type{};
auto simple_fsm = simple_machine<blackboard_type>();
simple_fsm.set_state(state_dummy{}, simple_bb);
simple_fsm.pause(simple_bb);
simple_fsm.resume(simple_bb);
simple_fsm.update(simple_bb);
Ou avec une machine à état de pile:
auto stack_bb = blackboard_type{};
auto stack_fsm = stack_machine<blackboard_type>{};
stack_fsm.push_state(state_dummy{}, stack_bb);
stack_fsm.push_state(state_dummy{}, stack_bb);
stack_fsm.update(stack_bb);
stack_fsm.pop_state(stack_bb);
stack_fsm.pop_state(stack_bb);
Tout d'abord, incluez l'en-tête:
# include < aitoolkit/behtree.hpp >
using namespace aitoolkit ::bt ;
Ensuite, créez votre type de tableau noir:
struct blackboard_type {
// ...
};
Ensuite, créez votre arbre:
auto tree = seq<blackboard_type>(
node_list<blackboard_type>(
check<blackboard_type>([]( const blackboard_type& bb) {
// check some condition
return true ;
}),
task<blackboard_type>([](blackboard_type& bb) {
// perform some action
return execution_state::success;
})
)
);
Enfin, évaluez-le:
auto blackboard = blackboard_type{
// ...
};
auto state = tree.evaluate(blackboard);
Pour plus d'informations, consultez la documentation.
Tout d'abord, incluez le fichier d'en-tête:
# include < aitoolkit/utility.hpp >
using namespace aitoolkit ::utility ;
Ensuite, créez un type de tableau noir:
struct blackboard_type {
int food{ 0 };
int wood{ 0 };
int stone{ 0 };
int gold{ 0 };
};
Ensuite, créez une classe pour chaque action que vous souhaitez pouvoir effectuer:
class collect_food final : public action<blackboard_type> {
public:
virtual float score ( const blackboard_type& blackboard) const override {
return 50 . 0f ;
}
virtual void apply (blackboard_type& blackboard) const override {
blackboard. food += 1 ;
}
};
class collect_wood final : public action<blackboard_type> {
public:
virtual float score ( const blackboard_type& blackboard) const override {
return 150 . 0f ;
}
virtual void apply (blackboard_type& blackboard) const override {
blackboard. wood += 1 ;
}
};
class collect_stone final : public action<blackboard_type> {
public:
virtual float score ( const blackboard_type& blackboard) const override {
return - 10 . 0f ;
}
virtual void apply (blackboard_type& blackboard) const override {
blackboard. stone += 1 ;
}
};
class collect_gold final : public action<blackboard_type> {
public:
virtual float score ( const blackboard_type& blackboard) const override {
return 75 . 0f ;
}
virtual void apply (blackboard_type& blackboard) const override {
blackboard. gold += 1 ;
}
};
Enfin, créez un évaluateur et exécutez-le:
auto evaluator = evaluator<blackboard_type>(
action_list<blackboard_type>(
collect_food{},
collect_wood{},
collect_stone{},
collect_gold{}
)
);
auto blackboard = blackboard_type{};
evaluator.run(blackboard);
Tout d'abord, incluez le fichier d'en-tête:
# include < aitoolkit/goap.hpp >
using namespace aitoolkit ::goap ;
Ensuite, créez une classe Blackboard qui tiendra l'état du planificateur:
struct blackboard_type {
bool has_axe{ false };
int wood{ 0 };
};
NB: Le tableau noir doit être comparable (
a == b
) et hashable.
Ensuite, créez une classe pour chaque action que vous souhaitez pouvoir effectuer:
class get_axe final : public action<blackboard_type> {
public:
virtual float cost ( const blackboard_type& blackboard) const override {
return 1 . 0f ;
}
virtual bool check_preconditions ( const blackboard_type& blackboard) const override {
return !blackboard. has_axe ;
}
virtual void apply_effects (blackboard_type& blackboard, bool dry_run) const override {
blackboard. has_axe = true ;
}
};
class chop_tree final : public action<blackboard_type> {
public:
virtual float cost ( const blackboard_type& blackboard) const override {
return 1 . 0f ;
}
virtual bool check_preconditions ( const blackboard_type& blackboard) const override {
return blackboard. has_axe ;
}
virtual void apply_effects (blackboard_type& blackboard, bool dry_run) const override {
blackboard. wood += 1 ;
}
};
Enfin, créez un plan et exécutez-le:
auto initial = blackboard_type{};
auto goal = blackboard_type{
. has_axe = true ,
. wood = 3
};
auto p = planner<blackboard_type>(
action_list<blackboard_type>(
get_axe{},
chop_tree{}
),
initial,
goal
);
auto blackboard = initial;
while (p) {
p. run_next (blackboard); // will mutate the blackboard
}
Pour plus d'informations, consultez la documentation.
La documentation est disponible en ligne ici.
Vous pouvez le construire localement à l'aide de doxygen:
$ make docs
Cette bibliothèque est publiée selon les termes de la licence du MIT.