Different descriptions for the same room
This commit is contained in:
		
							parent
							
								
									465ac02048
								
							
						
					
					
						commit
						42e90b8531
					
				@ -3,7 +3,7 @@ CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
.PHONY: clean run
 | 
					.PHONY: clean run
 | 
				
			||||||
 | 
					
 | 
				
			||||||
game: data/actions.c data/rooms.c data/words.c data/flags.c *.c
 | 
					game: data/actions.c data/rooms.c data/room_ins.c data/words.c data/flags.c *.c
 | 
				
			||||||
	$(CC) *.c $(CFLAGS) -o game
 | 
						$(CC) *.c $(CFLAGS) -o game
 | 
				
			||||||
 | 
					
 | 
				
			||||||
data/%.c: data/%.txt
 | 
					data/%.c: data/%.txt
 | 
				
			||||||
 | 
				
			|||||||
@ -6,9 +6,10 @@ EAST | * | 1 | You can't go east from here. |
 | 
				
			|||||||
WEST | * | 1 | You can't go west from here. |
 | 
					WEST | * | 1 | You can't go west from here. |
 | 
				
			||||||
NORTH | IN(FIRST_ROOM) | 10 | You head through the door. | GOTO(SECOND_ROOM)
 | 
					NORTH | IN(FIRST_ROOM) | 10 | You head through the door. | GOTO(SECOND_ROOM)
 | 
				
			||||||
SOUTH | IN(SECOND_ROOM) | 10 | You head back through the door. | GOTO(FIRST_ROOM)
 | 
					SOUTH | IN(SECOND_ROOM) | 10 | You head back through the door. | GOTO(FIRST_ROOM)
 | 
				
			||||||
EAST | IN(FIRST_ROOM) | 10 | You crouch under the beam and enter the room. | GOTO(LAST_ROOM)
 | 
					EAST | IN(FIRST_ROOM) & ENABLED(LEVER_PULLED) | 20 | You crouch under the portcullis and enter the room. | GOTO(LAST_ROOM)
 | 
				
			||||||
WEST | IN(LAST_ROOM) | 10 | You crouch under the beam and return to the room. | GOTO(FIRST_ROOM)
 | 
					EAST | IN(FIRST_ROOM) | 10 | There is a portcullis in the way. |
 | 
				
			||||||
 | 
					WEST | IN(LAST_ROOM) | 10 | You crouch under the portcullis and return to the room. | GOTO(FIRST_ROOM)
 | 
				
			||||||
PULL       | *                                      | 1    | You don't see anything to pull |
 | 
					PULL       | *                                      | 1    | You don't see anything to pull |
 | 
				
			||||||
PULL       | IN(FIRST_ROOM)                         | 10   | What do you want to pull? |
 | 
					PULL       | IN(FIRST_ROOM)                         | 10   | What do you want to pull? |
 | 
				
			||||||
PULL LEVER | IN(FIRST_ROOM)                         | 100  | You pull the lever. Nice. | ENABLE(LEVER_PULLED)
 | 
					PULL LEVER | IN(FIRST_ROOM)                         | 100  | You pull the lever and the portcullis raises up most of the way. | ENABLE(LEVER_PULLED)
 | 
				
			||||||
