Dumb-ways-to-memorize
2D game
Data Structures | Macros | Enumerations | Functions | Variables
ai_interpret.h File Reference
#include "globals.h"
#include "parseobject.h"
+ Include dependency graph for ai_interpret.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ai_function_t
 

Macros

#define MAX_AI   255
 
#define AI_PATH   "AI/"
 
#define AI_FUNCTION_OBJECT   "Think"
 
#define AI_VAR_G_STR   "Variables"
 
#define AI_TYPE_STR   "AI_Type"
 
#define AI_TYPE_PRESET_STR   "preset"
 
#define AI_TYPE_VARIABLE_STR   "variable"
 
#define AI_TYPE_RECURSIVE_STR   "AI_Recursive"
 
#define AI_VAR_GRAVITY_STR   "gravity"
 
#define AI_VAR_STR   "variables"
 
#define AI_CHECK_STR   "condition"
 
#define AI_BASE_JUMP   -5
 
#define AI_BASE_SPEED   1
 
#define AI_BASE_THINK_FRAMES   1
 
#define AI_BASE_DAMAGE   5
 

Enumerations

enum  ai_type_t {
  AI_TYPE_NULL,
  AI_TYPE_PRESET = 0x1,
  AI_TYPE_VARIABLE,
  AI_TYPE_RECURSIVE
}
 
enum  ai_variables_t {
  AI_VAR_SPEED,
  AI_VAR_FRAMES,
  AI_VAR_TIME,
  AI_VAR_DAMAGE,
  AI_VAR_DIR_X,
  AI_VAR_DIR_Y,
  AI_VAR_CHECK,
  AI_VAR_MAX
}
 
enum  ai_flags_t {
  AI_FLAG_CHECK_PLAYER = 0x1,
  AI_FLAG_CHECK_OBJECT = 0x2,
  AI_FLAG_GRAVITY = 0x4
}
 
enum  ai_actions_t {
  AI_ACTION_NOTHING,
  AI_ACTION_MOVE,
  AI_ACTION_WALK,
  AI_ACTION_JUMP,
  AI_ACTION_ATTACK,
  AI_ACTION_MAX
}
 
enum  ai_conditions_t {
  AI_CONDITION_PLAYER_DISTANCE,
  AI_CONDITION_OBJECT_DISTANCE,
  AI_CONDITION_OBJECT_NAME,
  AI_CONDITION_LINK_AI,
  AI_CONDITION_LINK_ACTION,
  AI_CONDITION_MAX
}
 

Functions

ai_function_t * ParseAI (object_t *obj, char *g_str, char **variables)
 
ai_function_t * ParsePresetAI (object_t *obj, char *g_str)
 
void SetAI_Var (ai_function_t *function, char *data_str, ai_variables_t var_type)
 
void SetAI_Action (ai_function_t *function, object_t *obj, jsmntok_t *tok, char *g_str, ai_actions_t action_type)
 
void SetAI_Check (ai_function_t *function, char **variables_str, char *data_str, ai_conditions_t condition)
 
int InitAISystem ()
 
void ShutdownAISystem ()
 
ai_type_t StrToAI_Type (const char *str)
 
ai_actions_t StrToAI_Action (const char *str)
 
ai_conditions_t StrToAI_Condition (const char *str)
 
ai_variables_t StrToVariableType (const char *str)
 

Variables

char * gAI_Variables []
 
char * gAI_Actions []
 
char * gAI_Conditions []
 
ai_function_t * gVariableAIs
 
ai_function_t * gPresetAIs
 

Macro Definition Documentation

#define AI_BASE_DAMAGE   5

Definition at line 29 of file ai_interpret.h.

Referenced by SetAI_Var().

#define AI_BASE_JUMP   -5

Definition at line 26 of file ai_interpret.h.

Referenced by SetAI_Action().

#define AI_BASE_SPEED   1

Definition at line 27 of file ai_interpret.h.

Referenced by SetAI_Var().

#define AI_BASE_THINK_FRAMES   1

Definition at line 28 of file ai_interpret.h.

Referenced by SetAI_Var().

#define AI_CHECK_STR   "condition"

Definition at line 23 of file ai_interpret.h.

#define AI_FUNCTION_OBJECT   "Think"

Definition at line 10 of file ai_interpret.h.

Referenced by ParseAI(), and ParsePresetAI().

#define AI_PATH   "AI/"

Definition at line 9 of file ai_interpret.h.

#define AI_TYPE_PRESET_STR   "preset"

Definition at line 13 of file ai_interpret.h.

Referenced by StrToAI_Type().

