Predicates
This commit is contained in:
		
							parent
							
								
									df0b65e097
								
							
						
					
					
						commit
						9da35b5854
					
				| @ -23,8 +23,13 @@ void load_action(Game *g, char *line) { | ||||
| 
 | ||||
|   line_token = strtok_r(NULL, "|", &line_token_guy); | ||||
| 
 | ||||
|   // predicate bullshit
 | ||||
|   // char *command_predicate_strtok = strtok_r
 | ||||
|   strcpy(command_buffer, line_token); | ||||
|   command_word = strtok_r(command_buffer, " &", &command_token_guy); | ||||
|   while (command_word != NULL) { | ||||
|     Predicate *p = create_predicate(g, command_word); | ||||
|     action->predicates[action->predicates_count++] = p; | ||||
|     command_word = strtok_r(NULL, " &", &command_token_guy); | ||||
|   } | ||||
| 
 | ||||
|   line_token = strtok_r(NULL, "|", &line_token_guy); | ||||
|   action->priority = atoi(line_token); | ||||
|  | ||||
| @ -26,7 +26,15 @@ int main(void) { | ||||
|       for (int j = 0; j < g->actions->actions[i].words_count; j++) { | ||||
| 	printf("%s ", g->actions->actions[i].words[j]->word); | ||||
|       } | ||||
|       printf("|preds|%d|%s|effects", g->actions->actions[i].priority, g->actions->actions[i].description); | ||||
| 
 | ||||
|       printf("|"); | ||||
|       for (int j = 0; j < g->actions->actions[i].predicates_count; j++) { | ||||
| 	if (j > 0) printf(" & "); | ||||
| 	fflush(stdout); | ||||
| 	print_predicate(g->actions->actions[i].predicates[j]); | ||||
|       } | ||||
| 
 | ||||
|       printf("|%d|%s|effects", g->actions->actions[i].priority, g->actions->actions[i].description); | ||||
| 
 | ||||
|       printf("\n"); | ||||
|     } | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "flag.h" | ||||
| @ -17,3 +18,43 @@ bool predicate_fulfilled(Game *g, Predicate *p) { | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| Predicate *create_predicate(Game *g, const char *string) { | ||||
|   printf("Predicate from %s:\n", string); | ||||
|   Predicate *p = malloc(sizeof(Predicate)); | ||||
|   char *buffer = malloc(strlen(string) + 1); | ||||
|   strcpy(buffer, string); | ||||
|   char *strtok_guy; | ||||
| 
 | ||||
|   char *token = strtok_r(buffer, "(", &strtok_guy); | ||||
|   if (strcmp(token, "*") == 0) { | ||||
|     p->type = PREDICATE_TRUE; | ||||
|   } else if (strcmp(token, "IN") == 0) { | ||||
|     p->type = PREDICATE_IN_ROOM; | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
|     p->argument = malloc(strlen(token) + 1); | ||||
|     strcpy(p->argument, token); | ||||
|   } else if (strcmp(token, "ENABLED") == 0) { | ||||
|     p->type = PREDICATE_FLAG_ENABLED; | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
|     p->argument = malloc(strlen(token) + 1); | ||||
|     strcpy(p->argument, token); | ||||
|   } | ||||
| 
 | ||||
|   free(buffer); | ||||
|   return p; | ||||
| } | ||||
| 
 | ||||
| void print_predicate(Predicate *p) { | ||||
|   switch (p->type) { | ||||
|   case PREDICATE_TRUE: | ||||
|     printf("*"); | ||||
|     break; | ||||
|   case PREDICATE_IN_ROOM: | ||||
|     printf("IN(%s)", p->argument); | ||||
|     break; | ||||
|   case PREDICATE_FLAG_ENABLED: | ||||
|     printf("ENABLED(%s)", p->argument); | ||||
|     break; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -18,5 +18,7 @@ struct Predicate { | ||||
| }; | ||||
| 
 | ||||
| bool predicate_fulfilled(Game *g, Predicate *p); | ||||
| Predicate *create_predicate(Game *g, const char *string); | ||||
| void print_predicate(Predicate *p); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user