Haul items around
This commit is contained in:
		
							parent
							
								
									faccabcafc
								
							
						
					
					
						commit
						868c2211be
					
				| @ -15,3 +15,8 @@ PULL       | *                                      | 1    | You don't see anyth | ||||
| PULL       | IN(SECOND_ROOM)                         | 10   | What do you want to pull? | | ||||
| PULL LEVER | IN(SECOND_ROOM)                         | 100  | You pull the lever and the sound of grinding machinery comes from the first room. | ENABLE(LEVER_PULLED) | ||||
| PULL LEVER | IN(SECOND_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it.    | | ||||
| TAKE TORCH | ITEM_HERE(UNLIT_TORCH) | 1000 | You pick up the torch | TAKE(UNLIT_TORCH) | ||||
| TAKE TORCH | HAS_ITEM(UNLIT_TORCH) | 1001 | You already have a torch | | ||||
| TAKE TORCH | * | 100 | What torch? | | ||||
| DROP TORCH | * | 100 | What torch? | | ||||
| DROP TORCH | HAS_ITEM(UNLIT_TORCH) | 1000 | You lay the torch carefully on the ground. | DROP(UNLIT_TORCH) | ||||
|  | ||||
| @ -12,3 +12,5 @@ HELP|HELP,H | ||||
| ANVIL|ANVIL | ||||
| FLASK|FLASK,BOTTLE,JAR | ||||
| TORCH|TORCH | ||||
| TAKE|TAKE,PICKUP,GET | ||||
| DROP|DROP | ||||
|  | ||||
| @ -34,6 +34,12 @@ void cause_effect(Game *g, Effect *e) { | ||||
|     push_line_to_log(g->input->log, find_room_in(g)->description); | ||||
|     log_items_in_room(g, g->current_room); | ||||
|     break; | ||||
|   case EFFECT_TAKE_ITEM: | ||||
|     take_item(g, e->argument); | ||||
|     break; | ||||
|   case EFFECT_DROP_ITEM: | ||||
|     drop_item(g, e->argument); | ||||
|     break; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| @ -61,6 +67,10 @@ Effect *create_effect(Game *g, const char *string) { | ||||
|       e->type = EFFECT_QUIT_GAME; | ||||
|     } else if (strcmp(token, "LOOK_ROOM") == 0) { | ||||
|       e->type = EFFECT_LOOK_ROOM; | ||||
|     } else if (strcmp(token, "TAKE") == 0) { | ||||
|       e->type = EFFECT_TAKE_ITEM; | ||||
|     } else if (strcmp(token, "DROP") == 0) { | ||||
|       e->type = EFFECT_DROP_ITEM; | ||||
|     } | ||||
| 
 | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
| @ -100,5 +110,11 @@ void print_effect(Effect *e) { | ||||
|   case EFFECT_LOOK_ROOM: | ||||
|     printf("LOOK_ROOM()"); | ||||
|     break; | ||||
|   case EFFECT_TAKE_ITEM: | ||||
|     printf("TAKE(%s)", e->argument); | ||||
|     break; | ||||
|   case EFFECT_DROP_ITEM: | ||||
|     printf("DROP(%s)", e->argument); | ||||
|     break; | ||||
|   } | ||||
| } | ||||
|  | ||||
| @ -12,6 +12,8 @@ typedef enum EffectType { | ||||
|   EFFECT_DISABLE, | ||||
|   EFFECT_QUIT_GAME, | ||||
|   EFFECT_LOOK_ROOM, | ||||
|   EFFECT_TAKE_ITEM, | ||||
|   EFFECT_DROP_ITEM, | ||||
| } EffectType; | ||||
| 
 | ||||
| #include "game.h" | ||||
|  | ||||
| @ -1,4 +1,5 @@ | ||||
| #include <stdio.h> | ||||
| #include <stdbool.h> | ||||
| #include <stdlib.h> | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| @ -50,3 +51,23 @@ void log_items_in_room(Game *g, Room *r) { | ||||
|     if (g->items->items[i].location == r) log_item_in_room(g, &g->items->items[i]); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| Item *find_item(Items *items, char *item_name) { | ||||
|   for (int i = 0; i < items->count; i++) { | ||||
|     if (strcmp(items->items[i].name, item_name) == 0) return &items->items[i]; | ||||
|   } | ||||
| 
 | ||||
|   return NULL; | ||||
| } | ||||
| 
 | ||||
| void take_item(Game *g, char *item_name) { | ||||
|   Item *item = find_item(g->items, item_name); | ||||
|   item->location = NULL; | ||||
|   item->in_inventory = true; | ||||
| } | ||||
| 
 | ||||
| void drop_item(Game *g, char *item_name) { | ||||
|   Item *item = find_item(g->items, item_name); | ||||
|   item->location = g->current_room; | ||||
|   item->in_inventory = false; | ||||
| } | ||||
|  | ||||
| @ -23,5 +23,8 @@ struct Items { | ||||
| 
 | ||||
| void game_load_items(Game *g); | ||||
| void log_items_in_room(Game *g, Room *r); | ||||
| Item *find_item(Items *items, char *item_name); | ||||
| void take_item(Game *g, char *item_name); | ||||
| void drop_item(Game *g, char *item_name); | ||||
| 
 | ||||
| #endif | ||||
|  | ||||
| @ -3,6 +3,7 @@ | ||||
| #include <string.h> | ||||
| #include "game.h" | ||||
| #include "flag.h" | ||||
| #include "item.h" | ||||
| #include "predicate.h" | ||||
| 
 | ||||
| bool predicate_fulfilled(Game *g, Predicate *p) { | ||||
| @ -11,6 +12,10 @@ bool predicate_fulfilled(Game *g, Predicate *p) { | ||||
|     return true; | ||||
|   case PREDICATE_IN_ROOM: | ||||
|     return strcmp(g->current_room->name, p->argument) == 0; | ||||
|   case PREDICATE_ITEM_HERE: | ||||
|     return find_item(g->items, p->argument)->location == g->current_room; | ||||
|   case PREDICATE_HAS_ITEM: | ||||
|     return find_item(g->items, p->argument)->in_inventory; | ||||
|   case PREDICATE_FLAG_ENABLED: | ||||
|     return flag_value(g->flags, p->argument) > 0; | ||||
|   default: | ||||
| @ -33,6 +38,16 @@ Predicate *create_predicate(Game *g, const char *string) { | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
|     p->argument = malloc(strlen(token) + 1); | ||||
|     strcpy(p->argument, token); | ||||
|   } else if (strcmp(token, "ITEM_HERE") == 0) { | ||||
|     p->type = PREDICATE_ITEM_HERE; | ||||
|     token = strtok_r(NULL, ")", &strtok_guy); | ||||
|     p->argument = malloc(strlen(token) + 1); | ||||
|     strcpy(p->argument, token); | ||||
|   } else if (strcmp(token, "HAS_ITEM") == 0) { | ||||
|     p->type = PREDICATE_HAS_ITEM; | ||||
|     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); | ||||
| @ -52,6 +67,12 @@ void print_predicate(Predicate *p) { | ||||
|   case PREDICATE_IN_ROOM: | ||||
|     printf("IN(%s)", p->argument); | ||||
|     break; | ||||
|   case PREDICATE_ITEM_HERE: | ||||
|     printf("ITEM_HERE(%s)", p->argument); | ||||
|     break; | ||||
|   case PREDICATE_HAS_ITEM: | ||||
|     printf("HAS_ITEM(%s)", p->argument); | ||||
|     break; | ||||
|   case PREDICATE_FLAG_ENABLED: | ||||
|     printf("ENABLED(%s)", p->argument); | ||||
|     break; | ||||
|  | ||||
| @ -6,7 +6,8 @@ typedef struct Predicate Predicate; | ||||
| typedef enum PredicateType { | ||||
|   PREDICATE_TRUE, | ||||
|   PREDICATE_IN_ROOM, | ||||
|   // PREDICATE_HAS_ITEM,
 | ||||
|   PREDICATE_ITEM_HERE, | ||||
|   PREDICATE_HAS_ITEM, | ||||
|   PREDICATE_FLAG_ENABLED, | ||||
| } PredicateType; | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user