#define AI_TYPE_RECURSIVE_STR   "AI_Recursive"

Definition at line 15 of file ai_interpret.h.

#define AI_TYPE_STR   "AI_Type"

Definition at line 12 of file ai_interpret.h.

Referenced by ParseAI(), and ParsePresetAI().

#define AI_TYPE_VARIABLE_STR   "variable"

Definition at line 14 of file ai_interpret.h.

Referenced by StrToAI_Type().

#define AI_VAR_G_STR   "Variables"

Definition at line 11 of file ai_interpret.h.

#define AI_VAR_GRAVITY_STR   "gravity"

Definition at line 16 of file ai_interpret.h.

#define AI_VAR_STR   "variables"

Definition at line 18 of file ai_interpret.h.

Referenced by ParseAI(), and ParsePresetAI().

#define MAX_AI   255

Definition at line 8 of file ai_interpret.h.

Referenced by InitAISystem().

Enumeration Type Documentation

Defines the enum for action types

Enumerator
AI_ACTION_NOTHING 
AI_ACTION_MOVE 
AI_ACTION_WALK 
AI_ACTION_JUMP 
AI_ACTION_ATTACK 
AI_ACTION_MAX 

Definition at line 64 of file ai_interpret.h.

Enumerator
AI_CONDITION_PLAYER_DISTANCE 
AI_CONDITION_OBJECT_DISTANCE 
AI_CONDITION_OBJECT_NAME 
AI_CONDITION_LINK_AI 
AI_CONDITION_LINK_ACTION 
AI_CONDITION_MAX 

Definition at line 75 of file ai_interpret.h.

enum ai_flags_t

Defines the enum for what flags are set on the ai

Enumerator
AI_FLAG_CHECK_PLAYER 
AI_FLAG_CHECK_OBJECT 
AI_FLAG_GRAVITY 

Definition at line 55 of file ai_interpret.h.

enum ai_type_t

Defines the enum for ai_types

Enumerator
AI_TYPE_NULL 
AI_TYPE_PRESET 

An enum constant representing the ai type preset option

AI_TYPE_VARIABLE 

An enum constant representing the ai type variable , which states there needs to be more values given

AI_TYPE_RECURSIVE 

An enum constant representing the ai type recursive option, which values turn into more functions

Definition at line 32 of file ai_interpret.h.

33 {
35  AI_TYPE_PRESET = 0x1, /**< An enum constant representing the ai type preset option */
36  AI_TYPE_VARIABLE, /**< An enum constant representing the ai type variable , which states there needs to be more values given */
37  AI_TYPE_RECURSIVE /**< An enum constant representing the ai type recursive option, which values turn into more functions */
38 }ai_type_t;
ai_type_t
Definition: ai_interpret.h:32

Defines the enum for the variables in mVariables

Enumerator
AI_VAR_SPEED 

An enum constant representing the ai variable speed option

AI_VAR_FRAMES 

An enum constant representing the ai variable for the amount of frames this action happens

AI_VAR_TIME 

An enum constant representing the ai variable for the amount of times to repeat this action

AI_VAR_DAMAGE 

An enum constant representing the ai variable for the damage this action should be

AI_VAR_DIR_X 

An enum constant representing the ai variable for the x direction

AI_VAR_DIR_Y 

An enum constant representing the ai variable for the y direction

AI_VAR_CHECK 

An enum constant representing the ai variable for whether there is a condition that needs to be met b4 the action can be done

AI_VAR_MAX 

Definition at line 41 of file ai_interpret.h.

