gs_ai.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*==============================================================================================================
  2. * Copyright: 2020 John Jackson
  3. * GSAI: AI Util for Gunslinger
  4. * File: gs_ai.h
  5. * Github: https://github.com/MrFrenik/gunslinger
  6. * All Rights Reserved
  7. * MIT License
  8. * May all those that this source may reach be blessed by the LORD and find peace and joy in life.
  9. * Everyone who drinks of this water will be thirsty again; but whoever drinks of the water
  10. * that I will give him shall never thirst; John 4:13-14
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  12. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  14. * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  15. * The above copyright, blessing, biblical verse, notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  18. * TO THE WARRANTIES OF MECHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  20. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. =================================================================================================================*/
  23. #ifndef GS_AI_H
  24. #define GS_AI_H
  25. /*
  26. USAGE: (IMPORTANT)
  27. =================================================================================================================
  28. Before including, define the gunslinger ai implementation like this:
  29. #define GS_AI_IMPL
  30. in EXACTLY ONE C or C++ file that includes this header, BEFORE the
  31. include, like this:
  32. #define GS_AI_IMPL
  33. #include "gs_ai.h"
  34. All other files should just #include "gs_ai.h" without the #define.
  35. MUST include "gs.h" and declare GS_IMPL BEFORE this file, since this file relies on that:
  36. #define GS_IMPL
  37. #include "gs.h"
  38. #define GS_AI_IMPL
  39. #include "gs_ai.h"
  40. ================================================================================================================
  41. */
  42. /*==== Interface ====*/
  43. /** @defgroup gs_ai_util AI Util
  44. * Gunslinger AI Util
  45. * @{
  46. */
  47. /*
  48. //=== Utility AI ===//
  49. Modeling Curves (examples, all [0, 1]):
  50. * Binary: y = x < m ? 0 : 1;
  51. * Linear: (100 - x) / 100
  52. * Cubic: (100 - x^3) / (100^3)
  53. * Logistic: 1/(1 + (2.718 * 0.45) ^ (x + 40))
  54. * Logit: log_e(x/(1-x) + 5) / 10
  55. Idea:
  56. * Collect current states of independent variables
  57. * NOrmalize using response/modeling curves
  58. * Combine as necessary
  59. * Compare all normalized values and select:
  60. - Highest/lowest selection
  61. - Weighted random from all choices
  62. - Weighted random from top/bottom n choices
  63. Table of utility scores with actions to take based on those scores. These actions could be behavior trees or single tasks.
  64. Reasoner:
  65. * Works on a modular list of choices/actions and reports which one to act on
  66. * Actions are determined by individual 'considerations'
  67. * Combined considerations generate an 'appraisal'
  68. - Appraisals are evaluated and the best fit for the time/context is chosen
  69. Considerations:
  70. * Considerations are atomic pieces of logic that combined can
  71. score to help determine which action to take
  72. * Easy to add/remove considerations from the list at ANY time
  73. * Easy to extend with new considerations
  74. * Easy to reuse considerations
  75. * Encapsulates on aspect of a larger decision
  76. - Distance
  77. - Cost
  78. - Selection History
  79. - Benefit
  80. - Health
  81. - Etc.
  82. * Parameterized for each individual actor for modularity and granularity of control
  83. //=== Behavior Tree ===//
  84. typedef enum
  85. {
  86. BSF_BT_RUNNING = 0x00,
  87. BSF_BT_SUCCESS,
  88. BSF_BT_FAILURE
  89. } bsf_bt_result;
  90. Main types of nodes:
  91. * Composite: One or more children
  92. * Decorator: Single child
  93. * Leaf: No children
  94. Composites:
  95. * Sequence: Breadth first walk through children, if all return success then return success. Else return failure. (Acts as an AND).
  96. * Selector: Breadth first walk through children, if any return success, return success. Else return failure. (Acts as an OR).
  97. Decorators:
  98. * Inverter: Invert result of reported child node state.
  99. * Repeater: Repeat a set number of loops. Typically used at root of tree to continuously loop behavior.
  100. * Repeat Until Success/Fail: Repeat indefinitely until either success/failure achieved.
  101. * Succeeder/Failer: Always return success/failure.
  102. */
  103. // General context structure used for agents
  104. typedef struct gs_ai_context_t
  105. {
  106. void* user_data;
  107. } gs_ai_context_t;
  108. //==================//
  109. //=== Utility AI ===//
  110. //=== Response Curves ===//
  111. typedef float (*gs_ai_curve_func)(float m, float k, float c, float b, float x);
  112. GS_API_DECL float gs_ai_curve_logit(float m, float k, float c, float b, float x);
  113. GS_API_DECL float gs_ai_curve_logistic(float m, float k, float c, float b, float x);
  114. GS_API_DECL float gs_ai_curve_sin(float m, float k, float c, float b, float x);
  115. GS_API_DECL float gs_ai_curve_cos(float m, float k, float c, float b, float x);
  116. GS_API_DECL float gs_ai_curve_linearquad(float m, float k, float c, float b, float x);
  117. GS_API_DECL float gs_ai_curve_binary_lt(float m, float k, float c, float b, float x);
  118. GS_API_DECL float gs_ai_curve_binary_gt(float m, float k, float c, float b, float x);
  119. GS_API_DECL float gs_ai_curve_binary_lte(float m, float k, float c, float b, float x);
  120. GS_API_DECL float gs_ai_curve_binary_gte(float m, float k, float c, float b, float x);
  121. GS_API_DECL float gs_ai_curve_binary_eq(float m, float k, float c, float b, float x);
  122. GS_API_DECL float gs_ai_curve_nsigmoid(float m, float k, float c, float b, float x);
  123. GS_API_DECL float gs_ai_curve_constant(float m, float k, float c, float b, float x);
  124. typedef struct
  125. {
  126. gs_ai_curve_func func;
  127. float slope; // Slope of curve (m)
  128. float exponent; // Order or curve (k)
  129. float shift_x; // Shift curve along x-axis (c)
  130. float shift_y; // Shift curve along y-axis (b)
  131. } gs_ai_utility_response_curve_desc_t;
  132. //=== Considerations ===//
  133. typedef struct {
  134. float data;
  135. float min;
  136. float max;
  137. gs_ai_utility_response_curve_desc_t curve;
  138. } gs_ai_utility_consideration_desc_t;
  139. typedef struct {
  140. gs_ai_utility_consideration_desc_t* considerations;
  141. size_t size;
  142. } gs_ai_utility_action_desc_t;
  143. typedef struct {
  144. gs_ai_utility_action_desc_t* actions;
  145. size_t size;
  146. int16_t force_eval;
  147. } gs_ai_utility_action_list_desc_t;
  148. //=== Evaluation ===//
  149. float gs_ai_utility_action_evaluate(gs_ai_utility_action_desc_t* desc);
  150. //=====================//
  151. //=== Behavior Tree ===//
  152. #define GS_AI_BT_NODE_MAX_CHILDREN 20
  153. #define GS_AI_BT_STATE_FAILURE -1
  154. #define GS_AI_BT_STATE_SUCCESS 0
  155. #define GS_AI_BT_STATE_RUNNING 1
  156. // These nodes are expensive.
  157. typedef struct gs_ai_bt_node_t {
  158. const char* name;
  159. uint16_t idx;
  160. int16_t processed_child;
  161. uint16_t num_children;
  162. uint16_t max_children;
  163. int16_t state;
  164. } gs_ai_bt_node_t;
  165. typedef struct gs_ai_bt_t {
  166. uint16_t current_idx;
  167. gs_dyn_array(uint32_t) parent_stack;
  168. gs_dyn_array(gs_ai_bt_node_t) stack;
  169. gs_ai_context_t ctx;
  170. } gs_ai_bt_t;
  171. typedef void (*gs_ai_bt_func)(struct gs_ai_bt_t* ctx);
  172. typedef void(*gs_ai_bt_leaf_func)(struct gs_ai_bt_t* ctx, struct gs_ai_bt_node_t* node);
  173. typedef bool(*gs_ai_bt_condition_func)(struct gs_ai_bt_t* ctx, struct gs_ai_bt_node_t* node);
  174. GS_API_DECL void gs_ai_bt_begin(struct gs_ai_bt_t* ctx);
  175. GS_API_DECL void gs_ai_bt_end(struct gs_ai_bt_t* ctx);
  176. GS_API_DECL void gs_ai_bt_children_begin(struct gs_ai_bt_t* ctx, struct gs_ai_bt_node_t* parent);
  177. GS_API_DECL void gs_ai_bt_children_end(struct gs_ai_bt_t* ctx);
  178. GS_API_DECL gs_ai_bt_node_t* gs_ai_bt_parent_node_get(struct gs_ai_bt_t* ctx);
  179. GS_API_DECL int16_t gs_ai_bt_repeater_begin(struct gs_ai_bt_t* ctx, uint32_t* count);
  180. GS_API_DECL void gs_ai_bt_repeater_end(struct gs_ai_bt_t* ctx);
  181. GS_API_DECL int16_t gs_ai_bt_inverter_begin(struct gs_ai_bt_t* ctx);
  182. GS_API_DECL void gs_ai_bt_inverter_end(struct gs_ai_bt_t* ctx);
  183. GS_API_DECL int16_t gs_ai_bt_condition_begin(struct gs_ai_bt_t* ctx, bool condition);
  184. GS_API_DECL void gs_ai_bt_condition_end(struct gs_ai_bt_t* ctx);
  185. GS_API_DECL int16_t gs_ai_bt_selector_begin(struct gs_ai_bt_t* ctx);
  186. GS_API_DECL void gs_ai_bt_selector_end(struct gs_ai_bt_t* ctx);
  187. GS_API_DECL int16_t gs_ai_bt_sequence_begin(struct gs_ai_bt_t* ctx);
  188. GS_API_DECL void gs_ai_bt_sequence_end(struct gs_ai_bt_t* ctx);
  189. GS_API_DECL int16_t gs_ai_bt_utility_selector_begin(struct gs_ai_bt_t* ctx, gs_ai_utility_action_list_desc_t* actions);
  190. GS_API_DECL void gs_ai_bt_utility_selector_end(struct gs_ai_bt_t* ctx);
  191. GS_API_DECL int32_t gs_ai_bt_parallel_begin(struct gs_ai_bt_t* ctx);
  192. GS_API_DECL void gs_ai_bt_parallel_end(struct gs_ai_bt_t* ctx);
  193. GS_API_DECL void gs_ai_bt_leaf(struct gs_ai_bt_t* ctx, gs_ai_bt_leaf_func func);
  194. GS_API_DECL void gs_ai_bt_wait(struct gs_ai_bt_t* ctx, float* time, float dt, float max);
  195. GS_API_DECL void gs_ai_bt_free(struct gs_ai_bt_t* ctx);
  196. #define gsai_bt(_CTX, ...)\
  197. do {\
  198. gs_ai_bt_begin((_CTX));\
  199. __VA_ARGS__\
  200. gs_ai_bt_end((_CTX));\
  201. } while (0)
  202. #define gsai_repeater(_CTX, ...)\
  203. do {\
  204. if (gs_ai_bt_repeater_begin((_CTX), NULL))\
  205. {\
  206. __VA_ARGS__\
  207. gs_ai_bt_repeater_end((_CTX));\
  208. }\
  209. } while (0)
  210. #define gsai_parallel(_CTX, ...)\
  211. do {\
  212. if (gs_ai_bt_parallel_begin((_CTX)))\
  213. {\
  214. __VA_ARGS__\
  215. gs_ai_bt_parallel_end((_CTX));\
  216. }\
  217. } while (0)
  218. #define gsai_inverter(_CTX, ...)\
  219. do {\
  220. if (gs_ai_bt_inverter_begin((_CTX)))\
  221. {\
  222. __VA_ARGS__\
  223. gs_ai_bt_inverter_end((_CTX));\
  224. }\
  225. } while (0)
  226. #define gsai_condition(_CTX, _COND, ...)\
  227. do {\
  228. if (gs_ai_bt_condition_begin((_CTX), (_COND)))\
  229. {\
  230. __VA_ARGS__\
  231. gs_ai_bt_condition_end((_CTX));\
  232. }\
  233. } while (0)
  234. #define gsai_selector(_CTX, ...)\
  235. do {\
  236. if (gs_ai_bt_selector_begin((_CTX)))\
  237. {\
  238. __VA_ARGS__\
  239. gs_ai_bt_selector_end((_CTX));\
  240. }\
  241. } while (0)
  242. #define gsai_sequence(_CTX, ...)\
  243. do {\
  244. if (gs_ai_bt_sequence_begin((_CTX)))\
  245. {\
  246. __VA_ARGS__\
  247. gs_ai_bt_sequence_end((_CTX));\
  248. }\
  249. } while (0)
  250. #define gsai_utility_selector(_CTX, _ACTIONS, ...)\
  251. do {\
  252. if (gs_ai_bt_utility_selector_begin((_CTX), (_ACTIONS))) {\
  253. __VA_ARGS__\
  254. gs_ai_bt_utility_selector_end((_CTX));\
  255. }\
  256. \
  257. } while (0)
  258. #define gsai_leaf(_CTX, _FUNC) gs_ai_bt_leaf((_CTX), (_FUNC))
  259. #define gsai_wait(_CTX, _TIME, _DT, _MAX) gs_ai_bt_wait((_CTX), (_TIME), (_DT), (_MAX))
  260. #define gsai_succeed(_CTX, _NODE) {_NODE->state = GS_AI_BT_STATE_SUCCESS; return;}
  261. #define gsai_fail(_CTX, _NODE) {_NODE->state = GS_AI_BT_STATE_FAILURE; return;}
  262. #define gsai_running(_CTX, _NODE) {_NODE->state = GS_AI_BT_STATE_RUNNING; return;}
  263. /** @} */ // end of gs_ai_util
  264. //========================//
  265. /*==== Implementation ====*/
  266. #ifdef GS_AI_IMPL
  267. //=====================//
  268. //=== Behavior Tree ===//
  269. GS_API_DECL void
  270. gs_ai_bt_free(struct gs_ai_bt_t* ctx)
  271. {
  272. if (ctx->parent_stack) gs_dyn_array_free(ctx->parent_stack);
  273. if (ctx->stack) gs_dyn_array_free(ctx->stack);
  274. }
  275. GS_API_DECL void
  276. gs_ai_bt_begin(struct gs_ai_bt_t* ctx)
  277. {
  278. ctx->current_idx = 0;
  279. }
  280. GS_API_DECL void
  281. gs_ai_bt_end(struct gs_ai_bt_t* ctx)
  282. {
  283. gs_assert(gs_dyn_array_empty(ctx->parent_stack));
  284. }
  285. GS_API_DECL gs_ai_bt_node_t*
  286. gs_ai_bt_parent_node_get(struct gs_ai_bt_t* ctx)
  287. {
  288. if (gs_dyn_array_empty(ctx->parent_stack)) return NULL;
  289. uint32_t idx = ctx->parent_stack[gs_dyn_array_size(ctx->parent_stack) - 1];
  290. return &ctx->stack[idx];
  291. }
  292. GS_API_DECL void
  293. gs_ai_bt_children_begin(struct gs_ai_bt_t* ctx, struct gs_ai_bt_node_t* parent)
  294. {
  295. parent->num_children = 0;
  296. gs_dyn_array_push(ctx->parent_stack, parent->idx);
  297. ctx->current_idx = parent->idx + 1;
  298. }
  299. GS_API_DECL void
  300. gs_ai_bt_children_end(struct gs_ai_bt_t* ctx)
  301. {
  302. gs_dyn_array_pop(ctx->parent_stack);
  303. }
  304. GS_API_DECL gs_ai_bt_node_t*
  305. gs_ai_bt_node_child_get(struct gs_ai_bt_t* ctx, struct gs_ai_bt_node_t* parent, uint32_t child_index)
  306. {
  307. gs_ai_bt_node_t* node = NULL;
  308. uint32_t ci = parent->idx + child_index + 1;
  309. uint32_t sz = gs_dyn_array_size(ctx->stack);
  310. if (ci < sz) {
  311. node = &ctx->stack[ci];
  312. }
  313. return node;
  314. }
  315. #define gs_ai_bt_node_next(CTX) gs_ai_bt_node_next_ext(ctx, 0)
  316. GS_API_DECL gs_ai_bt_node_t*
  317. gs_ai_bt_node_next_ext(struct gs_ai_bt_t* ctx, int16_t processed_child)
  318. {
  319. // Push on new node if doesn't exist
  320. gs_ai_bt_node_t* np = NULL;
  321. uint32_t cnt = gs_dyn_array_size( ctx->stack );
  322. if (ctx->current_idx >= gs_dyn_array_size(ctx->stack)) {
  323. gs_ai_bt_node_t node = gs_default_val();
  324. node.state = GS_AI_BT_STATE_RUNNING;
  325. node.idx = ctx->current_idx++;
  326. node.num_children = 0;
  327. node.processed_child = processed_child;
  328. gs_dyn_array_push(ctx->stack, node);
  329. np = &ctx->stack[gs_dyn_array_size(ctx->stack) - 1];
  330. }
  331. else {
  332. // Get pointer to node and increment idx
  333. np = &ctx->stack[ctx->current_idx];
  334. np->idx = ctx->current_idx++;
  335. }
  336. // Increment children of current parent, if available
  337. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  338. if (parent) {
  339. // Increase number of children
  340. parent->num_children++;
  341. // If we're concerned about max children, make sure we haven't passed it
  342. if (parent->max_children && parent->num_children > parent->max_children) {
  343. gs_log_error("Attempting to add more children than max allows.");
  344. }
  345. }
  346. return np;
  347. }
  348. GS_API_DECL int32_t
  349. gs_ai_bt_parallel_begin(struct gs_ai_bt_t* ctx)
  350. {
  351. // Get next node in stack
  352. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  353. node->name = "parallel";
  354. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  355. // If not processing this node, return 0x00 to fail if check
  356. if (parent && parent->processed_child != -1 &&
  357. gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  358. if (node->state == GS_AI_BT_STATE_RUNNING) {
  359. // Begin processing new child stack
  360. gs_ai_bt_children_begin(ctx, node);
  361. }
  362. // Processed child for parallel will be -1, to signify that we process ALL children.
  363. node->processed_child = -1;
  364. return node->state;
  365. }
  366. GS_API_DECL void
  367. gs_ai_bt_parallel_end(struct gs_ai_bt_t* ctx)
  368. {
  369. // Get top of parent stack for node
  370. #if 1
  371. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  372. // For each child
  373. bool all_succeed = true;
  374. for (uint32_t i = 0; i < node->num_children; ++i) {
  375. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  376. gs_assert(child);
  377. // If any child fails, this node fails
  378. if (child->state == GS_AI_BT_STATE_RUNNING) {all_succeed = false;}
  379. if (child->state == GS_AI_BT_STATE_FAILURE) {node->state = GS_AI_BT_STATE_FAILURE; all_succeed = false; break;}
  380. }
  381. // If we've failed, we don't need to process anymore children
  382. if (node->state == GS_AI_BT_STATE_FAILURE || !node->num_children) {
  383. node->state = GS_AI_BT_STATE_FAILURE;
  384. // Pop all children and their children off stack
  385. uint32_t cnt = gs_dyn_array_size(ctx->stack);
  386. for (uint32_t i = node->idx + 1; i < cnt; ++i) {
  387. gs_dyn_array_pop(ctx->stack);
  388. }
  389. ctx->current_idx = node->idx + 1;
  390. }
  391. else if (!all_succeed) {
  392. node->state = GS_AI_BT_STATE_RUNNING;
  393. }
  394. else {
  395. node->state = GS_AI_BT_STATE_SUCCESS;
  396. // Pop all children and their children off stack
  397. uint32_t cnt = gs_dyn_array_size(ctx->stack);
  398. for (uint32_t i = node->idx + 1; i < cnt; ++i) {
  399. gs_dyn_array_pop(ctx->stack);
  400. }
  401. ctx->current_idx = node->idx + 1;
  402. }
  403. #endif
  404. // End child stack, pop off transient parent stack
  405. gs_ai_bt_children_end(ctx);
  406. }
  407. GS_API_DECL int16_t
  408. gs_ai_bt_selector_begin(struct gs_ai_bt_t* ctx)
  409. {
  410. // Get next node in stack
  411. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  412. node->name = "selector";
  413. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  414. // If not processing this node, return 0x00 to fail if check
  415. if (parent && parent->processed_child != -1
  416. && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  417. if (node->state == GS_AI_BT_STATE_RUNNING) {
  418. // Begin processing new child stack
  419. gs_ai_bt_children_begin(ctx, node);
  420. }
  421. return node->state;
  422. }
  423. GS_API_DECL void
  424. gs_ai_bt_selector_end(struct gs_ai_bt_t* ctx)
  425. {
  426. // Get top of parent stack for node
  427. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  428. for (uint32_t i = 0; i < node->num_children; ++i) {
  429. node->processed_child = i;
  430. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  431. gs_assert(child);
  432. if (child->state == GS_AI_BT_STATE_RUNNING) {break;}
  433. else if (child->state == GS_AI_BT_STATE_SUCCESS) {node->state = GS_AI_BT_STATE_SUCCESS; break;}
  434. else if (child->state == GS_AI_BT_STATE_FAILURE && i == node->num_children - 1) {
  435. node->processed_child = node->num_children;
  436. break;
  437. }
  438. }
  439. // If we're successful, we don't need to process anymore children
  440. if (node->state == GS_AI_BT_STATE_SUCCESS || !node->num_children) {
  441. node->state = GS_AI_BT_STATE_SUCCESS;
  442. // Pop all children off stack
  443. for (uint32_t i = 0; i < node->num_children; ++i) {
  444. gs_dyn_array_pop(ctx->stack);
  445. }
  446. ctx->current_idx = node->idx + 1;
  447. }
  448. // If processed_child < num_children, then we're still processing/running
  449. else if ((int32_t)node->processed_child < (int32_t)node->num_children) {
  450. node->state = GS_AI_BT_STATE_RUNNING;
  451. }
  452. else {
  453. node->state = GS_AI_BT_STATE_FAILURE;
  454. // Pop off all children off stack
  455. for (uint32_t i = 0; i < node->num_children; ++i) {
  456. gs_dyn_array_pop(ctx->stack);
  457. }
  458. ctx->current_idx = node->idx + 1;
  459. }
  460. // End child stack, pop off transient parent stack
  461. gs_ai_bt_children_end(ctx);
  462. }
  463. GS_API_DECL int16_t
  464. gs_ai_bt_sequence_begin(struct gs_ai_bt_t* ctx)
  465. {
  466. // Get next node in stack
  467. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  468. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  469. node->name = "sequence";
  470. // If not processing this node, return 0x00 to fail if check
  471. if (parent && parent->processed_child != -1 &&
  472. // && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  473. (parent->processed_child + parent->idx + 1) != node->idx) return 0x00;
  474. if (node->state == GS_AI_BT_STATE_RUNNING) {
  475. // Begin processing new child stack
  476. gs_ai_bt_children_begin(ctx, node);
  477. }
  478. return node->state;
  479. }
  480. GS_API_DECL void
  481. gs_ai_bt_sequence_end(struct gs_ai_bt_t* ctx)
  482. {
  483. // Get top of parent stack for node
  484. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  485. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, node->processed_child);
  486. gs_assert(child);
  487. if (child->state == GS_AI_BT_STATE_RUNNING) {/* Do nothing */}
  488. else if (child->state == GS_AI_BT_STATE_FAILURE) {node->state = GS_AI_BT_STATE_FAILURE;}
  489. else if (child->state == GS_AI_BT_STATE_SUCCESS) {
  490. node->processed_child++;
  491. }
  492. // For each child
  493. /*
  494. for (uint32_t i = 0; i < node->num_children; ++i) {
  495. node->processed_child = i;
  496. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  497. gs_assert(child);
  498. if (child->state == GS_AI_BT_STATE_RUNNING) {break;}
  499. else if (child->state == GS_AI_BT_STATE_FAILURE) {node->state = GS_AI_BT_STATE_FAILURE; break;}
  500. else if (child->state == GS_AI_BT_STATE_SUCCESS && i == node->num_children - 1) {
  501. node->processed_child = node->num_children;
  502. break;
  503. }
  504. }
  505. */
  506. // If we've failed, we don't need to process anymore children
  507. if (node->state == GS_AI_BT_STATE_FAILURE || !node->num_children) {
  508. node->state = GS_AI_BT_STATE_FAILURE;
  509. // Pop all children off stack, reset current idx?
  510. for (uint32_t i = 0; i < node->num_children; ++i) {
  511. gs_dyn_array_pop(ctx->stack);
  512. }
  513. ctx->current_idx = node->idx + 1;
  514. node->processed_child = 0;
  515. }
  516. // If processed_child < num_children, then we're still processing/running
  517. else if ((int32_t)node->processed_child < (int32_t)node->num_children) {
  518. node->state = GS_AI_BT_STATE_RUNNING;
  519. }
  520. else {
  521. node->state = GS_AI_BT_STATE_SUCCESS;
  522. // Pop off all children off stack
  523. for (uint32_t i = 0; i < node->num_children; ++i) {
  524. gs_dyn_array_pop(ctx->stack);
  525. }
  526. ctx->current_idx = node->idx + 1;
  527. node->processed_child = 0;
  528. }
  529. // End child stack, pop off transient parent stack
  530. gs_ai_bt_children_end(ctx);
  531. }
  532. GS_API_DECL int16_t
  533. gs_ai_bt_utility_selector_begin(struct gs_ai_bt_t* ctx, gs_ai_utility_action_list_desc_t* actions)
  534. {
  535. // Get next node in stack
  536. gs_ai_bt_node_t* node = gs_ai_bt_node_next_ext(ctx, -1);
  537. node->name = "utility_selector";
  538. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  539. // If not processing this node, return 0x00 to fail if check
  540. if (parent && parent->processed_child != -1
  541. && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  542. if (!actions) return 0x00;
  543. // Set to running if we're here
  544. node->state = GS_AI_BT_STATE_RUNNING;
  545. if (node->state == GS_AI_BT_STATE_RUNNING) {
  546. // Utility eval over actions list to find processed child index
  547. if (node->processed_child == -1 || actions->force_eval) {
  548. uint32_t ucnt = actions->size / sizeof(gs_ai_utility_action_desc_t);
  549. float hscore = FLT_MIN;
  550. int16_t ci = 0;
  551. for (uint32_t i = 0; i < ucnt; ++i) {
  552. float score = gs_ai_utility_action_evaluate(&actions->actions[i]);
  553. if (score > hscore) {
  554. hscore = score;
  555. ci = (int16_t)i;
  556. }
  557. }
  558. node->processed_child = ci;
  559. }
  560. // Begin processing new child stack
  561. gs_ai_bt_children_begin(ctx, node);
  562. }
  563. return node->state;
  564. }
  565. GS_API_DECL void
  566. gs_ai_bt_utility_selector_end(struct gs_ai_bt_t* ctx)
  567. {
  568. // Get top of parent stack for node
  569. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  570. // for (uint32_t i = 0; i < node->num_children; ++i) {
  571. // // node->processed_child = i;
  572. // gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  573. // gs_assert(child);
  574. // if (child->state == GS_AI_BT_STATE_RUNNING) {break;}
  575. // else if (child->state == GS_AI_BT_STATE_SUCCESS) {node->state = GS_AI_BT_STATE_SUCCESS; break;}
  576. // else if (child->state == GS_AI_BT_STATE_FAILURE && i == node->num_children - 1) {
  577. // node->processed_child = node->num_children;
  578. // break;
  579. // }
  580. // }
  581. if (node->processed_child != -1) {
  582. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, node->processed_child);
  583. gs_assert(child);
  584. node->state = child->state;
  585. }
  586. // If we're successful, we don't need to process anymore children
  587. if (node->state == GS_AI_BT_STATE_SUCCESS || !node->num_children) {
  588. node->state = GS_AI_BT_STATE_SUCCESS;
  589. // Pop all children off stack
  590. for (uint32_t i = 0; i < node->num_children; ++i) {
  591. gs_dyn_array_pop(ctx->stack);
  592. }
  593. ctx->current_idx = node->idx + 1;
  594. node->processed_child = -1;
  595. }
  596. // If processed_child < num_children, then we're still processing/running
  597. else if ((int32_t)node->processed_child < (int32_t)node->num_children && node->processed_child != -1) {
  598. node->state = GS_AI_BT_STATE_RUNNING;
  599. }
  600. else {
  601. node->state = GS_AI_BT_STATE_FAILURE;
  602. // Pop off all children off stack
  603. for (uint32_t i = 0; i < node->num_children; ++i) {
  604. gs_dyn_array_pop(ctx->stack);
  605. }
  606. ctx->current_idx = node->idx + 1;
  607. node->processed_child = -1;
  608. }
  609. // End child stack, pop off transient parent stack
  610. gs_ai_bt_children_end(ctx);
  611. }
  612. GS_API_DECL int16_t
  613. gs_ai_bt_repeater_begin(struct gs_ai_bt_t* ctx, uint32_t* count)
  614. {
  615. // Get next node in stack
  616. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  617. node->name = "repeater";
  618. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  619. // If not processing this node, return 0x00 to fail if check
  620. if (parent && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  621. // Set max children
  622. node->max_children = 1;
  623. if (node->state != GS_AI_BT_STATE_RUNNING)
  624. {
  625. if (count && *count)
  626. {
  627. (*count)--;
  628. if (*count) node->state = GS_AI_BT_STATE_RUNNING;
  629. }
  630. else if (!count)
  631. {
  632. node->state = GS_AI_BT_STATE_RUNNING;
  633. }
  634. }
  635. if (node->state == GS_AI_BT_STATE_RUNNING)
  636. {
  637. // Begin processing new child stack
  638. gs_ai_bt_children_begin(ctx, node);
  639. }
  640. return node->state;
  641. }
  642. GS_API_DECL void
  643. gs_ai_bt_repeater_end(struct gs_ai_bt_t* ctx)
  644. {
  645. // Get top of parent stack for node
  646. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  647. for (uint32_t i = 0; i < node->num_children; ++i)
  648. {
  649. node->processed_child = i;
  650. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  651. gs_assert(child);
  652. node->state = child->state;
  653. }
  654. if (node->state != GS_AI_BT_STATE_RUNNING)
  655. {
  656. node->state = GS_AI_BT_STATE_SUCCESS;
  657. // Pop all children and their children off stack
  658. uint32_t cnt = gs_dyn_array_size(ctx->stack);
  659. for (uint32_t i = node->idx + 1; i < cnt; ++i) {
  660. gs_dyn_array_pop(ctx->stack);
  661. }
  662. ctx->current_idx = node->idx + 1;
  663. }
  664. // End child stack, pop off transient parent stack
  665. gs_ai_bt_children_end(ctx);
  666. }
  667. GS_API_DECL int16_t
  668. gs_ai_bt_inverter_begin(struct gs_ai_bt_t* ctx)
  669. {
  670. // Get next node in stack
  671. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  672. node->name = "inverter";
  673. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  674. // If not processing this node, return 0x00 to fail if check
  675. if (parent && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  676. // Set max children
  677. node->max_children = 1;
  678. if (node->state == GS_AI_BT_STATE_RUNNING) {
  679. // Begin processing new child stack
  680. gs_ai_bt_children_begin(ctx, node);
  681. }
  682. return node->state;
  683. }
  684. GS_API_DECL void
  685. gs_ai_bt_inverter_end(struct gs_ai_bt_t* ctx)
  686. {
  687. // Get top of parent stack for node
  688. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  689. for (uint32_t i = 0; i < node->num_children; ++i) {
  690. node->processed_child = i;
  691. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  692. gs_assert(child);
  693. switch (child->state) {
  694. case GS_AI_BT_STATE_RUNNING: node->state = GS_AI_BT_STATE_RUNNING; break;
  695. case GS_AI_BT_STATE_FAILURE: node->state = GS_AI_BT_STATE_SUCCESS; break;
  696. case GS_AI_BT_STATE_SUCCESS: node->state = GS_AI_BT_STATE_FAILURE; break;
  697. }
  698. }
  699. if (node->state != GS_AI_BT_STATE_RUNNING) {
  700. // Pop all children and their children off stack
  701. uint32_t cnt = gs_dyn_array_size(ctx->stack);
  702. for (uint32_t i = node->idx + 1; i < cnt; ++i) {
  703. gs_dyn_array_pop(ctx->stack);
  704. }
  705. ctx->current_idx = node->idx + 1;
  706. }
  707. // End child stack, pop off transient parent stack
  708. gs_ai_bt_children_end(ctx);
  709. }
  710. GS_API_DECL int16_t
  711. gs_ai_bt_condition_begin(struct gs_ai_bt_t* ctx, bool condition)
  712. {
  713. // Get next node in stack
  714. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  715. node->name = "condition";
  716. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  717. // If not processing this node, return 0x00 to fail if check
  718. if (parent && gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return 0x00;
  719. // Set max children
  720. node->max_children = 1;
  721. if (node->state == GS_AI_BT_STATE_RUNNING) {
  722. if (condition) {
  723. // Begin processing new child stack
  724. gs_ai_bt_children_begin(ctx, node);
  725. }
  726. else {
  727. node->state = GS_AI_BT_STATE_FAILURE;
  728. }
  729. }
  730. return (node->state != GS_AI_BT_STATE_FAILURE);
  731. }
  732. GS_API_DECL void
  733. gs_ai_bt_condition_end(struct gs_ai_bt_t* ctx)
  734. {
  735. // Get top of parent stack for node
  736. gs_ai_bt_node_t* node = gs_ai_bt_parent_node_get(ctx);
  737. for (uint32_t i = 0; i < node->num_children; ++i) {
  738. node->processed_child = i;
  739. gs_ai_bt_node_t* child = gs_ai_bt_node_child_get(ctx, node, i);
  740. gs_assert(child);
  741. node->state = child->state; // pass through child state
  742. }
  743. if (node->state != GS_AI_BT_STATE_RUNNING) {
  744. // Pop all children and their children off stack
  745. uint32_t cnt = gs_dyn_array_size(ctx->stack);
  746. for (uint32_t i = node->idx + 1; i < cnt; ++i) {
  747. gs_dyn_array_pop(ctx->stack);
  748. }
  749. ctx->current_idx = node->idx + 1;
  750. }
  751. // End child stack, pop off transient parent stack
  752. gs_ai_bt_children_end(ctx);
  753. }
  754. GS_API_DECL void
  755. gs_ai_bt_leaf(struct gs_ai_bt_t* ctx, gs_ai_bt_leaf_func func)
  756. {
  757. // Next node
  758. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  759. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  760. node->name = "leaf";
  761. // If not processing this node, return 0x00 to fail if check
  762. // if (parent && parent->processed_child != -1 &&
  763. // gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return;
  764. if (parent && parent->processed_child != -1 && (parent->idx + parent->processed_child + 1) != node->idx) return;
  765. func(ctx, node);
  766. }
  767. GS_API_DECL void
  768. gs_ai_bt_wait(struct gs_ai_bt_t* ctx, float* time, float dt, float max)
  769. {
  770. // Next node
  771. gs_ai_bt_node_t* node = gs_ai_bt_node_next(ctx);
  772. gs_ai_bt_node_t* parent = gs_ai_bt_parent_node_get(ctx);
  773. node->name = "wait";
  774. // If not processing this node, return 0x00 to fail if check
  775. if (parent && parent->processed_child != -1 &&
  776. gs_ai_bt_node_child_get(ctx, parent, parent->processed_child)->idx != node->idx) return;
  777. if (!time) {
  778. node->state = GS_AI_BT_STATE_SUCCESS;
  779. }
  780. else if (max == 0.f) {
  781. node->state = GS_AI_BT_STATE_SUCCESS;
  782. }
  783. else {
  784. node->state = GS_AI_BT_STATE_RUNNING;
  785. *time += dt;
  786. if (*time >= max) {
  787. node->state = GS_AI_BT_STATE_SUCCESS;
  788. }
  789. }
  790. }
  791. //==================//
  792. //=== Utility AI ===//
  793. //=== Response Curves ===//
  794. GS_API_DECL float
  795. gs_ai_curve_logit(float m, float k, float c, float b, float x)
  796. {
  797. if (k == 0.f) k = 0.0001f;
  798. float z = (x / k) - c;
  799. return 0.5f * (log(z / (1.f - z)) / log(pow(100.f, m))) + b + 0.5f;
  800. }
  801. GS_API_DECL float
  802. gs_ai_curve_logistic(float m, float k, float c, float b, float x)
  803. {
  804. float z = 10 * m * (x - c - 0.5f);
  805. return k * (1.f / (1.f + pow(2.7183f, -z))) + b;
  806. }
  807. GS_API_DECL float
  808. gs_ai_curve_sin(float m, float k, float c, float b, float x)
  809. {
  810. return m * sin(pow((x - c), k)) + b;
  811. }
  812. GS_API_DECL float
  813. gs_ai_curve_cos(float m, float k, float c, float b, float x)
  814. {
  815. return m * cos(pow((x - c), k)) + b;
  816. }
  817. GS_API_DECL float
  818. gs_ai_curve_linearquad(float m, float k, float c, float b, float x)
  819. {
  820. return m * pow((x - c), k) + b;
  821. }
  822. GS_API_DECL float
  823. gs_ai_curve_binary_lt(float m, float k, float c, float b, float x)
  824. {
  825. return x < m ? k : c;
  826. }
  827. GS_API_DECL float
  828. gs_ai_curve_binary_lte(float m, float k, float c, float b, float x)
  829. {
  830. return x <= m ? k : c;
  831. }
  832. GS_API_DECL float
  833. gs_ai_curve_binary_gt(float m, float k, float c, float b, float x)
  834. {
  835. return x > m ? k : c;
  836. }
  837. GS_API_DECL float
  838. gs_ai_curve_binary_gte(float m, float k, float c, float b, float x)
  839. {
  840. return x >= m ? k : c;
  841. }
  842. GS_API_DECL float
  843. gs_ai_curve_binary_eq(float m, float k, float c, float b, float x)
  844. {
  845. return x >= m ? k : c;
  846. }
  847. GS_API_DECL float
  848. gs_ai_curve_nsigmoid(float m, float k, float c, float b, float x)
  849. {
  850. return m * (((x - c) - k * (x - c)) / (k - 2 * k * fabsf(x - c) + 1)) + b;
  851. }
  852. GS_API_DECL float
  853. gs_ai_curve_constant(float m, float k, float c, float b, float x)
  854. {
  855. return x;
  856. }
  857. GS_API_DECL float
  858. gs_ai_utility_action_evaluate(gs_ai_utility_action_desc_t* desc)
  859. {
  860. float val = 1.f;
  861. uint32_t cnt = desc->size / sizeof(gs_ai_utility_consideration_desc_t);
  862. if (!cnt) return 0.f;
  863. for (uint32_t i = 0; i < cnt; ++i)
  864. {
  865. gs_ai_utility_consideration_desc_t* cp = &desc->considerations[i];
  866. // Normalize range from bookends to [0.f, 1.f]
  867. float v = gs_map_range(cp->min, cp->max, 0.f, 1.f, cp->data);
  868. // Run response curve to calculate value
  869. gs_ai_utility_response_curve_desc_t* c = &cp->curve;
  870. v = c->func(c->slope, c->exponent, c->shift_x, c->shift_y, v);
  871. // Final score multiplied to val
  872. val *= v;
  873. }
  874. // Calculate compensation factor
  875. float mod_factor = 1.f - (1.f / cnt);
  876. float makeup = (1.f - val) * mod_factor;
  877. val = val + (makeup * val);
  878. return val;
  879. }
  880. #undef GS_AI_IMPL
  881. #endif // GS_AI_IMPL
  882. #endif // GS_AI_H