Dumb-ways-to-memorize
2D game
Functions | Variables
ai_interpret.c File Reference
#include "ai_interpret.h"
#include "entity.h"
#include "parseentity.h"
#include "parsepowerup.h"
#include "mystrings.h"
#include <stdio.h>
#include <stdlib.h>
+ Include dependency graph for ai_interpret.c:

Go to the source code of this file.

Functions

void NothingAI (entity_t *ent)
 
void MoveAI (entity_t *ent)
 
void WalkAI (entity_t *ent)
 
void JumpAI (entity_t *ent)
 
void AttackAI (entity_t *ent)
 
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

ai_function_t * gVariableAIs = NULL
 
ai_function_t * gPresetAIs = NULL
 
char * gAI_Variables [] = {"speed", "frames", "time", "damage", 0}
 
char * gAI_Actions [] = {"nothing", "move", "walk", "jump", "attack", 0}
 
char * gAI_Conditions [] = {"distance_player", "distance_object", "object_check", "link_ai", "link_action", 0}
 
void(*)(entity_t *) GetFunctionAI (ai_function_t *data)
 

Function Documentation

void AttackAI ( entity_t *  ent)

AI for entity attacks, "spawns" a hit box / entity in front of Entity

Note
Currently Untested
Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016

Definition at line 200 of file ai_interpret.c.

References AI_FLAG_GRAVITY, AI_VAR_DAMAGE, AI_VAR_DIR_X, AI_VAR_DIR_Y, AI_VAR_FRAMES, AI_VAR_TIME, COLLISION_TYPE_RAGDOLL, FindCachedEntity(), FRAME_DELAY, and Spawn().

201 {
202  int flags;
203  vec2_t temp_vec2;
204  entity_t *temp_ent;
205  if(!ent->mData || !ent)
206  {
207  printf("MoveAI given a null paramerter \n");
208  return;
209  }
210  flags = ent->mData->mFlags;
211 
212  //Standard Vars
213  ent->mCollisionType = COLLISION_TYPE_RAGDOLL;
214  ent->mNextThink = SDL_GetTicks() + ent->mData->mVariables[AI_VAR_FRAMES]*FRAME_DELAY;
215  ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE];
216  ent->mWeight = (flags & AI_FLAG_GRAVITY) ? 0 : FindCachedEntity(ent->mName)->mWeight;
217 
218  if(ent->mData->mObject)
219  {
220  temp_ent = FindCachedEntity(ent->mData->mObject);
221  if(temp_ent)
222  {
223  temp_ent->mVelocity.x = ent->mData->mVariables[AI_VAR_DIR_X];
224  temp_ent->mVelocity.y = ent->mData->mVariables[AI_VAR_DIR_Y];
225  Spawn(ent, temp_ent);
226  }
227 
228  }
229 
230  //Check Data
231  if( --ent->mData->mVariables[AI_VAR_TIME] == 0)
232  {
233  ent->mData->mVariables[AI_VAR_TIME] = 1;
234  ent->mData = ent->mData->mLink;
235  }
236 }
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
void Spawn(entity_t *targ, entity_t *info)
Definition: parsepowerup.c:41
#define FRAME_DELAY
Definition: globals.h:135
Definition: globals.h:19
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
void JumpAI ( entity_t *  ent)

AI to change velocity of the entity, defaults to a jumping movement.

Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016

Definition at line 147 of file ai_interpret.c.

References AI_FLAG_GRAVITY, AI_VAR_DAMAGE, AI_VAR_DIR_X, AI_VAR_DIR_Y, AI_VAR_FRAMES, AI_VAR_SPEED, AI_VAR_TIME, COLLISION_TYPE_RAGDOLL, FindCachedEntity(), FRAME_DELAY, gCurrentTime, Vec2Add(), Vec2MultiplyScalar(), vec2_t::x, and vec2_t::y.

