Compare commits
	
		
			3 Commits
		
	
	
		
			df0b65e097
			...
			9ef8e88b9a
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9ef8e88b9a | |||
| fe24e4f01b | |||
| 9da35b5854 | 
| @ -7,12 +7,16 @@ game: data/actions.c data/rooms.c data/transitions.c data/words.c data/flags.c * | ||||
| 	$(CC) *.c $(CFLAGS) -o game | ||||
| 
 | ||||
| data/%.c: data/%.txt | ||||
| 	echo -n "char *data_$*_txt = \"" > data/$*.c | ||||
| 	cat data/$*.txt | perl -pe 's/\n/\\n/g' >> data/$*.c | ||||
| 	echo "\";" >> data/$*.c | ||||
| 	echo "char *data_$*_txt = " > data/$*.c | ||||
| 	cat data/$*.txt | \
 | ||||
| 	perl -pe 's/ *\| */|/g' | \
 | ||||
| 	perl -pe 's/^/"/' | \
 | ||||
| 	perl -pe 's/$$/\\n"/' >> data/$*.c | ||||
| 	echo ";" >> data/$*.c | ||||
| 
 | ||||
| run: game | ||||
| 	./game | ||||
| 
 | ||||
| clean: | ||||
| 	rm -f ./game | ||||
| 	rm -f ./data/*.c | ||||
|  | ||||
| @ -1,11 +1,16 @@ | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "action.h" | ||||
| #include "effect.h" | ||||
| #include "predicate.h" | ||||
| #include "game.h" | ||||
| #include "util.h" | ||||
| 
 | ||||
| void load_action(Game *g, char *line) { | ||||
|   Action *action = &g->actions->actions[g->actions->count]; | ||||
|   action->words_count = 0; | ||||
|   action->predicates_count = 0; | ||||
|   action->effects_count = 0; | ||||
| 
 | ||||
|   char *line_token_guy; | ||||
|   char *line_token = strtok_r(line, "|", &line_token_guy); | ||||
| @ -22,9 +27,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); | ||||
| @ -34,8 +43,16 @@ void load_action(Game *g, char *line) { | ||||
|   strcpy(action->description, line_token); | ||||
| 
 | ||||
|   line_token = strtok_r(NULL, "|", &line_token_guy); | ||||
| 
 | ||||
|   // action bullshit
 | ||||
|   if (line_token == NULL) { | ||||
|   } else { | ||||
|     strcpy(command_buffer, line_token); | ||||
|     command_word = strtok_r(command_buffer, " &", &command_token_guy); | ||||
|     while (command_word != NULL) { | ||||
|       Effect *e = create_effect(g, command_word); | ||||
|       action->effects[action->effects_count++] = e; | ||||
|       command_word = strtok_r(NULL, " &", &command_token_guy); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   g->actions->count++; | ||||
| } | ||||
|  | ||||
| @ -7,6 +7,7 @@ typedef struct Actions Actions; | ||||
| #include "game.h" | ||||
| #include "word.h" | ||||
| #include "predicate.h" | ||||
| #include "effect.h" | ||||
| 
 | ||||
| struct Action { | ||||
|   Word *words[4]; | ||||
| @ -15,7 +16,8 @@ struct Action { | ||||
|   int predicates_count; | ||||
|   int priority; | ||||
|   char *description; | ||||
|   // Effect *effect;
 | ||||
|   Effect *effects[10]; | ||||
|   int effects_count; | ||||
| }; | ||||
| 
 | ||||
| struct Actions { | ||||
|  | ||||
| @ -1,4 +1,4 @@ | ||||
| PULL       | *                                      | 1    | You don't see anything to pull | | ||||
| PULL|IN(lever_room)|10|What do you want to pull?| | ||||
| PULL LEVER|IN(lever_room)|100|You pull the lever. Nice.|ENABLE(lever_pulled) | ||||
| PULL LEVER|IN(lever_room) & ENABLED(lever_pulled)|1000|You already pulled it.| | ||||
| PULL       | IN(LEVER_ROOM)                         | 10   | What do you want to pull? | | ||||
| PULL LEVER | IN(LEVER_ROOM)                         | 100  | You pull the lever. Nice. | ENABLE(LEVER_PULLED) | ||||
| PULL LEVER | IN(LEVER_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it.    | | ||||
|  | ||||
							
								
								
									
										84
									
								
								01_text_adventure/effect.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								01_text_adventure/effect.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,84 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "flag.h" | ||||
| #include "effect.h" | ||||
| 
 | ||||
| void cause_effect(Game *g, Effect *e) { | ||||
|   if (e == NULL) return; | ||||
| 
 | ||||
|   switch (e->type) { | ||||
|   case EFFECT_NOOP: | ||||
|     break; | ||||
|   case EFFECT_GOTO: | ||||
|     g->current_room = find_room(g->rooms, e->argument); | ||||
|     break; | ||||
|   case EFFECT_INCREMENT: | ||||
|     find_flag(g->flags, e->argument)->value++; | ||||
|     break; | ||||
|   case EFFECT_DECREMENT: | ||||
|     find_flag(g->flags, e->argument)->value--; | ||||
|     break; | ||||
|   case EFFECT_ENABLE: | ||||
|     find_flag(g->flags, e->argument)->value = 1; | ||||
|     break; | ||||
|   case EFFECT_DISABLE: | ||||
|     find_flag(g->flags, e->argument)->value = 0; | ||||
|     break; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| Effect *create_effect(Game *g, const char *string) { | ||||
|   Effect *e = malloc(sizeof(Effect)); | ||||
|   char *buffer = malloc(strlen(string) + 1); | ||||
|   strcpy(buffer, string); | ||||
| 
 | ||||
|   char *strtok_guy; | ||||
|   char *token = strtok_r(buffer, "(", &strtok_guy); | ||||
|   if (token == NULL) { | ||||
|     e->type = EFFECT_NOOP; | ||||
|   } else { | ||||
|     if (strcmp(token, "GOTO") == 0) { | ||||
|       e->type = EFFECT_GOTO; | ||||
|     } else if (strcmp(token, "INCREMENT") == 0) { | ||||
|       e->type = EFFECT_INCREMENT; | ||||
|     } else if (strcmp(token, "DECREMENT") == 0) { | ||||
|       e->type = EFFECT_DECREMENT; | ||||
|     } else if (strcmp(token, "ENABLE") == 0) { | ||||
|       e->type = EFFECT_ENABLE; | ||||
|     } else if (strcmp(token, "DISABLE") == 0) { | ||||
|       e->type = EFFECT_DISABLE; | ||||
|     } | ||||
| 
 | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
|     e->argument = malloc(strlen(token) + 1); | ||||
|     strcpy(e->argument, token); | ||||
|   } | ||||
| 
 | ||||
|   free(buffer); | ||||
|   return e; | ||||
| } | ||||
| 
 | ||||
| void print_effect(Effect *e) { | ||||
|   switch (e->type) { | ||||
|   case EFFECT_NOOP: | ||||
|     printf("*"); | ||||
|     break; | ||||
|   case EFFECT_GOTO: | ||||
|     printf("GOTO(%s)", e->argument); | ||||
|     break; | ||||
|   case EFFECT_INCREMENT: | ||||
|     printf("INCREMENT(%s)", e->argument); | ||||
|     break; | ||||
|   case EFFECT_DECREMENT: | ||||
|     printf("DECRMENT(%s)", e->argument); | ||||
|     break; | ||||
|   case EFFECT_ENABLE: | ||||
|     printf("ENABLE(%s)", e->argument); | ||||
|     break; | ||||
|   case EFFECT_DISABLE: | ||||
|     printf("DISABLE(%s)", e->argument); | ||||
|     break; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										26
									
								
								01_text_adventure/effect.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								01_text_adventure/effect.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | ||||
| #ifndef _FD_EFFECT_ | ||||
| #define _FD_EFFECT_ | ||||
| 
 | ||||
| typedef struct Effect Effect; | ||||
| 
 | ||||
| typedef enum EffectType { | ||||
|   EFFECT_NOOP, | ||||
|   EFFECT_GOTO, | ||||
|   EFFECT_INCREMENT, | ||||
|   EFFECT_DECREMENT, | ||||
|   EFFECT_ENABLE, | ||||
|   EFFECT_DISABLE, | ||||
| } EffectType; | ||||
| 
 | ||||
| #include "game.h" | ||||
| 
 | ||||
| struct Effect { | ||||
|   EffectType type; | ||||
|   char *argument; | ||||
| }; | ||||
| 
 | ||||
| void cause_effect(Game *g, Effect *e); | ||||
| Effect *create_effect(Game *g, const char *string); | ||||
| void print_effect(Effect *e); | ||||
| 
 | ||||
| #endif | ||||
| @ -13,6 +13,7 @@ void load_flag(Game *g, char *line) { | ||||
| 
 | ||||
|   token = strtok(NULL, "|"); | ||||
|   flag->value = atoi(token); | ||||
|   printf("AGH %s\n", token); | ||||
| 
 | ||||
|   g->flags->count++; | ||||
| } | ||||
| @ -23,9 +24,15 @@ void game_load_flags(Game *g) { | ||||
| } | ||||
| 
 | ||||
| int flag_value(Flags *f, char *key) { | ||||
|   for (int i = 0; i < f->count; i++) { | ||||
|     if (strcmp(f->flags[i].key, key) == 0) return f->flags[i].value; | ||||
|   Flag *flag = find_flag(f, key); | ||||
|   return flag ? flag->value : -1; | ||||
| } | ||||
| 
 | ||||
|   return -1; | ||||
| Flag *find_flag(Flags *f, char *key) { | ||||
|   for (int i = 0; i < f->count; i++) { | ||||
|     if (strcmp(f->flags[i].key, key) == 0) return &f->flags[i]; | ||||
|   } | ||||
| 
 | ||||
|   printf("Couldn't find flag %s\n", key); | ||||
|   return NULL; | ||||
| } | ||||
|  | ||||
| @ -17,6 +17,7 @@ struct Flags { | ||||
| }; | ||||
| 
 | ||||
| void game_load_flags(Game *g); | ||||
| Flag *find_flag(Flags *f, char *key); | ||||
| int flag_value(Flags *f, char *key); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -26,11 +26,29 @@ 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(" & "); | ||||
| 	print_predicate(g->actions->actions[i].predicates[j]); | ||||
|       } | ||||
| 
 | ||||
|       printf("|%d|%s|", g->actions->actions[i].priority, g->actions->actions[i].description); | ||||
| 
 | ||||
|       for (int j = 0; j < g->actions->actions[i].effects_count; j++) { | ||||
| 	if (j > 0) printf(" & "); | ||||
| 	print_effect(g->actions->actions[i].effects[j]); | ||||
|       } | ||||
| 
 | ||||
|       printf("\n"); | ||||
|     } | ||||
| 
 | ||||
|     printf("before action\n"); | ||||
|     printf("lever_pulled: %d\n", flag_value(g->flags, "LEVER_PULLED")); | ||||
|     cause_effect(g, g->actions->actions[2].effects[0]); | ||||
|     printf("after action\n"); | ||||
|     printf("lever_pulled: %d\n", flag_value(g->flags, "LEVER_PULLED")); | ||||
| 
 | ||||
|     game_run_until_close(g); | ||||
|     CloseWindow(); | ||||
| 
 | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "flag.h" | ||||
| @ -17,3 +18,42 @@ bool predicate_fulfilled(Game *g, Predicate *p) { | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| Predicate *create_predicate(Game *g, const char *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