PULL LEVER | IN(FIRST_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it.    |
 | 
					PULL LEVER | IN(FIRST_ROOM) & ENABLED(LEVER_PULLED) | 1000 | You already pulled it.    |
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										4
									
								
								01_text_adventure/data/room_ins.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								01_text_adventure/data/room_ins.txt
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					FIRST_ROOM  | *                     | 1  | This is a plain room. A doorway is on the north wall, and a portcullis obstructing a doorway to the east.
 | 
				
			||||||
 | 
					FIRST_ROOM  | ENABLED(LEVER_PULLED) | 10 | This is a plain room. A doorway is on the north wall, and the doorway once obstructed by the portcullis is to the east.
 | 
				
			||||||
 | 
					SECOND_ROOM | *                     | 1 | This is a small room. It's painted blue. There's a doorway on the south wall.
 | 
				
			||||||
 | 
					LAST_ROOM   | *                     | 1 | This room is in disrepair. A doorway is open to the west.
 | 
				
			||||||
@ -1,3 +1,3 @@
 | 
				
			|||||||
FIRST_ROOM|This is a plain room. A wooden door is on the north wall, and a beam semi-obstructs an open doorway to the east.
 | 
					FIRST_ROOM
 | 
				
			||||||
SECOND_ROOM|This is a small room. It's painted blue. There's a door on the south wall.
 | 
					SECOND_ROOM
 | 
				
			||||||
LAST_ROOM|This room is in disrepair. A beam partially blocks the doorway to the west.
 | 
					LAST_ROOM
 | 
				
			||||||
 | 
				
			|||||||
@ -4,6 +4,7 @@
 | 
				
			|||||||
#include "game.h"
 | 
					#include "game.h"
 | 
				
			||||||
#include "flag.h"
 | 
					#include "flag.h"
 | 
				
			||||||
#include "effect.h"
 | 
					#include "effect.h"
 | 
				
			||||||
 | 
					#include "room_in.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void cause_effect(Game *g, Effect *e) {
 | 
					void cause_effect(Game *g, Effect *e) {
 | 
				
			||||||
  if (e == NULL) return;
 | 
					  if (e == NULL) return;
 | 
				
			||||||
@ -30,7 +31,8 @@ void cause_effect(Game *g, Effect *e) {
 | 
				
			|||||||
    g->should_close = true;
 | 
					    g->should_close = true;
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  case EFFECT_LOOK_ROOM:
 | 
					  case EFFECT_LOOK_ROOM:
 | 
				
			||||||
    push_line_to_log(g->input->log, g->current_room->description);
 | 
					    // TODO 
 | 
				
			||||||
 | 
					    push_line_to_log(g->input->log, find_room_in(g)->description);
 | 
				
			||||||
    break;
 | 
					    break;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ Game *game_create(void) {
 | 
				
			|||||||
  game_load_flags(g);
 | 
					  game_load_flags(g);
 | 
				
			||||||
  game_load_actions(g);
 | 
					  game_load_actions(g);
 | 
				
			||||||
  game_load_rooms(g);
 | 
					  game_load_rooms(g);
 | 
				
			||||||
 | 
					  game_load_room_ins(g);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Log *log = create_log();
 | 
					  Log *log = create_log();
 | 
				
			||||||
  g->log = log;
 | 
					  g->log = log;
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ typedef enum CommandType {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "input.h"
 | 
					#include "input.h"
 | 
				
			||||||
#include "room.h"
 | 
					#include "room.h"
 | 
				
			||||||
 | 
					#include "room_in.h"
 | 
				
			||||||
#include "log.h"
 | 
					#include "log.h"
 | 
				
			||||||
#include "word.h"
 | 
					#include "word.h"
 | 
				
			||||||
#include "flag.h"
 | 
					#include "flag.h"
 | 
				
			||||||
@ -27,6 +28,7 @@ struct Game {
 | 
				
			|||||||
  Input *input;
 | 
					  Input *input;
 | 
				
			||||||
  Log *log;
 | 
					  Log *log;
 | 
				
			||||||
  Rooms *rooms;
 | 
					  Rooms *rooms;
 | 
				
			||||||
 | 
					  RoomIns *room_ins;
 | 
				
			||||||
  Room *current_room;
 | 
					  Room *current_room;
 | 
				
			||||||
  Words *words;
 | 
					  Words *words;
 | 
				
			||||||
  Flags *flags;
 | 
					  Flags *flags;
 | 
				
			||||||
 | 
				
			|||||||
@ -11,10 +11,6 @@ void load_room(Game *g, char *line) {
 | 
				
			|||||||
  g->rooms->rooms[g->rooms->count].name = malloc(strlen(token) + 1);
 | 
					  g->rooms->rooms[g->rooms->count].name = malloc(strlen(token) + 1);
 | 
				
			||||||
  strcpy(g->rooms->rooms[g->rooms->count].name, token);
 | 
					  strcpy(g->rooms->rooms[g->rooms->count].name, token);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  token = strtok(NULL, "|");
 | 
					 | 
				
			||||||
  g->rooms->rooms[g->rooms->count].description = malloc(strlen(token) + 1);
 | 
					 | 
				
			||||||
  strcpy(g->rooms->rooms[g->rooms->count].description, token);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  g->rooms->count++;
 | 
					  g->rooms->count++;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -29,7 +25,6 @@ void game_load_rooms(Game *g) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void free_room(Room r) {
 | 
					void free_room(Room r) {
 | 
				
			||||||
  free(r.name);
 | 
					  free(r.name);
 | 
				
			||||||
  free(r.description);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void free_rooms(Rooms r) {
 | 
					void free_rooms(Rooms r) {
 | 
				
			||||||
 | 
				
			|||||||
@ -6,7 +6,6 @@ typedef struct Rooms Rooms;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
struct Room {
 | 
					struct Room {
 | 
				
			||||||
  char *name;
 | 
					  char *name;
 | 
				
			||||||
  char *description;
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Rooms {
 | 
					struct Rooms {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										71
									
								
								01_text_adventure/room_in.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								01_text_adventure/room_in.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
				
			|||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "game.h"
 | 
				
			||||||
 | 
					#include "room_in.h"
 | 
				
			||||||
 | 
					#include "util.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void load_room_in(Game *g, char *line) {
 | 
				
			||||||
 | 
					  RoomIn *ri = &g->room_ins->room_ins[g->room_ins->count];
 | 
				
			||||||
 | 
					  ri->predicates_count = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  char *line_token_guy;
 | 
				
			||||||
 | 
					  char *line_token = strtok_r(line, "|", &line_token_guy);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Room *r = find_room(g->rooms, line_token);
 | 
				
			||||||
 | 
					  g->room_ins->room_ins[g->room_ins->count].room = r;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  line_token = strtok_r(NULL, "|", &line_token_guy);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  char *room_in_token_guy;
 | 
				
			||||||
 | 
					  char room_in_buffer[200];
 | 
				
			||||||
 | 
					  strcpy(room_in_buffer, line_token);
 | 
				
			||||||
 | 
					  char *room_in_word = strtok_r(room_in_buffer, " &", &room_in_token_guy);
 | 
				
			||||||
 | 
					  while (room_in_word != NULL) {
 | 
				
			||||||
 | 
					    Predicate *p = create_predicate(g, room_in_word);
 | 
				
			||||||
 | 
					    ri->predicates[ri->predicates_count++] = p;
 | 
				
			||||||
 | 
					    room_in_word = strtok_r(NULL, " &", &room_in_token_guy);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  line_token = strtok_r(NULL, "|", &line_token_guy);
 | 
				
			||||||
 | 
					  ri->priority = atoi(line_token);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  line_token = strtok_r(NULL, "|", &line_token_guy);
 | 
				
			||||||
 | 
					  g->room_ins->room_ins[g->room_ins->count].description = malloc(strlen(line_token) + 1);
 | 
				
			||||||
 | 
					  strcpy(g->room_ins->room_ins[g->room_ins->count].description, line_token);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  g->room_ins->count++;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "data/room_ins.c"
 | 
				
			||||||
 | 
					void game_load_room_ins(Game *g) {
 | 
				
			||||||
 | 
					  g->room_ins = malloc(sizeof(RoomIns));
 | 
				
			||||||
 | 
					  g->room_ins->count = 0;
 | 
				
			||||||
 | 
					  parse_multiline_string(g, data_room_ins_txt, &load_room_in);
 | 
				
			||||||
 | 
					  printf("loaded room ins\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RoomIn *find_room_in(Game *g) {
 | 
				
			||||||
 | 
					  RoomIn *ri;
 | 
				
			||||||
 | 
					  int priority = -1;
 | 
				
			||||||
 | 
					  bool failed_predicate;
 | 
				
			||||||
 | 
					  for (int i = 0; i < g->room_ins->count; i++) {
 | 
				
			||||||
 | 
					    RoomIn *cri = &g->room_ins->room_ins[i];
 | 
				
			||||||
 | 
					    if (cri->priority < priority) continue;
 | 
				
			||||||
 | 
					    if (cri->room != g->current_room) continue;
 | 
				
			||||||
 | 
					    failed_predicate = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (int j = 0; j < cri->predicates_count; j++) {
 | 
				
			||||||
 | 
					      if (!predicate_fulfilled(g, cri->predicates[j])) failed_predicate = true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (failed_predicate) break;
 | 
				
			||||||
 | 
					    priority = cri->priority;
 | 
				
			||||||
 | 
					    ri = cri;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (ri) return ri;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  printf("Couldn't find a valid room_in\n");
 | 
				
			||||||
 | 
					  return &g->room_ins->room_ins[0];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										28
									
								
								01_text_adventure/room_in.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								01_text_adventure/room_in.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					#ifndef _FD_ROOM_IN_
 | 
				
			||||||
 | 
					#define _FD_ROOM_IN_
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct RoomIn RoomIn;
 | 
				
			||||||
 | 
					typedef struct RoomIns RoomIns;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "game.h"
 | 
				
			||||||
 | 
					#include "room.h"
 | 
				
			||||||
 | 
					#include "predicate.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct RoomIn {
 | 
				
			||||||
 | 
					  Room *room;
 | 
				
			||||||
 | 
					  Predicate *predicates[10];
 | 
				
			||||||
 | 
					  int predicates_count;
 | 
				
			||||||
 | 
					  int priority;
 | 
				
			||||||
 | 
					  char *description;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct RoomIns {
 | 
				
			||||||
 | 
					  RoomIn room_ins[200];
 | 
				
			||||||
 | 
					  int count;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void game_load_room_ins(Game *g);
 | 
				
			||||||
 | 
					RoomIns *find_room_ins(Room *r);
 | 
				
			||||||
 | 
					RoomIn *find_room_in(Game *g);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user