148 {
149  int flags;
150  vec2_t temp_vec2;
151  if(!ent->mData || !ent)
152  {
153  printf("MoveAI given a null paramerter \n");
154  return;
155  }
156  flags = ent->mData->mFlags;
157 
158  //Standard Vars
159  ent->mCollisionType = COLLISION_TYPE_RAGDOLL;
160  ent->mNextThink = gCurrentTime + ent->mData->mVariables[AI_VAR_FRAMES]*FRAME_DELAY;
161  ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE];
162  ent->mWeight = (flags & AI_FLAG_GRAVITY) ? 0 : FindCachedEntity(ent->mName)->mWeight;
163 
164  //Move
165  if(ent->mVelocity.y == 0)
166  {
167  temp_vec2.x = ent->mData->mVariables[AI_VAR_DIR_X];
168  temp_vec2.y = ent->mData->mVariables[AI_VAR_DIR_Y];
169  //TODO: normalize temp_vec2
170  Vec2MultiplyScalar(&temp_vec2,ent->mData->mVariables[AI_VAR_SPEED],&temp_vec2);
171  if( (ent->mVelocity.x == temp_vec2.x) & (ent->mVelocity.y == temp_vec2.y) )
172  {
173  ;
174  } else
175  {
176  Vec2Add(&temp_vec2, &ent->mVelocity, &ent->mVelocity);
177  }
178 
179  }
180 
181  //Check Data
182  if( --ent->mData->mVariables[AI_VAR_TIME] == 0)
183  {
184  ent->mData->mVariables[AI_VAR_TIME] = 1;
185  ent->mData = ent->mData->mLink;
186  }
187 }
int y
Definition: globals.h:22
void Vec2MultiplyScalar(vec2_t *A, int B, vec2_t *C)
Definition: mymath.c:41
unsigned int gCurrentTime
Definition: game.c:50
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
#define FRAME_DELAY
Definition: globals.h:135
int x
Definition: globals.h:21
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
Definition: globals.h:19
void MoveAI ( entity_t *  ent)

AI to Move ent from point A to B w/o collisions.

Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016

Learn how to lerp.

Definition at line 61 of file ai_interpret.c.

References AI_FLAG_GRAVITY, AI_VAR_DAMAGE, AI_VAR_DIR_X, AI_VAR_DIR_Y, AI_VAR_FRAMES, AI_VAR_SPEED, AI_VAR_TIME, COLLISION_TYPE_CLIP, FindCachedEntity(), FRAME_DELAY, gCurrentTime, Vec2Add(), Vec2MultiplyScalar(), vec2_t::x, and vec2_t::y.

62 {
63  int flags;
64  vec2_t temp_vec2;
65  if(!ent->mData || !ent)
66  {
67  printf("MoveAI given a null paramerter \n");
68  return;
69  }
70  flags = ent->mData->mFlags;
71 
72  //Standard Vars
73  ent->mCollisionType = COLLISION_TYPE_CLIP;
74  ent->mNextThink = gCurrentTime + ent->mData->mVariables[AI_VAR_FRAMES]*FRAME_DELAY;
75  ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE];
76  ent->mWeight = (flags & AI_FLAG_GRAVITY) ? 0 : FindCachedEntity(ent->mName)->mWeight;
77 
78  //Move
79  temp_vec2.x = ent->mData->mVariables[AI_VAR_DIR_X];
80  temp_vec2.y = ent->mData->mVariables[AI_VAR_DIR_Y];
81  //TODO: normalize temp_vec2
82  Vec2MultiplyScalar(&temp_vec2,ent->mData->mVariables[AI_VAR_SPEED],&temp_vec2);
83  Vec2Add(&temp_vec2, &ent->mVelocity, &ent->mVelocity);
84 
85  //Check Data
86  if( --ent->mData->mVariables[AI_VAR_TIME] == 0)
87  {
88  ent->mData->mVariables[AI_VAR_TIME] = 1;
89  ent->mData = ent->mData->mLink;
90  }
91 
92 
93 }
int y
Definition: globals.h:22
void Vec2MultiplyScalar(vec2_t *A, int B, vec2_t *C)
Definition: mymath.c:41
unsigned int gCurrentTime
Definition: game.c:50
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
#define FRAME_DELAY
Definition: globals.h:135
int x
Definition: globals.h:21
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
Definition: globals.h:19
void NothingAI ( entity_t *  ent)

AI that does nothing for its think, except make sure values are set.

Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016

Definition at line 25 of file ai_interpret.c.

References AI_FLAG_GRAVITY, AI_VAR_DAMAGE, AI_VAR_FRAMES, AI_VAR_TIME, COLLISION_TYPE_RAGDOLL, FindCachedEntity(), FRAME_DELAY, and gCurrentTime.

26 {
27  int flags;
28  vec2_t temp_vec2;
29  if(!ent->mData || !ent)
30  {
31  printf("MoveAI given a null paramerter \n");
32  return;
33  }
34  flags = ent->mData->mFlags;
35 
36  //Standard Vars
37  ent->mCollisionType = COLLISION_TYPE_RAGDOLL;
38  ent->mNextThink = gCurrentTime + ent->mData->mVariables[AI_VAR_FRAMES]*FRAME_DELAY;
39  ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE];
40  ent->mWeight = (flags & AI_FLAG_GRAVITY) ? 0 : FindCachedEntity(ent->mName)->mWeight;
41 
42  //Check Data
43  if( --ent->mData->mVariables[AI_VAR_TIME] == 0)
44  {
45  ent->mData->mVariables[AI_VAR_TIME] = 1;
46  ent->mData = ent->mData->mLink;
47  }
48 }
unsigned int gCurrentTime
Definition: game.c:50
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
#define FRAME_DELAY
Definition: globals.h:135
Definition: globals.h:19
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
void WalkAI ( entity_t *  ent)