42 {
43  AI_VAR_SPEED, /**< An enum constant representing the ai variable speed option */
44  AI_VAR_FRAMES, /**< An enum constant representing the ai variable for the amount of frames this action happens*/
45  AI_VAR_TIME, /**< An enum constant representing the ai variable for the amount of times to repeat this action */
46  AI_VAR_DAMAGE, /**< An enum constant representing the ai variable for the damage this action should be */
47  AI_VAR_DIR_X, /**< An enum constant representing the ai variable for the x direction */
48  AI_VAR_DIR_Y, /**< An enum constant representing the ai variable for the y direction */
49  AI_VAR_CHECK, /**< An enum constant representing the ai variable for whether there is a condition that needs to be met b4 the action can be done */
51 
ai_variables_t
Definition: ai_interpret.h:41

Function Documentation

int InitAISystem ( )

Init AI system, similar to Entity init, for now....

Returns
An int.
Author
Anthony Rios
Date
3/29/2016

Definition at line 646 of file ai_interpret.c.

References gPresetAIs, gVariableAIs, MAX_AI, and ShutdownAISystem().

Referenced by Setup().

647 {
648  if(gVariableAIs)
649  {
650  printf("Tried to init AI system, while already inited \n");
651  return -1;
652  }
653  gVariableAIs = (ai_function_t*) malloc(sizeof(ai_function_t)*(MAX_AI+1));
654  gPresetAIs = (ai_function_t*) malloc(sizeof(ai_function_t)*(MAX_AI+1));
655 
656  if(!gVariableAIs || !gPresetAIs)
657  {
658  printf("AI malloc error \n");
659  return -1;
660  }
661 
662  memset(gVariableAIs, 0, sizeof(ai_function_t)*(MAX_AI+1));
663  memset(gPresetAIs, 0, sizeof(ai_function_t)*(MAX_AI+1));
664  atexit(ShutdownAISystem);
665  return 0;
666 }
ai_function_t * gVariableAIs
Definition: ai_interpret.c:9
void ShutdownAISystem()
Definition: ai_interpret.c:668
#define MAX_AI
Definition: ai_interpret.h:8
ai_function_t * gPresetAIs
Definition: ai_interpret.c:10
ai_function_t* ParseAI ( object_t *  obj,
char *  g_str,
char **  variables 
)

Parses AI behavior using the variables given and the AI obj/file specified.

Parameters
[in,out]objIf non-null, the object.
[in,out]g_strIf non-null, the string.
[in,out]variablesIf non-null, the variables.
Returns
null if it fails, else a pointer to an ai_function_t.
Author
Anthony Rios
Date
3/29/2016

Definition at line 261 of file ai_interpret.c.

References AI_CONDITION_OBJECT_NAME, AI_FLAG_CHECK_OBJECT, AI_FLAG_GRAVITY, AI_FUNCTION_OBJECT, AI_TYPE_STR, AI_VAR_STR, CountMem(), CountObjectMembers(), FindKey(), FindObject(), FindValue(), gAI_Actions, gAI_Conditions, gAI_Variables, JsmnToString(), SetAI_Action(), SetAI_Check(), SetAI_Var(), StrToAI_Type(), and StrToInt().

Referenced by LoadLevel(), and SetAI_Check().

262 {
263  int i, j, k,children, position, variables_i, gravity;
264  ai_function_t *retVal;
265  jsmntok_t *temp_tok, *action_tok;
266  object_t *temp_obj, *action_obj, *variables_obj;
267  char *temp_str, *type_str, *cond_str,**variables_str;
268  if(!obj || !g_str)
269  {
270  return NULL;
271  }
272 
273  if(FindObject(obj, AI_VAR_STR))
274  {
275  gravity = 1;
276  } else
277  {
278  gravity = 0;
279  }
280 
281  temp_obj = FindObject(obj, AI_FUNCTION_OBJECT);
282  if(!temp_obj)
283  {
284  printf("No thinks in ai : %s \n", obj->name);
285  return NULL;
286  }
287 
288  type_str = FindValue(obj, AI_TYPE_STR, g_str);
289  if(!type_str)
290  {
291  printf("No types in ai : %s \n", obj->name);
292  return NULL;
293  }
294 
295  children = CountMem(temp_obj->children, sizeof(object_t));
296  retVal = (ai_function_t*) malloc(sizeof(ai_function_t)*(children+1));
297  memset(retVal, 0, sizeof(ai_function_t)*(children+1));
298  for(i = 0; i < children; i++)
299  {
300  for(j = 0; gAI_Variables[j]; j++ )
301  {
302  temp_str = FindValue(&temp_obj->children[i], gAI_Variables[j], g_str);
303  SetAI_Var(&retVal[i], variables[StrToInt(temp_str)], (ai_variables_t)j );
304  if(temp_str) free(temp_str);
305  temp_str = NULL;
306  }
307  for(j = 0; gAI_Actions[j]; j++ )
308  {
309  action_obj = FindObject(&temp_obj->children[i], gAI_Actions[j]);
310  action_tok = FindKey(temp_obj->children[i].keys, gAI_Actions[j], g_str);
311  if(!action_obj && !action_tok)
312  {
313  continue;
314  }
315 
316  SetAI_Action(&retVal[i],action_obj, action_tok, g_str, (ai_actions_t)j );
317  }
318  for(j = 0; gAI_Conditions[j]; j++)
319  {
320  cond_str = FindValue(&temp_obj->children[i], gAI_Conditions[j], g_str);
321  if(!cond_str)
322  {
323  continue;
324  }
325  variables_obj = FindObject(&temp_obj->children[i], AI_VAR_STR);
326  if(!variables_obj)
327  {
328  variables_str = NULL;
329  } else
330  {
331  variables_i = CountObjectMembers(variables_obj, g_str);
332  variables_str = (char**) malloc(sizeof(char*)*(variables_i+1));
333  variables_str[variables_i] = NULL;
334  for(k = 0; k < variables_i; k++)
335  {
336  temp_str = JsmnToString(&variables_obj->values[k], g_str);
337  variables_str[k] = temp_str ? variables[StrToInt(temp_str)] : NULL;
338  if(temp_str) free(temp_str);
339  temp_str = NULL;
340  }
341  }
342 
343  SetAI_Check(&retVal[i], variables_str, variables[StrToInt(cond_str)], (ai_conditions_t)j );
344  if(cond_str) free(cond_str);
345  cond_str = NULL;
346  }
347  retVal[i].mFlags |= gravity ? AI_FLAG_GRAVITY : 0;
348  retVal[i].mType = StrToAI_Type(type_str);
349  }
350 
351  //Linking and conditionals
352  for(i = 0; i < children; i++)
353  {
354  if(retVal[i].mLink)
355  {
356  continue;
357  } else
358  {
359  retVal[i].mLink = (i+1 == children) ? retVal: &retVal[i+1];
360  }
361  if(retVal[i].mFlags & AI_FLAG_CHECK_OBJECT)
362  {
363  temp_str = FindValue(&temp_obj->children[i], gAI_Conditions[AI_CONDITION_OBJECT_NAME], g_str);
364  if(!temp_str)
365  {
366  continue;
367  }
368  retVal[i].mObjectCheck = temp_str;
369  }
370  }
371  return retVal;
372 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
ai_conditions_t
Definition: ai_interpret.h:75
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
#define AI_VAR_STR
Definition: ai_interpret.h:18
int StrToInt(char *str)
Definition: mystrings.c:92
void SetAI_Var(ai_function_t *function, char *data_str, ai_variables_t var_type)
Definition: ai_interpret.c:481
int CountObjectMembers(object_t *obj, char *g_str)
Definition: parseobject.c:144
#define AI_FUNCTION_OBJECT
Definition: ai_interpret.h:10
void SetAI_Check(ai_function_t *function, char **variables_str, char *data_str, ai_conditions_t condition)
Definition: ai_interpret.c:601
char * gAI_Conditions[]
Definition: ai_interpret.c:14
jsmntok_t * FindKey(jsmntok_t *token, char *key, char *g_str)
Definition: mystrings.c:10
ai_actions_t
Definition: ai_interpret.h:64
void SetAI_Action(ai_function_t *function, object_t *obj, jsmntok_t *tok, char *g_str, ai_actions_t action_type)
Definition: ai_interpret.c:516
ai_variables_t
Definition: ai_interpret.h:41
#define AI_TYPE_STR
Definition: ai_interpret.h:12
char * gAI_Variables[]
Definition: ai_interpret.c:12
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
char * gAI_Actions[]
Definition: ai_interpret.c:13
ai_type_t StrToAI_Type(const char *str)
Definition: ai_interpret.c:679
ai_function_t* ParsePresetAI ( object_t *  obj,
char *  g_str 
)

Parse a preset AI, with values determined by whats in the file.

Parameters
[in,out]objIf non-null, the object.
[in,out]g_strIf non-null, the string.
Returns
null if it fails, else a pointer to an ai_function_t.
Author
Anthony Rios
Date
3/29/2016

Definition at line 374 of file ai_interpret.c.

References AI_CONDITION_OBJECT_NAME, AI_FLAG_CHECK_OBJECT, AI_FLAG_GRAVITY, AI_FUNCTION_OBJECT, AI_TYPE_STR, AI_VAR_STR, CountMem(), CountObjectMembers(), FindKey(), FindObject(), FindValue(), gAI_Actions, gAI_Conditions, gAI_Variables, JsmnToString(), SetAI_Action(), SetAI_Check(), SetAI_Var(), and StrToAI_Type().

Referenced by LoadLevel().

375 {
376  int i, j, k,children, position, variables, gravity;
377  ai_function_t *retVal;
378  jsmntok_t *temp_tok, *action_tok;
379  object_t *temp_obj, *action_obj, *variables_obj;
380  char *temp_str, *type_str, **variables_str;
381  if(!obj || !g_str)
382  {
383  return NULL;
384  }
385 
386  if(FindObject(obj, AI_VAR_STR))
387  {
388  gravity = 1;
389  } else
390  {
391  gravity = 0;
392  }
393  temp_obj = FindObject(obj, AI_FUNCTION_OBJECT);
394  if(!temp_obj)
395  {
396  printf("No thinks in ai : %s \n", obj->name);
397  return NULL;
398  }
399  temp_tok = FindKey(obj->keys, AI_TYPE_STR, g_str);
400  if(!temp_tok)
401  {
402  printf("No types in ai : %s \n", obj->name);
403  return NULL;
404  }
405  position = temp_tok - obj->keys;
406  type_str = JsmnToString(&obj->values[position], g_str);
407 
408  children = CountMem(temp_obj->children, sizeof(object_t));
409  retVal = (ai_function_t*) malloc(sizeof(ai_function_t)*(children+1));
410  memset(retVal, 0, sizeof(ai_function_t)*(children+1));
411  for(i = 0; i < children; i++)
412  {
413  for(j = 0; gAI_Variables[j]; j++ )
414  {
415  temp_str = FindValue(&temp_obj->children[i], gAI_Variables[j] ,g_str);
416 
417  SetAI_Var(&retVal[i], temp_str, (ai_variables_t)j );
418  if(temp_str) free(temp_str);
419  temp_str = NULL;
420  }
421  for(j = 0; gAI_Actions[j]; j++ )
422  {
423  action_obj = FindObject(&temp_obj->children[i], gAI_Actions[j]);
424  action_tok = FindKey(temp_obj->children[i].keys, gAI_Actions[j], g_str);
425  if(!action_obj && !action_tok)
426  {
427  continue;
428  }
429  SetAI_Action(&retVal[i],action_obj, action_tok, g_str, (ai_actions_t)j );
430  }
431  for(j = 0; gAI_Conditions[j]; j++)
432  {
433  temp_str = FindValue(&temp_obj->children[i], gAI_Conditions[j] ,g_str);
434  if(!temp_str)
435  {
436  continue;
437  }
438  variables_obj = FindObject(&temp_obj->children[i], AI_VAR_STR);
439  if(!variables_obj)
440  {
441  variables_str = NULL;
442  } else
443  {
444  variables = CountObjectMembers(variables_obj, g_str);
445  variables_str = (char**) malloc(sizeof(char*)*(variables+1));
446  variables_str[variables] = NULL;
447  for(k = 0; k < variables; k++)
448  {
449  variables_str[k] = JsmnToString(&variables_obj->values[k],g_str);
450  }
451  }
452 
453  SetAI_Check(&retVal[i], variables_str, temp_str, (ai_conditions_t)j );
454  if(temp_str) free(temp_str);
455  temp_str = NULL;
456  }
457  retVal[i].mFlags |= gravity ? AI_FLAG_GRAVITY : 0;
458  retVal[i].mType = StrToAI_Type(type_str);
459  }
460 
461  //Linking and conditionals
462  for(i = 0; i < children; i++)
463  {
464  if(!retVal[i].mLink)
465  {
466  retVal[i].mLink = (i+1 == children) ? retVal: &retVal[i+1];
467  }
468  if(retVal[i].mFlags & AI_FLAG_CHECK_OBJECT)
469  {
470  temp_str = FindValue(&temp_obj->children[i], gAI_Conditions[AI_CONDITION_OBJECT_NAME], g_str);
471  if(!temp_str)
472  {
473  continue;
474  }
475  retVal[i].mObjectCheck = temp_str;
476  }
477  }
478  return retVal;
479 }
Definition: jsmn.h:40
int CountMem(void *src, int size_type)
Definition: mymath.c:51
object_t * FindObject(object_t *obj, char *name)
Definition: parseobject.c:120
ai_conditions_t
Definition: ai_interpret.h:75
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
#define AI_VAR_STR
Definition: ai_interpret.h:18
void SetAI_Var(ai_function_t *function, char *data_str, ai_variables_t var_type)
Definition: ai_interpret.c:481
int CountObjectMembers(object_t *obj, char *g_str)
Definition: parseobject.c:144
#define AI_FUNCTION_OBJECT
Definition: ai_interpret.h:10
void SetAI_Check(ai_function_t *function, char **variables_str, char *data_str, ai_conditions_t condition)
Definition: ai_interpret.c:601
char * gAI_Conditions[]
Definition: ai_interpret.c:14
jsmntok_t * FindKey(jsmntok_t *token, char *key, char *g_str)
Definition: mystrings.c:10
ai_actions_t
Definition: ai_interpret.h:64
void SetAI_Action(ai_function_t *function, object_t *obj, jsmntok_t *tok, char *g_str, ai_actions_t action_type)
Definition: ai_interpret.c:516
ai_variables_t
Definition: ai_interpret.h:41
#define AI_TYPE_STR
Definition: ai_interpret.h:12
char * gAI_Variables[]
Definition: ai_interpret.c:12
char * FindValue(struct object_s *obj, char *key, char *g_str)
Definition: mystrings.c:53
char * gAI_Actions[]
Definition: ai_interpret.c:13
ai_type_t StrToAI_Type(const char *str)
Definition: ai_interpret.c:679
void SetAI_Action ( ai_function_t *  function,
object_t *  obj,
jsmntok_t tok,
char *  g_str,
ai_actions_t  action_type 
)

Sets AI action defined in ai_actions to the ai_function

Parameters
[in,out]functionIf non-null, the function.
[in,out]objIf non-null, the object.
[in,out]tokIf non-null, the tok.
[in,out]g_strIf non-null, the string.
[in,out]action_typeIf non-null, the action type.
Author
Anthony Rios
Date
3/29/2016

Definition at line 516 of file ai_interpret.c.

References AI_ACTION_ATTACK, AI_ACTION_JUMP, AI_ACTION_MOVE, AI_ACTION_NOTHING, AI_ACTION_WALK, AI_BASE_JUMP, AI_VAR_DIR_X, AI_VAR_DIR_Y, JsmnToString(), ParseToVec2(), vec2_t::x, and vec2_t::y.

Referenced by ParseAI(), and ParsePresetAI().

517 {
518  vec2_t *temp_vec2;
519  if(!action_type || !function)
520  {
521  return;
522  }
523  switch(action_type)
524  {
525  case AI_ACTION_NOTHING:
526  {
527  function->mAction = AI_ACTION_NOTHING;
528  break;
529  }
530  case AI_ACTION_MOVE:
531  {
532  if(!obj)
533  {
534  function->mVariables[AI_VAR_DIR_X] = 0;
535  function->mVariables[AI_VAR_DIR_Y] = 0;
536 
537  } else if( (temp_vec2 = ParseToVec2(obj, g_str)) != NULL)
538  {
539  function->mVariables[AI_VAR_DIR_X] = temp_vec2->x;
540  function->mVariables[AI_VAR_DIR_X] = temp_vec2->y;
541  } else
542  {
543  function->mVariables[AI_VAR_DIR_X] = 0;
544  function->mVariables[AI_VAR_DIR_Y] = 0;
545  }
546 
547  function->mAction = AI_ACTION_MOVE;
548  break;
549  }
550  case AI_ACTION_WALK:
551  {
552  if(!obj)
553  {
554  function->mVariables[AI_VAR_DIR_X] = 0;
555  function->mVariables[AI_VAR_DIR_Y] = 0;
556 
557  } else if( (temp_vec2 = ParseToVec2(obj, g_str)) != NULL)
558  {
559  function->mVariables[AI_VAR_DIR_X] = temp_vec2->x;
560  function->mVariables[AI_VAR_DIR_X] = temp_vec2->y;
561  } else
562  {
563  function->mVariables[AI_VAR_DIR_X] = 0;
564  function->mVariables[AI_VAR_DIR_Y] = 0;
565  }
566 
567  function->mAction = AI_ACTION_WALK;
568  break;
569  }
570  case AI_ACTION_JUMP:
571  {
572  if(!obj)
573  {
574  function->mVariables[AI_VAR_DIR_X] = 0;
575  function->mVariables[AI_VAR_DIR_Y] = AI_BASE_JUMP;
576 
577  } else if( (temp_vec2 = ParseToVec2(obj, g_str)) != NULL)
578  {
579  function->mVariables[AI_VAR_DIR_X] = temp_vec2->x;
580  function->mVariables[AI_VAR_DIR_X] = temp_vec2->y;
581  } else
582  {
583  function->mVariables[AI_VAR_DIR_X] = 0;
584  function->mVariables[AI_VAR_DIR_Y] = AI_BASE_JUMP;
585  }
586 
587  function->mAction = AI_ACTION_JUMP;
588  break;
589  }
590  case AI_ACTION_ATTACK:
591  {
592  function->mAction = AI_ACTION_ATTACK;
593  function->mObject = JsmnToString(tok, g_str);
594  break;
595  }
596  default:
597  break;
598  }
599 }
int y
Definition: globals.h:22
#define AI_BASE_JUMP
Definition: ai_interpret.h:26
char * JsmnToString(jsmntok_t *token, char *g_str)
Definition: mystrings.c:34
vec2_t * ParseToVec2(struct object_s *object, char *str)
int x
Definition: globals.h:21
Definition: globals.h:19
void SetAI_Check ( ai_function_t *  function,
char **  variables_str,
char *  data_str,
ai_conditions_t  condition 
)

Sets checks that need to occur for the ai_function to be called.

Parameters
[in,out]functionIf non-null, the function.
[in,out]variables_strIf non-null, the variables string.
[in,out]data_strIf non-null, the data string.
[in,out]check_strIf non-null, the condition type.
Author
Anthony Rios
Date
3/29/2016

Definition at line 601 of file ai_interpret.c.

References AI_CONDITION_LINK_ACTION, AI_CONDITION_LINK_AI, AI_CONDITION_OBJECT_DISTANCE, AI_CONDITION_PLAYER_DISTANCE, AI_FLAG_CHECK_OBJECT, AI_FLAG_CHECK_PLAYER, AI_VAR_CHECK, ConvertFileToUseable(), ParseAI(), ParseToObject(), and StrToInt().

Referenced by ParseAI(), and ParsePresetAI().

602 {
603  ai_function_t *temp_ai;
604  object_t *temp_obj;
605  jsmntok_t *temp_tok;
606  char *temp_str;
607  if(!condition || !function)
608  {
609  return;
610  }
611  switch(condition)
612  {
614  {
615  function->mVariables[AI_VAR_CHECK] = StrToInt(data_str);
616  break;
617  }
619  {
620  function->mVariables[AI_VAR_CHECK] = StrToInt(data_str);
621  function->mFlags |= AI_FLAG_CHECK_PLAYER;
622  break;
623  }
625  {
626  function->mVariables[AI_VAR_CHECK] = StrToInt(data_str);
627  function->mFlags |= AI_FLAG_CHECK_OBJECT;
628  break;
629  }
631  {
632  ConvertFileToUseable(data_str, NULL, &temp_str, &temp_tok);
633  if(!temp_str || !temp_tok)
634  {
635  return;
636  }
637  temp_obj = ParseToObject(temp_tok, temp_str);
638  function->mLink = ParseAI(temp_obj, temp_str, variables_str);
639  break;
640  }
641  default:
642  break;
643  }
644 }
Definition: jsmn.h:40
int StrToInt(char *str)
Definition: mystrings.c:92
int ConvertFileToUseable(char *fileName, jsmn_parser *parser, char **stringStorage, jsmntok_t **jsmnStorage)
Definition: mystrings.c:192
object_t * ParseToObject(jsmntok_t *token, char *g_str)
Definition: parseobject.c:8
ai_function_t * ParseAI(object_t *obj, char *g_str, char **variables)
Definition: ai_interpret.c:261
void SetAI_Var ( ai_function_t *  function,
char *  data_str,
ai_variables_t  var_type 
)

Sets AI variables in ai_function->mVariables.

Parameters
[in,out]functionIf non-null, the function.
[in,out]data_strIf non-null, the data string.
[in,out]var_typeIf non-null, the variable type.
Author
Anthony Rios
Date
3/29/2016

Definition at line 481 of file ai_interpret.c.

References AI_BASE_DAMAGE, AI_BASE_SPEED, AI_BASE_THINK_FRAMES, AI_VAR_DAMAGE, AI_VAR_FRAMES, AI_VAR_SPEED, AI_VAR_TIME, and StrToInt().

Referenced by ParseAI(), and ParsePresetAI().

482 {
483  if(!var_type)
484  {
485  return;
486  }
487 
488  switch(var_type)
489  {
490  case AI_VAR_SPEED:
491  {
492  function->mVariables[AI_VAR_SPEED] = data_str ? StrToInt(data_str) : AI_BASE_SPEED;
493  break;
494  }
495  case AI_VAR_FRAMES:
496  {
497  function->mVariables[AI_VAR_FRAMES] = data_str ? StrToInt(data_str) : AI_BASE_THINK_FRAMES;
498  break;
499  }
500  case AI_VAR_TIME:
501  {
502  function->mVariables[AI_VAR_TIME] = data_str ? StrToInt(data_str) : 1;
503  break;
504  }
505  case AI_VAR_DAMAGE:
506  {
507  function->mVariables[AI_VAR_DAMAGE] = data_str ? StrToInt(data_str) : AI_BASE_DAMAGE;
508  break;
509  }
510  default:
511  break;
512  }
513 
514 }
#define AI_BASE_DAMAGE
Definition: ai_interpret.h:29
int StrToInt(char *str)
Definition: mystrings.c:92
#define AI_BASE_THINK_FRAMES
Definition: ai_interpret.h:28
#define AI_BASE_SPEED
Definition: ai_interpret.h:27
void ShutdownAISystem ( )

Shutdown AI system, just frees the global variables simple.

Author
Anthony Rios
Date
3/30/2016

Definition at line 668 of file ai_interpret.c.

References gPresetAIs, and gVariableAIs.

Referenced by InitAISystem().

669 {
670  if(!gVariableAIs || !gPresetAIs)
671  {
672  printf("AI not initialized before shutdown called \n");
673  return;
674  }
675  free(gVariableAIs);
676  free(gPresetAIs);
677 }
ai_function_t * gVariableAIs
Definition: ai_interpret.c:9
ai_function_t * gPresetAIs
Definition: ai_interpret.c:10
ai_actions_t StrToAI_Action ( const char *  str)

Definition at line 691 of file ai_interpret.c.

References AI_ACTION_MAX, and gAI_Actions.

692 {
693  int i;
694  for(i = 0; gAI_Actions[i]; i++)
695  {
696  if(!strcmp(gAI_Actions[i], str))
697  {
698  return (ai_actions_t)i;
699  }
700  }
701  return AI_ACTION_MAX;
702 }
ai_actions_t
Definition: ai_interpret.h:64
char * gAI_Actions[]
Definition: ai_interpret.c:13
ai_conditions_t StrToAI_Condition ( const char *  str)

Definition at line 704 of file ai_interpret.c.

References AI_CONDITION_MAX, and gAI_Conditions.

705 {
706  int i;
707  for(i = 0; gAI_Conditions[i]; i++)
708  {
709  if(!strcmp(gAI_Conditions[i], str))
710  {
711  return (ai_conditions_t)i;
712  }
713  }
714  return AI_CONDITION_MAX;
715 }
ai_conditions_t
Definition: ai_interpret.h:75
char * gAI_Conditions[]
Definition: ai_interpret.c:14
ai_type_t StrToAI_Type ( const char *  str)

Converts a str to an AI type.

Parameters
strThe string.
Returns
str as an ai_type_t.
Author
Anthony Rios
Date
3/29/2016

Definition at line 679 of file ai_interpret.c.

References AI_TYPE_NULL, AI_TYPE_PRESET, AI_TYPE_PRESET_STR, AI_TYPE_VARIABLE, and AI_TYPE_VARIABLE_STR.

Referenced by ParseAI(), and ParsePresetAI().

680 {
681  if(!strcmp(str, AI_TYPE_PRESET_STR))
682  {
683  return AI_TYPE_PRESET;
684  } else if(!strcmp(str, AI_TYPE_VARIABLE_STR))
685  {
686  return AI_TYPE_VARIABLE;
687  }
688  return AI_TYPE_NULL;
689 }
#define AI_TYPE_VARIABLE_STR
Definition: ai_interpret.h:14
#define AI_TYPE_PRESET_STR
Definition: ai_interpret.h:13
ai_variables_t StrToVariableType ( const char *  str)

Definition at line 717 of file ai_interpret.c.

References AI_VAR_MAX, and gAI_Variables.

718 {
719  int i;
720  for(i = 0; gAI_Variables[i]; i++)
721  {
722  if(!strcmp(gAI_Variables[i], str))
723  {
724  return (ai_variables_t)i;
725  }
726  }
727  return AI_VAR_MAX;
728 }
ai_variables_t
Definition: ai_interpret.h:41
char * gAI_Variables[]
Definition: ai_interpret.c:12

Variable Documentation

char* gAI_Actions[]

The actions that are related to ai functions

Definition at line 13 of file ai_interpret.c.

Referenced by ParseAI(), ParsePresetAI(), and StrToAI_Action().

char* gAI_Conditions[]

The conditions for the ai function to be executed

Definition at line 14 of file ai_interpret.c.

Referenced by ParseAI(), ParsePresetAI(), and StrToAI_Condition().

char* gAI_Variables[]

The variables to be parsed for ai data

Definition at line 12 of file ai_interpret.c.

Referenced by ParseAI(), ParsePresetAI(), and StrToVariableType().

ai_function_t* gPresetAIs

The AI's of type preset

Definition at line 10 of file ai_interpret.c.

Referenced by InitAISystem(), and ShutdownAISystem().

ai_function_t* gVariableAIs

The AI's of type variable

Definition at line 9 of file ai_interpret.c.

Referenced by InitAISystem(), and ShutdownAISystem().