AI to "walk" entity from point A to B w/ collisions.

Parameters
[in,out]entIf non-null, the ent.
Author
Anthony Rios
Date
3/29/2016
Todo:
Lerp

Definition at line 106 of file ai_interpret.c.

References AI_FLAG_GRAVITY, AI_VAR_DAMAGE, AI_VAR_DIR_X, AI_VAR_DIR_Y, AI_VAR_FRAMES, AI_VAR_SPEED, AI_VAR_TIME, COLLISION_TYPE_RAGDOLL, FindCachedEntity(), FRAME_DELAY, gCurrentTime, Vec2Add(), Vec2MultiplyScalar(), vec2_t::x, and vec2_t::y.

107 {
108  int flags;
109  vec2_t temp_vec2;
110  if(!ent->mData || !ent)
111  {
112  printf("MoveAI given a null paramerter \n");
113  return;
114  }
115  flags = ent->mData->mFlags;
116 
117  //Standard Vars
118  ent->mCollisionType = COLLISION_TYPE_RAGDOLL;
119  ent->mNextThink = gCurrentTime + ent->mData->mVariables[AI_VAR_FRAMES]*FRAME_DELAY;
120  ent->mDamage = ent->mData->mVariables[AI_VAR_DAMAGE];
121  ent->mWeight = (flags & AI_FLAG_GRAVITY) ? 0 : FindCachedEntity(ent->mName)->mWeight;
122 
123  //Move
124  temp_vec2.x = ent->mData->mVariables[AI_VAR_DIR_X];
125  temp_vec2.y = ent->mData->mVariables[AI_VAR_DIR_Y];
126  //TODO: normalize temp_vec2
127  Vec2MultiplyScalar(&temp_vec2,ent->mData->mVariables[AI_VAR_SPEED],&temp_vec2);
128  Vec2Add(&temp_vec2, &ent->mVelocity, &ent->mVelocity);
129 
130  //Check Data
131  if( --ent->mData->mVariables[AI_VAR_TIME] == 0)
132  {
133  ent->mData->mVariables[AI_VAR_TIME] = 1;
134  ent->mData = ent->mData->mLink;
135  }
136 }
int y
Definition: globals.h:22
void Vec2MultiplyScalar(vec2_t *A, int B, vec2_t *C)
Definition: mymath.c:41
unsigned int gCurrentTime
Definition: game.c:50
entity_t * FindCachedEntity(const char *name)
Definition: entity.c:301
#define FRAME_DELAY
Definition: globals.h:135
int x
Definition: globals.h:21
void Vec2Add(vec2_t *A, vec2_t *B, vec2_t *C)
Definition: mymath.c:20
Definition: globals.h:19

Variable Documentation

char* gAI_Actions[] = {"nothing", "move", "walk", "jump", "attack", 0}

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[] = {"distance_player", "distance_object", "object_check", "link_ai", "link_action", 0}

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[] = {"speed", "frames", "time", "damage", 0}

The variables to be parsed for ai data

Definition at line 12 of file ai_interpret.c.

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

void(*)(entity_t *) GetFunctionAI(ai_function_t *data)

Returns a function pointer to the think function, given ai_function data.

Parameters
[in,out]parameter1If non-null, the first parameter.
Returns
null if it fails, else a GetFunctionAI(ai_function_t *data.
Author
Anthony Rios
Date
3/29/2016

Definition at line 238 of file ai_interpret.c.

Referenced by ThinkEnemy().

239 {
240  if(!data)
241  {
242  return NULL;
243  }
244  switch(data->mAction)
245  {
246  case(AI_ACTION_NOTHING) :
247  return NothingAI;
248  case(AI_ACTION_MOVE):
249  return MoveAI;
250  case(AI_ACTION_WALK):
251  return WalkAI;
252  case(AI_ACTION_JUMP):
253  return JumpAI;
254  case(AI_ACTION_ATTACK):
255  return AttackAI;
256  default:
257  return NULL;
258  }
259 }
void NothingAI(entity_t *ent)
Definition: ai_interpret.c:25
void MoveAI(entity_t *ent)
Definition: ai_interpret.c:61
void WalkAI(entity_t *ent)
Definition: ai_interpret.c:106
void AttackAI(entity_t *ent)
Definition: ai_interpret.c:200
void JumpAI(entity_t *ent)
Definition: ai_interpret.c:147
ai_function_t* gPresetAIs = NULL

The AI's of type preset

Definition at line 10 of file ai_interpret.c.

Referenced by InitAISystem(), and ShutdownAISystem().

ai_function_t* gVariableAIs = NULL

The AI's of type variable

Definition at line 9 of file ai_interpret.c.

Referenced by InitAISystem(), and ShutdownAISystem().