├── Tutorial 09 End ├── include │ └── rogue.h ├── makefile └── src │ ├── main.c │ ├── player.c │ └── room.c ├── Tutorial 10 End ├── include │ └── rogue.h ├── makefile └── src │ ├── level.c │ ├── main.c │ ├── player.c │ └── room.c ├── Tutorial 17 End ├── grid ├── include │ └── rogue.h ├── makefile ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ └── screen.c ├── Tutorial 18 End ├── grid ├── include │ └── rogue.h ├── makefile ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ └── screen.c ├── Tutorial 20 End ├── grid ├── include │ ├── rogue.h │ └── utils.h ├── makefile ├── map.txt ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ └── utils │ └── pathFinding.c ├── Tutorial 21 End ├── grid ├── include │ ├── rogue.h │ └── utils.h ├── makefile ├── map.txt ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ └── utils │ └── pathFinding.c ├── Tutorial 22 End ├── grid ├── include │ ├── rogue.h │ └── utils.h ├── makefile ├── map.txt ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ └── utils │ └── pathFinding.c ├── Tutorial 23 End ├── grid ├── include │ ├── mainMenu.h │ ├── rogue.h │ └── utils.h ├── makefile ├── map.txt ├── sources.list └── src │ ├── combat.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ ├── utils │ └── pathFinding.c │ └── windows │ └── mainMenu.c ├── Tutorial 24 End ├── grid ├── include │ ├── mainMenu.h │ ├── rogue.h │ └── utils.h ├── makefile ├── map.txt ├── rogue ├── rogue.sublime-project ├── rogue.sublime-workspace ├── sources.list └── src │ ├── combat.c │ ├── game.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ ├── utils │ └── pathFinding.c │ └── windows │ └── mainMenu.c ├── Tutorial 25 End ├── grid ├── include │ ├── door.h │ ├── game.h │ ├── level.h │ ├── mainMenu.h │ ├── monster.h │ ├── player.h │ ├── position.h │ ├── rogue.h │ ├── room.h │ └── utils.h ├── makefile ├── map.txt ├── rogue.sublime-project ├── rogue.sublime-workspace ├── sources.list └── src │ ├── combat.c │ ├── game.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ ├── utils │ └── pathFinding.c │ └── windows │ └── mainMenu.c ├── Tutorial 25 Start ├── grid ├── include │ ├── game.h │ ├── level.h │ ├── mainMenu.h │ ├── monster.h │ ├── player.h │ ├── position.h │ ├── rogue.h │ ├── room.h │ └── utils.h ├── makefile ├── map.txt ├── rogue.sublime-project ├── rogue.sublime-workspace ├── sources.list └── src │ ├── combat.c │ ├── game.c │ ├── level.c │ ├── main.c │ ├── monster.c │ ├── player.c │ ├── room.c │ ├── screen.c │ ├── utils │ └── pathFinding.c │ └── windows │ └── mainMenu.c ├── readme.md └── rogue01.jpg /Tutorial 09 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct Position { 9 | int x; 10 | int y; 11 | // TILE_TYPE tile; 12 | } Position; 13 | 14 | typedef struct Room 15 | { 16 | Position position; 17 | int height; 18 | int width; 19 | 20 | Position ** doors; 21 | // Monster ** monsters; 22 | // Item ** items; 23 | } Room; 24 | 25 | typedef struct Player 26 | { 27 | Position position; 28 | int health; 29 | // Room * room; 30 | } Player; 31 | 32 | int screenSetUp(); 33 | Room ** mapSetUp(); 34 | Player * playerSetUp(); 35 | int handleInput(int input, Player * user); 36 | int checkPostion(int newY, int newX, Player * user); 37 | int playerMove(int y, int x, Player * user); 38 | 39 | /* room functions */ 40 | Room * createRoom(int x, int y, int height, int width); 41 | int drawRoom(Room * room); 42 | int connectDoors(Position * doorOne, Position * doorTwo); 43 | 44 | #endif -------------------------------------------------------------------------------- /Tutorial 09 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c 8 | 9 | all: rogue run clean 10 | 11 | rogue: 12 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 13 | 14 | run: 15 | ./rogue 16 | 17 | clean: 18 | rm rogue -------------------------------------------------------------------------------- /Tutorial 09 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | Player * user; 6 | int ch; 7 | 8 | screenSetUp(); 9 | 10 | mapSetUp(); 11 | 12 | user = playerSetUp(); 13 | 14 | /* main game loop */ 15 | while ((ch = getch()) != 'q') 16 | { 17 | handleInput(ch, user); 18 | 19 | } 20 | endwin(); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | int screenSetUp() 27 | { 28 | initscr(); 29 | printw("Hello world!"); 30 | noecho(); 31 | refresh(); 32 | 33 | srand(time(NULL)); 34 | 35 | return 1; 36 | 37 | } 38 | 39 | Room ** mapSetUp() 40 | { 41 | Room ** rooms; 42 | rooms = malloc(sizeof(Room)*6); 43 | 44 | rooms[0] = createRoom(13, 13, 6, 8); 45 | drawRoom(rooms[0]); 46 | 47 | rooms[1] = createRoom(40, 2, 6, 8); 48 | drawRoom(rooms[1]); 49 | 50 | rooms[2] = createRoom(40, 10, 6, 12); 51 | drawRoom(rooms[2]); 52 | 53 | connectDoors(rooms[0]->doors[3], rooms[2]->doors[1]); 54 | 55 | connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 56 | 57 | return rooms; 58 | } 59 | -------------------------------------------------------------------------------- /Tutorial 09 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | 8 | newPlayer->position.x = 14; 9 | newPlayer->position.y = 14; 10 | newPlayer->health = 20; 11 | 12 | playerMove(14, 14, newPlayer); 13 | 14 | return newPlayer; 15 | } 16 | 17 | int handleInput(int input, Player * user) 18 | { 19 | int newY; 20 | int newX; 21 | switch (input) 22 | { 23 | /* move up */ 24 | case 'w': 25 | case 'W': 26 | newY = user->position.y - 1; 27 | newX = user->position.x; 28 | break; 29 | 30 | /* move down */ 31 | case 's': 32 | case 'S': 33 | newY = user->position.y + 1; 34 | newX = user->position.x; 35 | break; 36 | 37 | /* move left */ 38 | case 'a': 39 | case 'A': 40 | newY = user->position.y; 41 | newX = user->position.x - 1; 42 | break; 43 | 44 | /* move right */ 45 | case 'd': 46 | case 'D': 47 | newY = user->position.y; 48 | newX = user->position.x + 1; 49 | break; 50 | 51 | default: 52 | break; 53 | 54 | } 55 | 56 | checkPostion(newY, newX, user); 57 | 58 | } 59 | 60 | /* check what is at next position */ 61 | int checkPostion(int newY, int newX, Player * user) 62 | { 63 | int space; 64 | switch (mvinch(newY, newX)) 65 | { 66 | case '.': 67 | case '#': 68 | case '+': 69 | playerMove(newY, newX, user); 70 | break; 71 | default: 72 | move(user->position.y, user->position.x); 73 | break; 74 | } 75 | 76 | } 77 | 78 | int playerMove(int y, int x, Player * user) 79 | { 80 | mvprintw(user->position.y, user->position.x, "."); 81 | 82 | user->position.y = y; 83 | user->position.x = x; 84 | 85 | mvprintw(user->position.y, user->position.x, "@"); 86 | move(user->position.y, user->position.x); 87 | 88 | 89 | } -------------------------------------------------------------------------------- /Tutorial 09 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int x, int y, int height, int width) 4 | { 5 | Room * newRoom; 6 | newRoom = malloc(sizeof(Room)); 7 | 8 | newRoom->position.x = x; 9 | newRoom->position.y = y; 10 | newRoom->height = height; 11 | newRoom->width = width; 12 | 13 | newRoom->doors = malloc(sizeof(Position) * 4); 14 | 15 | /* top door */ 16 | newRoom->doors[0] = malloc(sizeof(Position)); 17 | newRoom->doors[0]->x = rand() % (width - 2) + newRoom->position.x + 1; 18 | newRoom->doors[0]->y = newRoom->position.y; 19 | 20 | /* left door */ 21 | newRoom->doors[1] = malloc(sizeof(Position)); 22 | newRoom->doors[1]->y = rand() % (height - 2) + newRoom->position.y + 1; 23 | newRoom->doors[1]->x = newRoom->position.x; 24 | 25 | /* bottom door */ 26 | newRoom->doors[2] = malloc(sizeof(Position)); 27 | newRoom->doors[2]->x = rand() % (width - 2) + newRoom->position.x + 1; 28 | newRoom->doors[2]->y = newRoom->position.y + newRoom->height - 1; 29 | 30 | /* right door */ 31 | newRoom->doors[3] = malloc(sizeof(Position)); 32 | newRoom->doors[3]->y = rand() % (height - 2) + newRoom->position.y + 1; 33 | newRoom->doors[3]->x = newRoom->position.x + width - 1; 34 | 35 | return newRoom; 36 | } 37 | 38 | int drawRoom(Room * room) 39 | { 40 | int x; 41 | int y; 42 | 43 | /* draw top and bottom */ 44 | for (x = room->position.x; x < room->position.x + room->width; x++) 45 | { 46 | mvprintw(room->position.y, x, "-"); /* top */ 47 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 48 | } 49 | 50 | /* draw floors and side walls */ 51 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 52 | { 53 | /* draw side walls */ 54 | mvprintw(y, room->position.x, "|"); 55 | mvprintw(y, room->position.x + room->width - 1, "|"); 56 | 57 | /* draw floors */ 58 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 59 | { 60 | mvprintw(y, x, "."); 61 | } 62 | } 63 | 64 | /* draw doors */ 65 | mvprintw(room->doors[0]->y, room->doors[0]->x, "+"); 66 | mvprintw(room->doors[1]->y, room->doors[1]->x, "+"); 67 | mvprintw(room->doors[2]->y, room->doors[2]->x, "+"); 68 | mvprintw(room->doors[3]->y, room->doors[3]->x, "+"); 69 | 70 | return 1; 71 | } 72 | 73 | int connectDoors(Position * doorOne, Position * doorTwo) 74 | { 75 | Position temp; 76 | Position previous; 77 | 78 | int count = 0; 79 | 80 | temp.x = doorOne->x; 81 | temp.y = doorOne->y; 82 | 83 | previous = temp; 84 | 85 | while (1) 86 | { 87 | /* step left */ 88 | if ((abs((temp.x - 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x - 1) == ' ')) 89 | { 90 | previous.x = temp.x; 91 | temp.x = temp.x - 1; 92 | 93 | /* step right */ 94 | } else if ((abs((temp.x + 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x + 1) == ' ')) 95 | { 96 | previous.x = temp.x; 97 | temp.x = temp.x + 1; 98 | 99 | /* step down */ 100 | } else if ((abs((temp.y + 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y + 1, temp.x) == ' ')) 101 | { 102 | previous.y = temp.y; 103 | temp.y = temp.y + 1; 104 | 105 | /* step up */ 106 | } else if ((abs((temp.y - 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y - 1, temp.x) == ' ')) 107 | { 108 | previous.y = temp.y; 109 | temp.y = temp.y - 1; 110 | } else 111 | { 112 | if (count == 0) 113 | { 114 | temp = previous; 115 | count++; 116 | continue; 117 | } 118 | else 119 | { 120 | return 0; 121 | } 122 | } 123 | 124 | mvprintw(temp.y, temp.x, "#"); 125 | 126 | getch(); 127 | 128 | } 129 | 130 | return 1; 131 | } -------------------------------------------------------------------------------- /Tutorial 10 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct Position { 9 | int x; 10 | int y; 11 | // TILE_TYPE tile; 12 | } Position; 13 | 14 | typedef struct Room 15 | { 16 | Position position; 17 | int height; 18 | int width; 19 | 20 | Position ** doors; 21 | // Monster ** monsters; 22 | // Item ** items; 23 | } Room; 24 | 25 | typedef struct Player 26 | { 27 | Position position; 28 | int health; 29 | // Room * room; 30 | } Player; 31 | 32 | int screenSetUp(); 33 | 34 | /* level/map functions */ 35 | Room ** mapSetUp(); 36 | char ** saveLevelPositions(); 37 | 38 | /* player functions */ 39 | Player * playerSetUp(); 40 | Position * handleInput(int input, Player * user); 41 | int checkPostion(Position * newPosition, Player * user, char ** level); 42 | int playerMove(Position * newPosition, Player * user, char ** level); 43 | 44 | /* room functions */ 45 | Room * createRoom(int x, int y, int height, int width); 46 | int drawRoom(Room * room); 47 | int connectDoors(Position * doorOne, Position * doorTwo); 48 | 49 | #endif -------------------------------------------------------------------------------- /Tutorial 10 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c 8 | 9 | all: rogue run clean 10 | 11 | rogue: 12 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 13 | 14 | run: 15 | ./rogue 16 | 17 | clean: 18 | rm rogue -------------------------------------------------------------------------------- /Tutorial 10 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room ** mapSetUp() 4 | { 5 | Room ** rooms; 6 | rooms = malloc(sizeof(Room)*6); 7 | 8 | rooms[0] = createRoom(13, 13, 6, 8); 9 | drawRoom(rooms[0]); 10 | 11 | rooms[1] = createRoom(40, 2, 6, 8); 12 | drawRoom(rooms[1]); 13 | 14 | rooms[2] = createRoom(40, 10, 6, 12); 15 | drawRoom(rooms[2]); 16 | 17 | connectDoors(rooms[0]->doors[3], rooms[2]->doors[1]); 18 | 19 | connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 20 | 21 | return rooms; 22 | } 23 | 24 | 25 | char ** saveLevelPositions() 26 | { 27 | int x, y; 28 | char ** positions; 29 | positions = malloc(sizeof(char *) * 25); 30 | 31 | for (y = 0; y < 25; y++) 32 | { 33 | positions[y] = malloc(sizeof(char) * 100); 34 | for (x = 0; x < 100; x++) 35 | { 36 | positions[y][x] = mvinch(y, x); 37 | } 38 | } 39 | 40 | return positions; 41 | } -------------------------------------------------------------------------------- /Tutorial 10 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | Player * user; 6 | int ch; 7 | Position * newPosition; 8 | 9 | char ** level; 10 | 11 | screenSetUp(); 12 | 13 | mapSetUp(); 14 | 15 | level = saveLevelPositions(); 16 | 17 | user = playerSetUp(); 18 | 19 | mvprintw(0, 0, "The char at 13, 13 is: %c", level[13][13]); 20 | 21 | /* main game loop */ 22 | while ((ch = getch()) != 'q') 23 | { 24 | newPosition = handleInput(ch, user); 25 | checkPostion(newPosition, user, level); 26 | } 27 | endwin(); 28 | 29 | return 0; 30 | } 31 | 32 | 33 | int screenSetUp() 34 | { 35 | initscr(); 36 | printw("Hello world!"); 37 | noecho(); 38 | refresh(); 39 | 40 | srand(time(NULL)); 41 | 42 | return 1; 43 | 44 | } -------------------------------------------------------------------------------- /Tutorial 10 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | 8 | newPlayer->position.x = 14; 9 | newPlayer->position.y = 14; 10 | newPlayer->health = 20; 11 | 12 | mvprintw(newPlayer->position.y, newPlayer->position.x, "@"); 13 | move(newPlayer->position.y, newPlayer->position.x); 14 | 15 | return newPlayer; 16 | } 17 | 18 | Position * handleInput(int input, Player * user) 19 | { 20 | Position * newPosition; 21 | newPosition = malloc(sizeof(Position)); 22 | switch (input) 23 | { 24 | /* move up */ 25 | case 'w': 26 | case 'W': 27 | newPosition->y = user->position.y - 1; 28 | newPosition->x = user->position.x; 29 | break; 30 | 31 | /* move down */ 32 | case 's': 33 | case 'S': 34 | newPosition->y = user->position.y + 1; 35 | newPosition->x = user->position.x; 36 | break; 37 | 38 | /* move left */ 39 | case 'a': 40 | case 'A': 41 | newPosition->y = user->position.y; 42 | newPosition->x = user->position.x - 1; 43 | break; 44 | 45 | /* move right */ 46 | case 'd': 47 | case 'D': 48 | newPosition->y = user->position.y; 49 | newPosition->x = user->position.x + 1; 50 | break; 51 | 52 | default: 53 | break; 54 | 55 | } 56 | 57 | return newPosition; 58 | 59 | } 60 | 61 | /* check what is at next position */ 62 | int checkPostion(Position * newPosition, Player * user, char ** level) 63 | { 64 | int space; 65 | switch (mvinch(newPosition->y, newPosition->x)) 66 | { 67 | case '.': 68 | case '#': 69 | case '+': 70 | playerMove(newPosition, user, level); 71 | break; 72 | default: 73 | move(user->position.y, user->position.x); 74 | break; 75 | } 76 | 77 | } 78 | 79 | int playerMove(Position * newPosition, Player * user, char ** level) 80 | { 81 | char buffer[8]; 82 | 83 | sprintf(buffer, "%c", level[user->position.y][user->position.x]); 84 | 85 | mvprintw(user->position.y, user->position.x, buffer); 86 | 87 | user->position.y = newPosition->y; 88 | user->position.x = newPosition->x; 89 | 90 | mvprintw(user->position.y, user->position.x, "@"); 91 | move(user->position.y, user->position.x); 92 | 93 | 94 | } -------------------------------------------------------------------------------- /Tutorial 10 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int x, int y, int height, int width) 4 | { 5 | Room * newRoom; 6 | newRoom = malloc(sizeof(Room)); 7 | 8 | newRoom->position.x = x; 9 | newRoom->position.y = y; 10 | newRoom->height = height; 11 | newRoom->width = width; 12 | 13 | newRoom->doors = malloc(sizeof(Position) * 4); 14 | 15 | /* top door */ 16 | newRoom->doors[0] = malloc(sizeof(Position)); 17 | newRoom->doors[0]->x = rand() % (width - 2) + newRoom->position.x + 1; 18 | newRoom->doors[0]->y = newRoom->position.y; 19 | 20 | /* left door */ 21 | newRoom->doors[1] = malloc(sizeof(Position)); 22 | newRoom->doors[1]->y = rand() % (height - 2) + newRoom->position.y + 1; 23 | newRoom->doors[1]->x = newRoom->position.x; 24 | 25 | /* bottom door */ 26 | newRoom->doors[2] = malloc(sizeof(Position)); 27 | newRoom->doors[2]->x = rand() % (width - 2) + newRoom->position.x + 1; 28 | newRoom->doors[2]->y = newRoom->position.y + newRoom->height - 1; 29 | 30 | /* right door */ 31 | newRoom->doors[3] = malloc(sizeof(Position)); 32 | newRoom->doors[3]->y = rand() % (height - 2) + newRoom->position.y + 1; 33 | newRoom->doors[3]->x = newRoom->position.x + width - 1; 34 | 35 | return newRoom; 36 | } 37 | 38 | int drawRoom(Room * room) 39 | { 40 | int x; 41 | int y; 42 | 43 | /* draw top and bottom */ 44 | for (x = room->position.x; x < room->position.x + room->width; x++) 45 | { 46 | mvprintw(room->position.y, x, "-"); /* top */ 47 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 48 | } 49 | 50 | /* draw floors and side walls */ 51 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 52 | { 53 | /* draw side walls */ 54 | mvprintw(y, room->position.x, "|"); 55 | mvprintw(y, room->position.x + room->width - 1, "|"); 56 | 57 | /* draw floors */ 58 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 59 | { 60 | mvprintw(y, x, "."); 61 | } 62 | } 63 | 64 | /* draw doors */ 65 | mvprintw(room->doors[0]->y, room->doors[0]->x, "+"); 66 | mvprintw(room->doors[1]->y, room->doors[1]->x, "+"); 67 | mvprintw(room->doors[2]->y, room->doors[2]->x, "+"); 68 | mvprintw(room->doors[3]->y, room->doors[3]->x, "+"); 69 | 70 | return 1; 71 | } 72 | 73 | int connectDoors(Position * doorOne, Position * doorTwo) 74 | { 75 | Position temp; 76 | Position previous; 77 | 78 | int count = 0; 79 | 80 | temp.x = doorOne->x; 81 | temp.y = doorOne->y; 82 | 83 | previous = temp; 84 | 85 | while (1) 86 | { 87 | /* step left */ 88 | if ((abs((temp.x - 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x - 1) == ' ')) 89 | { 90 | previous.x = temp.x; 91 | temp.x = temp.x - 1; 92 | 93 | /* step right */ 94 | } else if ((abs((temp.x + 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x + 1) == ' ')) 95 | { 96 | previous.x = temp.x; 97 | temp.x = temp.x + 1; 98 | 99 | /* step down */ 100 | } else if ((abs((temp.y + 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y + 1, temp.x) == ' ')) 101 | { 102 | previous.y = temp.y; 103 | temp.y = temp.y + 1; 104 | 105 | /* step up */ 106 | } else if ((abs((temp.y - 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y - 1, temp.x) == ' ')) 107 | { 108 | previous.y = temp.y; 109 | temp.y = temp.y - 1; 110 | } else 111 | { 112 | if (count == 0) 113 | { 114 | temp = previous; 115 | count++; 116 | continue; 117 | } 118 | else 119 | { 120 | return 0; 121 | } 122 | } 123 | 124 | mvprintw(temp.y, temp.x, "#"); 125 | 126 | //getch(); 127 | 128 | } 129 | 130 | return 1; 131 | } -------------------------------------------------------------------------------- /Tutorial 17 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 17 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct Level 9 | { 10 | char ** tiles; 11 | int level; 12 | int numberOfRooms; 13 | struct Room ** rooms; 14 | struct Monster ** monsters; 15 | int numberOfMonsters; 16 | struct Player * user; 17 | } Level; 18 | 19 | typedef struct Position { 20 | int x; 21 | int y; 22 | // TILE_TYPE tile; 23 | } Position; 24 | 25 | typedef struct Room 26 | { 27 | Position position; 28 | int height; 29 | int width; 30 | 31 | Position ** doors; 32 | // Monster ** monsters; 33 | // Item ** items; 34 | } Room; 35 | 36 | typedef struct Player 37 | { 38 | Position * position; 39 | int health; 40 | int attack; 41 | int gold; 42 | int maxHealth; 43 | int exp; 44 | // Room * room; 45 | } Player; 46 | 47 | typedef struct Monster 48 | { 49 | char string[2]; 50 | char symbol; 51 | int health; 52 | int attack; 53 | int speed; 54 | int defence; 55 | int pathfinding; 56 | int alive; 57 | Position * position; 58 | } Monster; 59 | 60 | /* screen functions */ 61 | int screenSetUp(); 62 | int printGameHub(Level * level); 63 | 64 | /* level/map functions */ 65 | Level * createLevel(); 66 | Room ** roomsSetUp(); 67 | char ** saveLevelPositions(); 68 | 69 | /* player functions */ 70 | Player * playerSetUp(); 71 | Position * handleInput(int input, Player * user); 72 | int checkPostion(Position * newPosition, Level * level); 73 | int playerMove(Position * newPosition, Player * user, char ** level); 74 | int placePlayer(Room ** rooms, Player * user); 75 | 76 | /* room functions */ 77 | Room * createRoom(int grid); 78 | int drawRoom(Room * room); 79 | int connectDoors(Position * doorOne, Position * doorTwo); 80 | 81 | /* monster functions */ 82 | int addMonsters(Level * level); 83 | Monster * selectMonster(int level); 84 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 85 | int setStartingPosition(Monster * monster, Room * room); 86 | int moveMonsters(Level * level); 87 | int pathfindingSeek(Position * start, Position * destination); 88 | int pathfindingRandom(Position * position); 89 | Monster * getMonsterAt(Position * position, Monster ** monsters); 90 | int killMonster(Monster * monster); 91 | 92 | 93 | int combat(Player * player, Monster * monster, int order); 94 | 95 | #endif -------------------------------------------------------------------------------- /Tutorial 17 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c 8 | 9 | all: rogue run clean 10 | 11 | rogue: 12 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 13 | 14 | run: 15 | ./rogue 16 | 17 | clean: 18 | rm rogue -------------------------------------------------------------------------------- /Tutorial 17 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 17 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 17 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | Level * createLevel(int level) 13 | { 14 | Level * newLevel; 15 | newLevel = malloc(sizeof(Level)); 16 | 17 | newLevel->level = level; 18 | newLevel->numberOfRooms = 3; 19 | newLevel->rooms = roomsSetUp(); 20 | newLevel->tiles = saveLevelPositions(); 21 | 22 | newLevel->user = playerSetUp(); 23 | placePlayer(newLevel->rooms, newLevel->user); 24 | 25 | addMonsters(newLevel); 26 | 27 | return newLevel; 28 | } 29 | 30 | Room ** roomsSetUp() 31 | { 32 | int x; 33 | Room ** rooms; 34 | rooms = malloc(sizeof(Room)*6); 35 | 36 | for (x = 0; x < 6; x++) 37 | { 38 | rooms[x] = createRoom(x); 39 | drawRoom(rooms[x]); 40 | } 41 | connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 42 | 43 | connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 44 | 45 | return rooms; 46 | } 47 | 48 | 49 | char ** saveLevelPositions() 50 | { 51 | int x, y; 52 | char ** positions; 53 | positions = malloc(sizeof(char *) * 25); 54 | 55 | for (y = 0; y < 25; y++) 56 | { 57 | positions[y] = malloc(sizeof(char) * 100); 58 | for (x = 0; x < 100; x++) 59 | { 60 | positions[y][x] = mvinch(y, x); 61 | } 62 | } 63 | 64 | return positions; 65 | } -------------------------------------------------------------------------------- /Tutorial 17 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | int ch; 6 | Position * newPosition; 7 | 8 | Level * level; 9 | 10 | screenSetUp(); 11 | 12 | level = createLevel(1); 13 | printGameHub(level); 14 | 15 | /* main game loop */ 16 | while ((ch = getch()) != 'q') 17 | { 18 | printGameHub(level); 19 | newPosition = handleInput(ch, level->user); 20 | checkPostion(newPosition, level); 21 | moveMonsters(level); 22 | move(level->user->position->y, level->user->position->x); 23 | } 24 | endwin(); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Tutorial 17 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 17 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid) 4 | { 5 | Room * newRoom; 6 | newRoom = malloc(sizeof(Room)); 7 | 8 | switch (grid) 9 | { 10 | case 0: 11 | newRoom->position.x = 0; 12 | newRoom->position.y = 0; 13 | break; 14 | case 1: 15 | newRoom->position.x = 33; 16 | newRoom->position.y = 0; 17 | break; 18 | 19 | case 2: 20 | newRoom->position.x = 66; 21 | newRoom->position.y = 0; 22 | break; 23 | case 3: 24 | newRoom->position.x = 0; 25 | newRoom->position.y = 14; 26 | break; 27 | 28 | case 4: 29 | newRoom->position.x = 33; 30 | newRoom->position.y = 14; 31 | break; 32 | case 5: 33 | newRoom->position.x = 66; 34 | newRoom->position.y = 14; 35 | break; 36 | 37 | } 38 | 39 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 40 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 41 | 42 | /* offset */ 43 | newRoom->position.x += rand() % (29 - newRoom->width + 1); 44 | newRoom->position.y += rand() % (9 - newRoom->height + 1); 45 | 46 | newRoom->doors = malloc(sizeof(Position) * 4); 47 | 48 | /* top door */ 49 | newRoom->doors[0] = malloc(sizeof(Position)); 50 | newRoom->doors[0]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 51 | newRoom->doors[0]->y = newRoom->position.y; 52 | 53 | /* left door */ 54 | newRoom->doors[1] = malloc(sizeof(Position)); 55 | newRoom->doors[1]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 56 | newRoom->doors[1]->x = newRoom->position.x; 57 | 58 | /* bottom door */ 59 | newRoom->doors[2] = malloc(sizeof(Position)); 60 | newRoom->doors[2]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 61 | newRoom->doors[2]->y = newRoom->position.y + newRoom->height - 1; 62 | 63 | /* right door */ 64 | newRoom->doors[3] = malloc(sizeof(Position)); 65 | newRoom->doors[3]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 66 | newRoom->doors[3]->x = newRoom->position.x + newRoom->width - 1; 67 | 68 | return newRoom; 69 | } 70 | 71 | int drawRoom(Room * room) 72 | { 73 | int x; 74 | int y; 75 | 76 | /* draw top and bottom */ 77 | for (x = room->position.x; x < room->position.x + room->width; x++) 78 | { 79 | mvprintw(room->position.y, x, "-"); /* top */ 80 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 81 | } 82 | 83 | /* draw floors and side walls */ 84 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 85 | { 86 | /* draw side walls */ 87 | mvprintw(y, room->position.x, "|"); 88 | mvprintw(y, room->position.x + room->width - 1, "|"); 89 | 90 | /* draw floors */ 91 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 92 | { 93 | mvprintw(y, x, "."); 94 | } 95 | } 96 | 97 | /* draw doors */ 98 | mvprintw(room->doors[0]->y, room->doors[0]->x, "+"); 99 | mvprintw(room->doors[1]->y, room->doors[1]->x, "+"); 100 | mvprintw(room->doors[2]->y, room->doors[2]->x, "+"); 101 | mvprintw(room->doors[3]->y, room->doors[3]->x, "+"); 102 | 103 | return 1; 104 | } 105 | 106 | int connectDoors(Position * doorOne, Position * doorTwo) 107 | { 108 | Position temp; 109 | Position previous; 110 | 111 | int count = 0; 112 | 113 | temp.x = doorOne->x; 114 | temp.y = doorOne->y; 115 | 116 | previous = temp; 117 | 118 | while (1) 119 | { 120 | /* step left */ 121 | if ((abs((temp.x - 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x - 1) == ' ')) 122 | { 123 | previous.x = temp.x; 124 | temp.x = temp.x - 1; 125 | 126 | /* step right */ 127 | } else if ((abs((temp.x + 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x + 1) == ' ')) 128 | { 129 | previous.x = temp.x; 130 | temp.x = temp.x + 1; 131 | 132 | /* step down */ 133 | } else if ((abs((temp.y + 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y + 1, temp.x) == ' ')) 134 | { 135 | previous.y = temp.y; 136 | temp.y = temp.y + 1; 137 | 138 | /* step up */ 139 | } else if ((abs((temp.y - 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y - 1, temp.x) == ' ')) 140 | { 141 | previous.y = temp.y; 142 | temp.y = temp.y - 1; 143 | } else 144 | { 145 | if (count == 0) 146 | { 147 | temp = previous; 148 | count++; 149 | continue; 150 | } 151 | else 152 | { 153 | return 0; 154 | } 155 | } 156 | 157 | mvprintw(temp.y, temp.x, "#"); 158 | 159 | //getch(); 160 | 161 | } 162 | 163 | return 1; 164 | } -------------------------------------------------------------------------------- /Tutorial 17 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | initscr(); 6 | printw("Hello world!"); 7 | noecho(); 8 | refresh(); 9 | 10 | srand(time(NULL)); 11 | 12 | return 1; 13 | 14 | } 15 | 16 | int printGameHub(Level * level) 17 | { 18 | mvprintw(25, 0, " Level: %d", level->level); 19 | printw(" Gold: %d", level->user->gold); 20 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 21 | printw(" Attack: %d", level->user->attack); 22 | printw(" Exp: %d", level->user->exp); 23 | printw(" "); 24 | 25 | return 1; 26 | } -------------------------------------------------------------------------------- /Tutorial 18 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 18 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct Level 9 | { 10 | char ** tiles; 11 | int level; 12 | int numberOfRooms; 13 | struct Room ** rooms; 14 | struct Monster ** monsters; 15 | int numberOfMonsters; 16 | struct Player * user; 17 | } Level; 18 | 19 | typedef struct Position { 20 | int x; 21 | int y; 22 | // TILE_TYPE tile; 23 | } Position; 24 | 25 | typedef struct Room 26 | { 27 | Position position; 28 | int height; 29 | int width; 30 | 31 | Position ** doors; 32 | // Monster ** monsters; 33 | // Item ** items; 34 | } Room; 35 | 36 | typedef struct Player 37 | { 38 | Position * position; 39 | int health; 40 | int attack; 41 | int gold; 42 | int maxHealth; 43 | int exp; 44 | // Room * room; 45 | } Player; 46 | 47 | typedef struct Monster 48 | { 49 | char string[2]; 50 | char symbol; 51 | int health; 52 | int attack; 53 | int speed; 54 | int defence; 55 | int pathfinding; 56 | int alive; 57 | Position * position; 58 | } Monster; 59 | 60 | /* screen functions */ 61 | int screenSetUp(); 62 | int printGameHub(Level * level); 63 | 64 | /* level/map functions */ 65 | Level * createLevel(); 66 | Room ** roomsSetUp(); 67 | char ** saveLevelPositions(); 68 | 69 | /* player functions */ 70 | Player * playerSetUp(); 71 | Position * handleInput(int input, Player * user); 72 | int checkPostion(Position * newPosition, Level * level); 73 | int playerMove(Position * newPosition, Player * user, char ** level); 74 | int placePlayer(Room ** rooms, Player * user); 75 | 76 | /* room functions */ 77 | Room * createRoom(int grid); 78 | int drawRoom(Room * room); 79 | int connectDoors(Position * doorOne, Position * doorTwo); 80 | 81 | /* monster functions */ 82 | int addMonsters(Level * level); 83 | Monster * selectMonster(int level); 84 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 85 | int setStartingPosition(Monster * monster, Room * room); 86 | int moveMonsters(Level * level); 87 | int pathfindingSeek(Position * start, Position * destination); 88 | int pathfindingRandom(Position * position); 89 | Monster * getMonsterAt(Position * position, Monster ** monsters); 90 | int killMonster(Monster * monster); 91 | 92 | 93 | int combat(Player * player, Monster * monster, int order); 94 | 95 | #endif -------------------------------------------------------------------------------- /Tutorial 18 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c 8 | 9 | all: rogue 10 | 11 | rogue: $(SOURCES) 12 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 13 | 14 | run: 15 | ./rogue 16 | 17 | clean: 18 | rm rogue -------------------------------------------------------------------------------- /Tutorial 18 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 18 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 18 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 3; 20 | newLevel->rooms = roomsSetUp(); 21 | newLevel->tiles = saveLevelPositions(); 22 | 23 | newLevel->user = playerSetUp(); 24 | placePlayer(newLevel->rooms, newLevel->user); 25 | 26 | addMonsters(newLevel); 27 | 28 | return newLevel; 29 | } 30 | 31 | Room ** roomsSetUp() 32 | { 33 | int x; 34 | Room ** rooms; 35 | rooms = malloc(sizeof(Room)*6); 36 | 37 | for (x = 0; x < 6; x++) 38 | { 39 | rooms[x] = createRoom(x); 40 | drawRoom(rooms[x]); 41 | } 42 | connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 43 | 44 | connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 45 | 46 | return rooms; 47 | } 48 | 49 | 50 | char ** saveLevelPositions() 51 | { 52 | int x, y; 53 | char ** positions; 54 | positions = malloc(sizeof(char *) * 25); 55 | 56 | for (y = 0; y < 25; y++) 57 | { 58 | positions[y] = malloc(sizeof(char) * 100); 59 | for (x = 0; x < 100; x++) 60 | { 61 | positions[y][x] = mvinch(y, x); 62 | } 63 | } 64 | 65 | return positions; 66 | } -------------------------------------------------------------------------------- /Tutorial 18 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | int ch; 6 | Position * newPosition; 7 | 8 | Level * level; 9 | 10 | screenSetUp(); 11 | 12 | level = createLevel(1); 13 | printGameHub(level); 14 | 15 | /* main game loop */ 16 | while ((ch = getch()) != 'q') 17 | { 18 | printGameHub(level); 19 | newPosition = handleInput(ch, level->user); 20 | checkPostion(newPosition, level); 21 | moveMonsters(level); 22 | move(level->user->position->y, level->user->position->x); 23 | } 24 | endwin(); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Tutorial 18 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 18 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid) 4 | { 5 | Room * newRoom; 6 | newRoom = malloc(sizeof(Room)); 7 | 8 | switch (grid) 9 | { 10 | case 0: 11 | newRoom->position.x = 0; 12 | newRoom->position.y = 0; 13 | break; 14 | case 1: 15 | newRoom->position.x = 33; 16 | newRoom->position.y = 0; 17 | break; 18 | 19 | case 2: 20 | newRoom->position.x = 66; 21 | newRoom->position.y = 0; 22 | break; 23 | case 3: 24 | newRoom->position.x = 0; 25 | newRoom->position.y = 14; 26 | break; 27 | 28 | case 4: 29 | newRoom->position.x = 33; 30 | newRoom->position.y = 14; 31 | break; 32 | case 5: 33 | newRoom->position.x = 66; 34 | newRoom->position.y = 14; 35 | break; 36 | 37 | } 38 | 39 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 40 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 41 | 42 | /* offset */ 43 | newRoom->position.x += rand() % (29 - newRoom->width + 1); 44 | newRoom->position.y += rand() % (9 - newRoom->height + 1); 45 | 46 | newRoom->doors = malloc(sizeof(Position) * 4); 47 | 48 | /* top door */ 49 | newRoom->doors[0] = malloc(sizeof(Position)); 50 | newRoom->doors[0]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 51 | newRoom->doors[0]->y = newRoom->position.y; 52 | 53 | /* left door */ 54 | newRoom->doors[1] = malloc(sizeof(Position)); 55 | newRoom->doors[1]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 56 | newRoom->doors[1]->x = newRoom->position.x; 57 | 58 | /* bottom door */ 59 | newRoom->doors[2] = malloc(sizeof(Position)); 60 | newRoom->doors[2]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 61 | newRoom->doors[2]->y = newRoom->position.y + newRoom->height - 1; 62 | 63 | /* right door */ 64 | newRoom->doors[3] = malloc(sizeof(Position)); 65 | newRoom->doors[3]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 66 | newRoom->doors[3]->x = newRoom->position.x + newRoom->width - 1; 67 | 68 | return newRoom; 69 | } 70 | 71 | int drawRoom(Room * room) 72 | { 73 | int x; 74 | int y; 75 | 76 | /* draw top and bottom */ 77 | for (x = room->position.x; x < room->position.x + room->width; x++) 78 | { 79 | mvprintw(room->position.y, x, "-"); /* top */ 80 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 81 | } 82 | 83 | /* draw floors and side walls */ 84 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 85 | { 86 | /* draw side walls */ 87 | mvprintw(y, room->position.x, "|"); 88 | mvprintw(y, room->position.x + room->width - 1, "|"); 89 | 90 | /* draw floors */ 91 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 92 | { 93 | mvprintw(y, x, "."); 94 | } 95 | } 96 | 97 | /* draw doors */ 98 | mvprintw(room->doors[0]->y, room->doors[0]->x, "+"); 99 | mvprintw(room->doors[1]->y, room->doors[1]->x, "+"); 100 | mvprintw(room->doors[2]->y, room->doors[2]->x, "+"); 101 | mvprintw(room->doors[3]->y, room->doors[3]->x, "+"); 102 | 103 | return 1; 104 | } 105 | 106 | int connectDoors(Position * doorOne, Position * doorTwo) 107 | { 108 | Position temp; 109 | Position previous; 110 | 111 | int count = 0; 112 | 113 | temp.x = doorOne->x; 114 | temp.y = doorOne->y; 115 | 116 | previous = temp; 117 | 118 | while (1) 119 | { 120 | /* step left */ 121 | if ((abs((temp.x - 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x - 1) == ' ')) 122 | { 123 | previous.x = temp.x; 124 | temp.x = temp.x - 1; 125 | 126 | /* step right */ 127 | } else if ((abs((temp.x + 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x + 1) == ' ')) 128 | { 129 | previous.x = temp.x; 130 | temp.x = temp.x + 1; 131 | 132 | /* step down */ 133 | } else if ((abs((temp.y + 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y + 1, temp.x) == ' ')) 134 | { 135 | previous.y = temp.y; 136 | temp.y = temp.y + 1; 137 | 138 | /* step up */ 139 | } else if ((abs((temp.y - 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y - 1, temp.x) == ' ')) 140 | { 141 | previous.y = temp.y; 142 | temp.y = temp.y - 1; 143 | } else 144 | { 145 | if (count == 0) 146 | { 147 | temp = previous; 148 | count++; 149 | continue; 150 | } 151 | else 152 | { 153 | return 0; 154 | } 155 | } 156 | 157 | mvprintw(temp.y, temp.x, "#"); 158 | 159 | //getch(); 160 | 161 | } 162 | 163 | return 1; 164 | } -------------------------------------------------------------------------------- /Tutorial 18 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | initscr(); 6 | printw("Hello world!"); 7 | noecho(); 8 | refresh(); 9 | 10 | srand(time(NULL)); 11 | 12 | return 1; 13 | 14 | } 15 | 16 | int printGameHub(Level * level) 17 | { 18 | mvprintw(25, 0, " Level: %d", level->level); 19 | printw(" Gold: %d", level->user->gold); 20 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 21 | printw(" Attack: %d", level->user->attack); 22 | printw(" Exp: %d", level->user->exp); 23 | printw(" "); 24 | 25 | return 1; 26 | } -------------------------------------------------------------------------------- /Tutorial 20 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 20 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Level 11 | { 12 | char ** tiles; 13 | int level; 14 | int numberOfRooms; 15 | struct Room ** rooms; 16 | struct Monster ** monsters; 17 | int numberOfMonsters; 18 | struct Player * user; 19 | } Level; 20 | 21 | typedef struct Position { 22 | int x; 23 | int y; 24 | // TILE_TYPE tile; 25 | } Position; 26 | 27 | typedef struct Room 28 | { 29 | Position position; 30 | int height; 31 | int width; 32 | 33 | Position ** doors; 34 | // Monster ** monsters; 35 | // Item ** items; 36 | } Room; 37 | 38 | typedef struct Player 39 | { 40 | Position * position; 41 | int health; 42 | int attack; 43 | int gold; 44 | int maxHealth; 45 | int exp; 46 | // Room * room; 47 | } Player; 48 | 49 | typedef struct Monster 50 | { 51 | char string[2]; 52 | char symbol; 53 | int health; 54 | int attack; 55 | int speed; 56 | int defence; 57 | int pathfinding; 58 | int alive; 59 | Position * position; 60 | } Monster; 61 | 62 | /************* Global Variables *************/ 63 | int MAX_HEIGHT; 64 | int MAX_WIDTH; 65 | 66 | /* screen functions */ 67 | int screenSetUp(); 68 | int printGameHub(Level * level); 69 | 70 | /* level/map functions */ 71 | Level * createLevel(); 72 | Room ** roomsSetUp(); 73 | char ** saveLevelPositions(); 74 | 75 | /* player functions */ 76 | Player * playerSetUp(); 77 | Position * handleInput(int input, Player * user); 78 | int checkPostion(Position * newPosition, Level * level); 79 | int playerMove(Position * newPosition, Player * user, char ** level); 80 | int placePlayer(Room ** rooms, Player * user); 81 | 82 | /* room functions */ 83 | Room * createRoom(int grid); 84 | int drawRoom(Room * room); 85 | int connectDoors(Position * doorOne, Position * doorTwo); 86 | 87 | /* monster functions */ 88 | int addMonsters(Level * level); 89 | Monster * selectMonster(int level); 90 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 91 | int setStartingPosition(Monster * monster, Room * room); 92 | int moveMonsters(Level * level); 93 | int pathfindingSeek(Position * start, Position * destination); 94 | int pathfindingRandom(Position * position); 95 | Monster * getMonsterAt(Position * position, Monster ** monsters); 96 | int killMonster(Monster * monster); 97 | 98 | 99 | int combat(Player * player, Monster * monster, int order); 100 | 101 | #endif -------------------------------------------------------------------------------- /Tutorial 20 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 20 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)utils/*.c 9 | 10 | all: rogue 11 | 12 | rogue: $(SOURCES) 13 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 14 | 15 | run: 16 | ./rogue 17 | 18 | clean: 19 | rm rogue -------------------------------------------------------------------------------- /Tutorial 20 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 20 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 20 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 3; 20 | newLevel->rooms = roomsSetUp(); 21 | newLevel->tiles = saveLevelPositions(); 22 | 23 | newLevel->user = playerSetUp(); 24 | placePlayer(newLevel->rooms, newLevel->user); 25 | 26 | addMonsters(newLevel); 27 | 28 | return newLevel; 29 | } 30 | 31 | Room ** roomsSetUp() 32 | { 33 | int x; 34 | Room ** rooms; 35 | rooms = malloc(sizeof(Room)*6); 36 | 37 | for (x = 0; x < 6; x++) 38 | { 39 | rooms[x] = createRoom(x); 40 | drawRoom(rooms[x]); 41 | } 42 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 43 | pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 44 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 45 | 46 | return rooms; 47 | } 48 | 49 | 50 | char ** saveLevelPositions() 51 | { 52 | int x, y; 53 | char ** positions; 54 | positions = malloc(sizeof(char *) * 25); 55 | 56 | for (y = 0; y < MAX_HEIGHT; y++) 57 | { 58 | positions[y] = malloc(sizeof(char) * 100); 59 | for (x = 0; x < MAX_WIDTH; x++) 60 | { 61 | positions[y][x] = mvinch(y, x); 62 | } 63 | } 64 | 65 | return positions; 66 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | int ch; 6 | 7 | MAX_HEIGHT = 25; 8 | MAX_WIDTH = 100; 9 | Position * newPosition; 10 | 11 | Level * level; 12 | 13 | screenSetUp(); 14 | 15 | level = createLevel(1); 16 | printGameHub(level); 17 | 18 | /* main game loop */ 19 | while ((ch = getch()) != 'q') 20 | { 21 | printGameHub(level); 22 | newPosition = handleInput(ch, level->user); 23 | checkPostion(newPosition, level); 24 | moveMonsters(level); 25 | move(level->user->position->y, level->user->position->x); 26 | } 27 | endwin(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid) 4 | { 5 | Room * newRoom; 6 | newRoom = malloc(sizeof(Room)); 7 | 8 | switch (grid) 9 | { 10 | case 0: 11 | newRoom->position.x = 0; 12 | newRoom->position.y = 0; 13 | break; 14 | case 1: 15 | newRoom->position.x = 33; 16 | newRoom->position.y = 0; 17 | break; 18 | 19 | case 2: 20 | newRoom->position.x = 66; 21 | newRoom->position.y = 0; 22 | break; 23 | case 3: 24 | newRoom->position.x = 0; 25 | newRoom->position.y = 14; 26 | break; 27 | 28 | case 4: 29 | newRoom->position.x = 33; 30 | newRoom->position.y = 14; 31 | break; 32 | case 5: 33 | newRoom->position.x = 66; 34 | newRoom->position.y = 14; 35 | break; 36 | 37 | } 38 | 39 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 40 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 41 | 42 | /* offset */ 43 | newRoom->position.x += rand() % (29 - newRoom->width + 1); 44 | newRoom->position.y += rand() % (9 - newRoom->height + 1); 45 | 46 | newRoom->doors = malloc(sizeof(Position) * 4); 47 | 48 | /* top door */ 49 | newRoom->doors[0] = malloc(sizeof(Position)); 50 | newRoom->doors[0]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 51 | newRoom->doors[0]->y = newRoom->position.y; 52 | 53 | /* left door */ 54 | newRoom->doors[1] = malloc(sizeof(Position)); 55 | newRoom->doors[1]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 56 | newRoom->doors[1]->x = newRoom->position.x; 57 | 58 | /* bottom door */ 59 | newRoom->doors[2] = malloc(sizeof(Position)); 60 | newRoom->doors[2]->x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 61 | newRoom->doors[2]->y = newRoom->position.y + newRoom->height - 1; 62 | 63 | /* right door */ 64 | newRoom->doors[3] = malloc(sizeof(Position)); 65 | newRoom->doors[3]->y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 66 | newRoom->doors[3]->x = newRoom->position.x + newRoom->width - 1; 67 | 68 | return newRoom; 69 | } 70 | 71 | int drawRoom(Room * room) 72 | { 73 | int x; 74 | int y; 75 | 76 | /* draw top and bottom */ 77 | for (x = room->position.x; x < room->position.x + room->width; x++) 78 | { 79 | mvprintw(room->position.y, x, "-"); /* top */ 80 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 81 | } 82 | 83 | /* draw floors and side walls */ 84 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 85 | { 86 | /* draw side walls */ 87 | mvprintw(y, room->position.x, "|"); 88 | mvprintw(y, room->position.x + room->width - 1, "|"); 89 | 90 | /* draw floors */ 91 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 92 | { 93 | mvprintw(y, x, "."); 94 | } 95 | } 96 | 97 | /* draw doors */ 98 | mvprintw(room->doors[0]->y, room->doors[0]->x, "+"); 99 | mvprintw(room->doors[1]->y, room->doors[1]->x, "+"); 100 | mvprintw(room->doors[2]->y, room->doors[2]->x, "+"); 101 | mvprintw(room->doors[3]->y, room->doors[3]->x, "+"); 102 | 103 | return 1; 104 | } 105 | 106 | int connectDoors(Position * doorOne, Position * doorTwo) 107 | { 108 | Position temp; 109 | Position previous; 110 | 111 | int count = 0; 112 | 113 | temp.x = doorOne->x; 114 | temp.y = doorOne->y; 115 | 116 | previous = temp; 117 | 118 | while (1) 119 | { 120 | /* step left */ 121 | if ((abs((temp.x - 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x - 1) == ' ')) 122 | { 123 | previous.x = temp.x; 124 | temp.x = temp.x - 1; 125 | 126 | /* step right */ 127 | } else if ((abs((temp.x + 1) - doorTwo->x) < abs(temp.x - doorTwo->x)) && (mvinch(temp.y, temp.x + 1) == ' ')) 128 | { 129 | previous.x = temp.x; 130 | temp.x = temp.x + 1; 131 | 132 | /* step down */ 133 | } else if ((abs((temp.y + 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y + 1, temp.x) == ' ')) 134 | { 135 | previous.y = temp.y; 136 | temp.y = temp.y + 1; 137 | 138 | /* step up */ 139 | } else if ((abs((temp.y - 1) - doorTwo->y) < abs(temp.y - doorTwo->y)) && (mvinch(temp.y - 1, temp.x) == ' ')) 140 | { 141 | previous.y = temp.y; 142 | temp.y = temp.y - 1; 143 | } else 144 | { 145 | if (count == 0) 146 | { 147 | temp = previous; 148 | count++; 149 | continue; 150 | } 151 | else 152 | { 153 | return 0; 154 | } 155 | } 156 | 157 | mvprintw(temp.y, temp.x, "#"); 158 | 159 | //getch(); 160 | 161 | } 162 | 163 | return 1; 164 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | initscr(); 6 | printw("Hello world!"); 7 | noecho(); 8 | refresh(); 9 | 10 | srand(time(NULL)); 11 | 12 | return 1; 13 | 14 | } 15 | 16 | int printGameHub(Level * level) 17 | { 18 | mvprintw(25, 0, " Level: %d", level->level); 19 | printw(" Gold: %d", level->user->gold); 20 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 21 | printw(" Attack: %d", level->user->attack); 22 | printw(" Exp: %d", level->user->exp); 23 | printw(" "); 24 | 25 | return 1; 26 | } -------------------------------------------------------------------------------- /Tutorial 20 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 12 | { 13 | // north 14 | if (y > 0 && cameFrom[y - 1][x][0] < 0 ) 15 | { 16 | addPositionYX(frontier, frontierCount, y - 1, x); 17 | frontierCount++; 18 | cameFrom[y - 1][x][0] = y; 19 | cameFrom[y - 1][x][1] = x; 20 | } 21 | 22 | // south 23 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 ) 24 | { 25 | addPositionYX(frontier, frontierCount, y + 1, x); 26 | frontierCount++; 27 | cameFrom[y + 1][x][0] = y; 28 | cameFrom[y + 1][x][1] = x; 29 | } 30 | 31 | // east 32 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 ) 33 | { 34 | addPositionYX(frontier, frontierCount, y, x + 1); 35 | frontierCount++; 36 | cameFrom[y][x + 1][0] = y; 37 | cameFrom[y][x + 1][1] = x; 38 | } 39 | 40 | // west 41 | if (x > 0 && cameFrom[y][x - 1][0] < 0 ) 42 | { 43 | addPositionYX(frontier, frontierCount, y, x - 1); 44 | frontierCount++; 45 | cameFrom[y][x - 1][0] = y; 46 | cameFrom[y][x - 1][1] = x; 47 | } 48 | 49 | return frontierCount; 50 | 51 | } 52 | 53 | void pathFind(Position * start, Position * end) 54 | { 55 | int i, j; 56 | int x, y; 57 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 58 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 59 | 60 | int frontierIndex = 0; 61 | int frontierCount = 0; 62 | 63 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 64 | { 65 | frontier[i] = malloc(sizeof(int)*2); 66 | } 67 | 68 | for (i = 0; i < MAX_HEIGHT; i++) 69 | { 70 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 71 | for (j = 0; j < MAX_WIDTH; j++) 72 | { 73 | cameFrom[i][j] = malloc(sizeof(int)*2); 74 | cameFrom[i][j][0] = -1; 75 | cameFrom[i][j][1] = -1; 76 | } 77 | } 78 | 79 | // add start to cameFrom 80 | cameFrom[start->y][start->x][0] = -9; 81 | cameFrom[start->y][start->x][1] = -9; 82 | 83 | // add start position to frontier 84 | addPositionYX(frontier, frontierCount, start->y, start->x); 85 | frontierCount++; 86 | 87 | 88 | while (frontierIndex < frontierCount) 89 | { 90 | y = frontier[frontierIndex][0]; 91 | x = frontier[frontierIndex][1]; 92 | 93 | frontierIndex++; 94 | 95 | if (y == end->y && x == end->x) 96 | { 97 | break; 98 | } 99 | 100 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 101 | } 102 | 103 | y = end->y; 104 | x = end->x; 105 | 106 | while (y != start->y || x != start->x) 107 | { 108 | y = cameFrom[y][x][0]; 109 | x = cameFrom[y][x][1]; 110 | mvprintw(y, x, "+"); 111 | getch(); 112 | } 113 | } -------------------------------------------------------------------------------- /Tutorial 21 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 21 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Level 11 | { 12 | char ** tiles; 13 | int level; 14 | int numberOfRooms; 15 | struct Room ** rooms; 16 | struct Monster ** monsters; 17 | int numberOfMonsters; 18 | struct Player * user; 19 | } Level; 20 | 21 | typedef struct Position { 22 | int x; 23 | int y; 24 | // TILE_TYPE tile; 25 | } Position; 26 | 27 | typedef struct Room 28 | { 29 | Position position; 30 | int height; 31 | int width; 32 | 33 | Position ** doors; 34 | // Monster ** monsters; 35 | // Item ** items; 36 | } Room; 37 | 38 | typedef struct Player 39 | { 40 | Position * position; 41 | int health; 42 | int attack; 43 | int gold; 44 | int maxHealth; 45 | int exp; 46 | // Room * room; 47 | } Player; 48 | 49 | typedef struct Monster 50 | { 51 | char string[2]; 52 | char symbol; 53 | int health; 54 | int attack; 55 | int speed; 56 | int defence; 57 | int pathfinding; 58 | int alive; 59 | Position * position; 60 | } Monster; 61 | 62 | /************* Global Variables *************/ 63 | int MAX_HEIGHT; 64 | int MAX_WIDTH; 65 | 66 | /* screen functions */ 67 | int screenSetUp(); 68 | int printGameHub(Level * level); 69 | 70 | /* level/map functions */ 71 | Level * createLevel(); 72 | Room ** roomsSetUp(); 73 | char ** saveLevelPositions(); 74 | 75 | /* player functions */ 76 | Player * playerSetUp(); 77 | Position * handleInput(int input, Player * user); 78 | int checkPostion(Position * newPosition, Level * level); 79 | int playerMove(Position * newPosition, Player * user, char ** level); 80 | int placePlayer(Room ** rooms, Player * user); 81 | 82 | /* room functions */ 83 | Room * createRoom(int grid); 84 | int drawRoom(Room * room); 85 | int connectDoors(Position * doorOne, Position * doorTwo); 86 | 87 | /* monster functions */ 88 | int addMonsters(Level * level); 89 | Monster * selectMonster(int level); 90 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 91 | int setStartingPosition(Monster * monster, Room * room); 92 | int moveMonsters(Level * level); 93 | int pathfindingSeek(Position * start, Position * destination); 94 | int pathfindingRandom(Position * position); 95 | Monster * getMonsterAt(Position * position, Monster ** monsters); 96 | int killMonster(Monster * monster); 97 | 98 | 99 | int combat(Player * player, Monster * monster, int order); 100 | 101 | #endif -------------------------------------------------------------------------------- /Tutorial 21 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 21 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)utils/*.c 9 | 10 | all: rogue 11 | 12 | rogue: $(SOURCES) 13 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 14 | 15 | run: 16 | ./rogue 17 | 18 | clean: 19 | rm rogue -------------------------------------------------------------------------------- /Tutorial 21 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 21 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 21 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 21 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 3; 20 | newLevel->rooms = roomsSetUp(); 21 | newLevel->tiles = saveLevelPositions(); 22 | 23 | newLevel->user = playerSetUp(); 24 | placePlayer(newLevel->rooms, newLevel->user); 25 | 26 | addMonsters(newLevel); 27 | 28 | return newLevel; 29 | } 30 | 31 | Room ** roomsSetUp() 32 | { 33 | int x; 34 | Room ** rooms; 35 | rooms = malloc(sizeof(Room)*6); 36 | 37 | for (x = 0; x < 6; x++) 38 | { 39 | rooms[x] = createRoom(x); 40 | drawRoom(rooms[x]); 41 | } 42 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 43 | pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 44 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 45 | 46 | return rooms; 47 | } 48 | 49 | 50 | char ** saveLevelPositions() 51 | { 52 | int x, y; 53 | char ** positions; 54 | positions = malloc(sizeof(char *) * 25); 55 | 56 | for (y = 0; y < MAX_HEIGHT; y++) 57 | { 58 | positions[y] = malloc(sizeof(char) * 100); 59 | for (x = 0; x < MAX_WIDTH; x++) 60 | { 61 | positions[y][x] = mvinch(y, x); 62 | } 63 | } 64 | 65 | return positions; 66 | } -------------------------------------------------------------------------------- /Tutorial 21 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | int ch; 6 | 7 | MAX_HEIGHT = 25; 8 | MAX_WIDTH = 100; 9 | Position * newPosition; 10 | 11 | Level * level; 12 | 13 | screenSetUp(); 14 | 15 | level = createLevel(1); 16 | printGameHub(level); 17 | 18 | /* main game loop */ 19 | while ((ch = getch()) != 'q') 20 | { 21 | printGameHub(level); 22 | newPosition = handleInput(ch, level->user); 23 | checkPostion(newPosition, level); 24 | moveMonsters(level); 25 | move(level->user->position->y, level->user->position->x); 26 | } 27 | endwin(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Tutorial 21 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 21 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | initscr(); 6 | printw("Hello world!"); 7 | noecho(); 8 | refresh(); 9 | 10 | srand(time(NULL)); 11 | 12 | return 1; 13 | 14 | } 15 | 16 | int printGameHub(Level * level) 17 | { 18 | mvprintw(25, 0, " Level: %d", level->level); 19 | printw(" Gold: %d", level->user->gold); 20 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 21 | printw(" Attack: %d", level->user->attack); 22 | printw(" Exp: %d", level->user->exp); 23 | printw(" "); 24 | 25 | return 1; 26 | } -------------------------------------------------------------------------------- /Tutorial 21 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 22 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 22 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Level 11 | { 12 | char ** tiles; 13 | int level; 14 | int numberOfRooms; 15 | struct Room ** rooms; 16 | struct Monster ** monsters; 17 | int numberOfMonsters; 18 | struct Player * user; 19 | } Level; 20 | 21 | typedef struct Position { 22 | int x; 23 | int y; 24 | // TILE_TYPE tile; 25 | } Position; 26 | 27 | typedef struct Room 28 | { 29 | Position position; 30 | int height; 31 | int width; 32 | 33 | struct Door ** doors; 34 | int numberOfDoors; 35 | // Monster ** monsters; 36 | // Item ** items; 37 | } Room; 38 | 39 | typedef struct Door 40 | { 41 | Position position; 42 | int connected; 43 | } Door; 44 | 45 | typedef struct Player 46 | { 47 | Position * position; 48 | int health; 49 | int attack; 50 | int gold; 51 | int maxHealth; 52 | int exp; 53 | // Room * room; 54 | } Player; 55 | 56 | typedef struct Monster 57 | { 58 | char string[2]; 59 | char symbol; 60 | int health; 61 | int attack; 62 | int speed; 63 | int defence; 64 | int pathfinding; 65 | int alive; 66 | Position * position; 67 | } Monster; 68 | 69 | /************* Global Variables *************/ 70 | int MAX_HEIGHT; 71 | int MAX_WIDTH; 72 | 73 | /* screen functions */ 74 | int screenSetUp(); 75 | int printGameHub(Level * level); 76 | 77 | /* level/map functions */ 78 | Level * createLevel(); 79 | Room ** roomsSetUp(); 80 | char ** saveLevelPositions(); 81 | void connectDoors(Level * level); 82 | 83 | /* player functions */ 84 | Player * playerSetUp(); 85 | Position * handleInput(int input, Player * user); 86 | int checkPostion(Position * newPosition, Level * level); 87 | int playerMove(Position * newPosition, Player * user, char ** level); 88 | int placePlayer(Room ** rooms, Player * user); 89 | 90 | /* room functions */ 91 | Room * createRoom(int grid, int numberOfDoors); 92 | int drawRoom(Room * room); 93 | 94 | /* monster functions */ 95 | int addMonsters(Level * level); 96 | Monster * selectMonster(int level); 97 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 98 | int setStartingPosition(Monster * monster, Room * room); 99 | int moveMonsters(Level * level); 100 | int pathfindingSeek(Position * start, Position * destination); 101 | int pathfindingRandom(Position * position); 102 | Monster * getMonsterAt(Position * position, Monster ** monsters); 103 | int killMonster(Monster * monster); 104 | 105 | 106 | int combat(Player * player, Monster * monster, int order); 107 | 108 | #endif -------------------------------------------------------------------------------- /Tutorial 22 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 22 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)utils/*.c 9 | 10 | all: rogue 11 | 12 | rogue: $(SOURCES) 13 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 14 | 15 | run: 16 | ./rogue 17 | 18 | clean: 19 | rm rogue -------------------------------------------------------------------------------- /Tutorial 22 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 22 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 22 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 22 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 6; 20 | newLevel->rooms = roomsSetUp(); 21 | connectDoors(newLevel); 22 | newLevel->tiles = saveLevelPositions(); 23 | 24 | newLevel->user = playerSetUp(); 25 | placePlayer(newLevel->rooms, newLevel->user); 26 | 27 | addMonsters(newLevel); 28 | 29 | return newLevel; 30 | } 31 | 32 | Room ** roomsSetUp() 33 | { 34 | int x; 35 | Room ** rooms; 36 | rooms = malloc(sizeof(Room)*6); 37 | 38 | for (x = 0; x < 6; x++) 39 | { 40 | rooms[x] = createRoom(x, 4); 41 | drawRoom(rooms[x]); 42 | } 43 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 44 | //pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 45 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 46 | 47 | return rooms; 48 | } 49 | 50 | void connectDoors(Level * level) 51 | { 52 | int i, j; 53 | int randomRoom, randomDoor; 54 | int count; 55 | 56 | for (i = 0; i < level->numberOfRooms; i++) 57 | { 58 | for (j = 0; j < level->rooms[i]->numberOfDoors; j++) 59 | { 60 | if (level->rooms[i]->doors[j]->connected == 1) 61 | { 62 | continue; 63 | } 64 | 65 | count = 0; 66 | 67 | while (count < 2) 68 | { 69 | randomRoom = rand() % level->numberOfRooms; 70 | randomDoor = rand() % level->rooms[randomRoom]->numberOfDoors; 71 | 72 | if (level->rooms[randomRoom]->doors[randomDoor]->connected == 1 || randomRoom == i) 73 | { 74 | count++; 75 | continue; 76 | } 77 | 78 | pathFind(&level->rooms[randomRoom]->doors[randomDoor]->position, &level->rooms[i]->doors[j]->position); 79 | 80 | level->rooms[randomRoom]->doors[randomDoor]->connected = 1; 81 | level->rooms[i]->doors[j]->connected = 1; 82 | break; 83 | } 84 | 85 | 86 | 87 | } 88 | } 89 | 90 | } 91 | 92 | 93 | char ** saveLevelPositions() 94 | { 95 | int x, y; 96 | char ** positions; 97 | positions = malloc(sizeof(char *) * 25); 98 | 99 | for (y = 0; y < MAX_HEIGHT; y++) 100 | { 101 | positions[y] = malloc(sizeof(char) * 100); 102 | for (x = 0; x < MAX_WIDTH; x++) 103 | { 104 | positions[y][x] = mvinch(y, x); 105 | } 106 | } 107 | 108 | return positions; 109 | } -------------------------------------------------------------------------------- /Tutorial 22 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int main () 4 | { 5 | int ch; 6 | 7 | MAX_HEIGHT = 25; 8 | MAX_WIDTH = 100; 9 | Position * newPosition; 10 | 11 | Level * level; 12 | 13 | screenSetUp(); 14 | 15 | level = createLevel(1); 16 | printGameHub(level); 17 | 18 | /* main game loop */ 19 | while ((ch = getch()) != 'q') 20 | { 21 | printGameHub(level); 22 | newPosition = handleInput(ch, level->user); 23 | checkPostion(newPosition, level); 24 | moveMonsters(level); 25 | move(level->user->position->y, level->user->position->x); 26 | } 27 | endwin(); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Tutorial 22 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 22 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid, int numberOfDoors) 4 | { 5 | int i; 6 | Room * newRoom; 7 | newRoom = malloc(sizeof(Room)); 8 | newRoom->numberOfDoors = numberOfDoors; 9 | 10 | switch (grid) 11 | { 12 | case 0: 13 | newRoom->position.x = 0; 14 | newRoom->position.y = 0; 15 | break; 16 | case 1: 17 | newRoom->position.x = 33; 18 | newRoom->position.y = 0; 19 | break; 20 | 21 | case 2: 22 | newRoom->position.x = 66; 23 | newRoom->position.y = 0; 24 | break; 25 | case 3: 26 | newRoom->position.x = 0; 27 | newRoom->position.y = 14; 28 | break; 29 | 30 | case 4: 31 | newRoom->position.x = 33; 32 | newRoom->position.y = 14; 33 | break; 34 | case 5: 35 | newRoom->position.x = 66; 36 | newRoom->position.y = 14; 37 | break; 38 | 39 | } 40 | 41 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 42 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 43 | 44 | /* offset */ 45 | newRoom->position.x += rand() % (30 - newRoom->width) + 1; 46 | newRoom->position.y += rand() % (10 - newRoom->height) + 1; 47 | 48 | newRoom->doors = malloc(sizeof(Door *) * numberOfDoors); 49 | 50 | for (i = 0; i < numberOfDoors; i++) 51 | { 52 | newRoom->doors[i] = malloc(sizeof(Door)); 53 | newRoom->doors[i]->connected = 0; 54 | } 55 | 56 | /* top door */ 57 | newRoom->doors[0]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 58 | newRoom->doors[0]->position.y = newRoom->position.y; 59 | 60 | /* left door */ 61 | newRoom->doors[1]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 62 | newRoom->doors[1]->position.x = newRoom->position.x; 63 | 64 | /* bottom door */ 65 | newRoom->doors[2]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 66 | newRoom->doors[2]->position.y = newRoom->position.y + newRoom->height - 1; 67 | 68 | /* right door */ 69 | newRoom->doors[3]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 70 | newRoom->doors[3]->position.x = newRoom->position.x + newRoom->width - 1; 71 | 72 | return newRoom; 73 | } 74 | 75 | int drawRoom(Room * room) 76 | { 77 | int x; 78 | int y; 79 | 80 | /* draw top and bottom */ 81 | for (x = room->position.x; x < room->position.x + room->width; x++) 82 | { 83 | mvprintw(room->position.y, x, "-"); /* top */ 84 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 85 | } 86 | 87 | /* draw floors and side walls */ 88 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 89 | { 90 | /* draw side walls */ 91 | mvprintw(y, room->position.x, "|"); 92 | mvprintw(y, room->position.x + room->width - 1, "|"); 93 | 94 | /* draw floors */ 95 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 96 | { 97 | mvprintw(y, x, "."); 98 | } 99 | } 100 | 101 | /* draw doors */ 102 | mvprintw(room->doors[0]->position.y, room->doors[0]->position.x, "+"); 103 | mvprintw(room->doors[1]->position.y, room->doors[1]->position.x, "+"); 104 | mvprintw(room->doors[2]->position.y, room->doors[2]->position.x, "+"); 105 | mvprintw(room->doors[3]->position.y, room->doors[3]->position.x, "+"); 106 | 107 | return 1; 108 | } 109 | -------------------------------------------------------------------------------- /Tutorial 22 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | initscr(); 6 | noecho(); 7 | refresh(); 8 | 9 | srand(time(NULL)); 10 | 11 | return 1; 12 | 13 | } 14 | 15 | int printGameHub(Level * level) 16 | { 17 | mvprintw(25, 0, " Level: %d", level->level); 18 | printw(" Gold: %d", level->user->gold); 19 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 20 | printw(" Attack: %d", level->user->attack); 21 | printw(" Exp: %d", level->user->exp); 22 | printw(" "); 23 | 24 | return 1; 25 | } -------------------------------------------------------------------------------- /Tutorial 22 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | //getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 23 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 23 End/include/mainMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_MENU_H 2 | #define MAIN_MENU_H 3 | 4 | enum {START_GAME, QUIT_GAME}; 5 | 6 | int mainMenu(int numberItems, char * choices[]); 7 | 8 | #endif -------------------------------------------------------------------------------- /Tutorial 23 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Level 11 | { 12 | char ** tiles; 13 | int level; 14 | int numberOfRooms; 15 | struct Room ** rooms; 16 | struct Monster ** monsters; 17 | int numberOfMonsters; 18 | struct Player * user; 19 | } Level; 20 | 21 | typedef struct Position { 22 | int x; 23 | int y; 24 | // TILE_TYPE tile; 25 | } Position; 26 | 27 | typedef struct Room 28 | { 29 | Position position; 30 | int height; 31 | int width; 32 | 33 | struct Door ** doors; 34 | int numberOfDoors; 35 | // Monster ** monsters; 36 | // Item ** items; 37 | } Room; 38 | 39 | typedef struct Door 40 | { 41 | Position position; 42 | int connected; 43 | } Door; 44 | 45 | typedef struct Player 46 | { 47 | Position * position; 48 | int health; 49 | int attack; 50 | int gold; 51 | int maxHealth; 52 | int exp; 53 | // Room * room; 54 | } Player; 55 | 56 | typedef struct Monster 57 | { 58 | char string[2]; 59 | char symbol; 60 | int health; 61 | int attack; 62 | int speed; 63 | int defence; 64 | int pathfinding; 65 | int alive; 66 | Position * position; 67 | } Monster; 68 | 69 | /************* Global Variables *************/ 70 | int MAX_HEIGHT; 71 | int MAX_WIDTH; 72 | 73 | /* screen functions */ 74 | int screenSetUp(); 75 | int printGameHub(Level * level); 76 | 77 | /* level/map functions */ 78 | Level * createLevel(); 79 | Room ** roomsSetUp(); 80 | char ** saveLevelPositions(); 81 | void connectDoors(Level * level); 82 | 83 | /* player functions */ 84 | Player * playerSetUp(); 85 | Position * handleInput(int input, Player * user); 86 | int checkPostion(Position * newPosition, Level * level); 87 | int playerMove(Position * newPosition, Player * user, char ** level); 88 | int placePlayer(Room ** rooms, Player * user); 89 | 90 | /* room functions */ 91 | Room * createRoom(int grid, int numberOfDoors); 92 | int drawRoom(Room * room); 93 | 94 | /* monster functions */ 95 | int addMonsters(Level * level); 96 | Monster * selectMonster(int level); 97 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 98 | int setStartingPosition(Monster * monster, Room * room); 99 | int moveMonsters(Level * level); 100 | int pathfindingSeek(Position * start, Position * destination); 101 | int pathfindingRandom(Position * position); 102 | Monster * getMonsterAt(Position * position, Monster ** monsters); 103 | int killMonster(Monster * monster); 104 | 105 | 106 | int combat(Player * player, Monster * monster, int order); 107 | 108 | #endif -------------------------------------------------------------------------------- /Tutorial 23 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 23 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lmenu -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)windows/*.c\ 9 | $(SRCDIR)utils/*.c 10 | 11 | all: rogue 12 | 13 | rogue: $(SOURCES) 14 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 15 | 16 | run: 17 | ./rogue 18 | 19 | clean: 20 | rm rogue -------------------------------------------------------------------------------- /Tutorial 23 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 23 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 23 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 6; 20 | newLevel->rooms = roomsSetUp(); 21 | connectDoors(newLevel); 22 | newLevel->tiles = saveLevelPositions(); 23 | 24 | newLevel->user = playerSetUp(); 25 | placePlayer(newLevel->rooms, newLevel->user); 26 | 27 | addMonsters(newLevel); 28 | 29 | return newLevel; 30 | } 31 | 32 | Room ** roomsSetUp() 33 | { 34 | int x; 35 | Room ** rooms; 36 | rooms = malloc(sizeof(Room)*6); 37 | 38 | for (x = 0; x < 6; x++) 39 | { 40 | rooms[x] = createRoom(x, 4); 41 | drawRoom(rooms[x]); 42 | } 43 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 44 | //pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 45 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 46 | 47 | return rooms; 48 | } 49 | 50 | void connectDoors(Level * level) 51 | { 52 | int i, j; 53 | int randomRoom, randomDoor; 54 | int count; 55 | 56 | for (i = 0; i < level->numberOfRooms; i++) 57 | { 58 | for (j = 0; j < level->rooms[i]->numberOfDoors; j++) 59 | { 60 | if (level->rooms[i]->doors[j]->connected == 1) 61 | { 62 | continue; 63 | } 64 | 65 | count = 0; 66 | 67 | while (count < 2) 68 | { 69 | randomRoom = rand() % level->numberOfRooms; 70 | randomDoor = rand() % level->rooms[randomRoom]->numberOfDoors; 71 | 72 | if (level->rooms[randomRoom]->doors[randomDoor]->connected == 1 || randomRoom == i) 73 | { 74 | count++; 75 | continue; 76 | } 77 | 78 | pathFind(&level->rooms[randomRoom]->doors[randomDoor]->position, &level->rooms[i]->doors[j]->position); 79 | 80 | level->rooms[randomRoom]->doors[randomDoor]->connected = 1; 81 | level->rooms[i]->doors[j]->connected = 1; 82 | break; 83 | } 84 | 85 | 86 | 87 | } 88 | } 89 | 90 | } 91 | 92 | 93 | char ** saveLevelPositions() 94 | { 95 | int x, y; 96 | char ** positions; 97 | positions = malloc(sizeof(char *) * 25); 98 | 99 | for (y = 0; y < MAX_HEIGHT; y++) 100 | { 101 | positions[y] = malloc(sizeof(char) * 100); 102 | for (x = 0; x < MAX_WIDTH; x++) 103 | { 104 | positions[y][x] = mvinch(y, x); 105 | } 106 | } 107 | 108 | return positions; 109 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "mainMenu.h" 3 | 4 | int gameLoop() 5 | { 6 | int ch; 7 | Position * newPosition; 8 | Level * level; 9 | 10 | level = createLevel(1); 11 | printGameHub(level); 12 | 13 | /* main game loop */ 14 | while ((ch = getch()) != 'q') 15 | { 16 | printGameHub(level); 17 | newPosition = handleInput(ch, level->user); 18 | checkPostion(newPosition, level); 19 | moveMonsters(level); 20 | move(level->user->position->y, level->user->position->x); 21 | 22 | if (level->user->health <= 0) 23 | { 24 | return -1; 25 | } 26 | } 27 | } 28 | 29 | void menuLoop() 30 | { 31 | int choice; 32 | char * choices[] = {"Start Game", "End Game"}; 33 | 34 | while (true) 35 | { 36 | choice = mainMenu(2, choices); 37 | 38 | switch (choice) 39 | { 40 | case START_GAME: 41 | gameLoop(); 42 | clear(); 43 | break; 44 | case QUIT_GAME: 45 | return; 46 | break; 47 | } 48 | } 49 | } 50 | 51 | int main () 52 | { 53 | screenSetUp(); 54 | menuLoop(); 55 | endwin(); 56 | 57 | return 0; 58 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | 23 | mvprintw(user->position->y, user->position->x, "@"); 24 | move(user->position->y, user->position->x); 25 | } 26 | 27 | Position * handleInput(int input, Player * user) 28 | { 29 | Position * newPosition; 30 | newPosition = malloc(sizeof(Position)); 31 | switch (input) 32 | { 33 | /* move up */ 34 | case 'w': 35 | case 'W': 36 | newPosition->y = user->position->y - 1; 37 | newPosition->x = user->position->x; 38 | break; 39 | 40 | /* move down */ 41 | case 's': 42 | case 'S': 43 | newPosition->y = user->position->y + 1; 44 | newPosition->x = user->position->x; 45 | break; 46 | 47 | /* move left */ 48 | case 'a': 49 | case 'A': 50 | newPosition->y = user->position->y; 51 | newPosition->x = user->position->x - 1; 52 | break; 53 | 54 | /* move right */ 55 | case 'd': 56 | case 'D': 57 | newPosition->y = user->position->y; 58 | newPosition->x = user->position->x + 1; 59 | break; 60 | 61 | default: 62 | break; 63 | 64 | } 65 | 66 | return newPosition; 67 | 68 | } 69 | 70 | /* check what is at next position */ 71 | int checkPostion(Position * newPosition, Level * level) 72 | { 73 | Player * user; 74 | user = level->user; 75 | int space; 76 | switch (mvinch(newPosition->y, newPosition->x)) 77 | { 78 | case '.': 79 | case '#': 80 | case '+': 81 | playerMove(newPosition, user, level->tiles); 82 | break; 83 | case 'X': 84 | case 'G': 85 | case 'T': 86 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 87 | default: 88 | move(user->position->y, user->position->x); 89 | break; 90 | } 91 | 92 | } 93 | 94 | int playerMove(Position * newPosition, Player * user, char ** level) 95 | { 96 | char buffer[8]; 97 | 98 | sprintf(buffer, "%c", level[user->position->y][user->position->x]); 99 | 100 | mvprintw(user->position->y, user->position->x, buffer); 101 | 102 | user->position->y = newPosition->y; 103 | user->position->x = newPosition->x; 104 | 105 | mvprintw(user->position->y, user->position->x, "@"); 106 | move(user->position->y, user->position->x); 107 | 108 | 109 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid, int numberOfDoors) 4 | { 5 | int i; 6 | Room * newRoom; 7 | newRoom = malloc(sizeof(Room)); 8 | newRoom->numberOfDoors = numberOfDoors; 9 | 10 | switch (grid) 11 | { 12 | case 0: 13 | newRoom->position.x = 0; 14 | newRoom->position.y = 0; 15 | break; 16 | case 1: 17 | newRoom->position.x = 33; 18 | newRoom->position.y = 0; 19 | break; 20 | 21 | case 2: 22 | newRoom->position.x = 66; 23 | newRoom->position.y = 0; 24 | break; 25 | case 3: 26 | newRoom->position.x = 0; 27 | newRoom->position.y = 14; 28 | break; 29 | 30 | case 4: 31 | newRoom->position.x = 33; 32 | newRoom->position.y = 14; 33 | break; 34 | case 5: 35 | newRoom->position.x = 66; 36 | newRoom->position.y = 14; 37 | break; 38 | 39 | } 40 | 41 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 42 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 43 | 44 | /* offset */ 45 | newRoom->position.x += rand() % (30 - newRoom->width) + 1; 46 | newRoom->position.y += rand() % (10 - newRoom->height) + 1; 47 | 48 | newRoom->doors = malloc(sizeof(Door *) * numberOfDoors); 49 | 50 | for (i = 0; i < numberOfDoors; i++) 51 | { 52 | newRoom->doors[i] = malloc(sizeof(Door)); 53 | newRoom->doors[i]->connected = 0; 54 | } 55 | 56 | /* top door */ 57 | newRoom->doors[0]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 58 | newRoom->doors[0]->position.y = newRoom->position.y; 59 | 60 | /* left door */ 61 | newRoom->doors[1]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 62 | newRoom->doors[1]->position.x = newRoom->position.x; 63 | 64 | /* bottom door */ 65 | newRoom->doors[2]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 66 | newRoom->doors[2]->position.y = newRoom->position.y + newRoom->height - 1; 67 | 68 | /* right door */ 69 | newRoom->doors[3]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 70 | newRoom->doors[3]->position.x = newRoom->position.x + newRoom->width - 1; 71 | 72 | return newRoom; 73 | } 74 | 75 | int drawRoom(Room * room) 76 | { 77 | int x; 78 | int y; 79 | 80 | /* draw top and bottom */ 81 | for (x = room->position.x; x < room->position.x + room->width; x++) 82 | { 83 | mvprintw(room->position.y, x, "-"); /* top */ 84 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 85 | } 86 | 87 | /* draw floors and side walls */ 88 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 89 | { 90 | /* draw side walls */ 91 | mvprintw(y, room->position.x, "|"); 92 | mvprintw(y, room->position.x + room->width - 1, "|"); 93 | 94 | /* draw floors */ 95 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 96 | { 97 | mvprintw(y, x, "."); 98 | } 99 | } 100 | 101 | /* draw doors */ 102 | mvprintw(room->doors[0]->position.y, room->doors[0]->position.x, "+"); 103 | mvprintw(room->doors[1]->position.y, room->doors[1]->position.x, "+"); 104 | mvprintw(room->doors[2]->position.y, room->doors[2]->position.x, "+"); 105 | mvprintw(room->doors[3]->position.y, room->doors[3]->position.x, "+"); 106 | 107 | return 1; 108 | } 109 | -------------------------------------------------------------------------------- /Tutorial 23 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | MAX_HEIGHT = 25; 6 | MAX_WIDTH = 100; 7 | 8 | initscr(); 9 | noecho(); 10 | keypad(stdscr, TRUE); 11 | refresh(); 12 | 13 | srand(time(NULL)); 14 | 15 | return 1; 16 | 17 | } 18 | 19 | int printGameHub(Level * level) 20 | { 21 | mvprintw(25, 0, " Level: %d", level->level); 22 | printw(" Gold: %d", level->user->gold); 23 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 24 | printw(" Attack: %d", level->user->attack); 25 | printw(" Exp: %d", level->user->exp); 26 | printw(" "); 27 | 28 | return 1; 29 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | //getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 23 End/src/windows/mainMenu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mainMenu.h" 5 | 6 | void closeMenu(int numberItems, MENU * menu, ITEM ** items) 7 | { 8 | int i; 9 | unpost_menu(menu); 10 | free_menu(menu); 11 | for (i = 0; i < numberItems; i++) 12 | { 13 | free_item(items[i]); 14 | } 15 | } 16 | 17 | int mainMenu(int numberItems, char * choices[]) 18 | { 19 | int i, c; 20 | int value; 21 | MENU * menu; 22 | ITEM ** items = malloc(sizeof(**items)*numberItems); 23 | ITEM * current; 24 | 25 | for (i = 0; i < numberItems; i++) 26 | { 27 | items[i] = new_item( choices[i], ""); 28 | } 29 | items[i] = (ITEM*)NULL; 30 | 31 | menu = new_menu((ITEM**)items); 32 | post_menu(menu); 33 | refresh(); 34 | 35 | // menu loop 36 | while (true) 37 | { 38 | c = getch(); 39 | switch (c) 40 | { 41 | case KEY_DOWN: 42 | menu_driver(menu, REQ_DOWN_ITEM); 43 | break; 44 | case KEY_UP: 45 | menu_driver(menu, REQ_UP_ITEM); 46 | break; 47 | case 10: // enter key 48 | current = current_item(menu); 49 | value = item_index(current); 50 | closeMenu(numberItems, menu, items); 51 | return value; 52 | } 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /Tutorial 24 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 24 End/include/mainMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_MENU_H 2 | #define MAIN_MENU_H 3 | 4 | enum {START_GAME, QUIT_GAME}; 5 | 6 | int mainMenu(int numberItems, char * choices[]); 7 | 8 | #endif -------------------------------------------------------------------------------- /Tutorial 24 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Game 11 | { 12 | struct Level * levels[10]; 13 | int currentLevel; 14 | } Game; 15 | 16 | typedef struct Level 17 | { 18 | char ** tiles; 19 | int level; 20 | int numberOfRooms; 21 | struct Room ** rooms; 22 | struct Monster ** monsters; 23 | int numberOfMonsters; 24 | struct Player * user; 25 | } Level; 26 | 27 | typedef struct Position { 28 | int x; 29 | int y; 30 | // TILE_TYPE tile; 31 | } Position; 32 | 33 | typedef struct Room 34 | { 35 | Position position; 36 | int height; 37 | int width; 38 | 39 | struct Door ** doors; 40 | int numberOfDoors; 41 | // Monster ** monsters; 42 | // Item ** items; 43 | } Room; 44 | 45 | typedef struct Door 46 | { 47 | Position position; 48 | int connected; 49 | } Door; 50 | 51 | typedef struct Player 52 | { 53 | Position * position; 54 | int health; 55 | int attack; 56 | int gold; 57 | int maxHealth; 58 | int exp; 59 | // Room * room; 60 | } Player; 61 | 62 | typedef struct Monster 63 | { 64 | char string[2]; 65 | char symbol; 66 | int health; 67 | int attack; 68 | int speed; 69 | int defence; 70 | int pathfinding; 71 | int alive; 72 | Position * position; 73 | } Monster; 74 | 75 | /************* Global Variables *************/ 76 | int MAX_HEIGHT; 77 | int MAX_WIDTH; 78 | 79 | /* screen functions */ 80 | int screenSetUp(); 81 | int printGameHub(Level * level); 82 | 83 | /* level/map functions */ 84 | Level * createLevel(); 85 | Room ** roomsSetUp(); 86 | char ** saveLevelPositions(); 87 | void connectDoors(Level * level); 88 | 89 | /* player functions */ 90 | Player * playerSetUp(); 91 | Position * handleInput(int input, Player * user); 92 | int checkPostion(Position * newPosition, Level * level); 93 | int playerMove(Position * newPosition, Player * user, char ** level); 94 | int placePlayer(Room ** rooms, Player * user); 95 | 96 | void drawPlayer(Player * player); 97 | 98 | /* room functions */ 99 | Room * createRoom(int grid, int numberOfDoors); 100 | int drawRoom(Room * room); 101 | 102 | /* monster functions */ 103 | int addMonsters(Level * level); 104 | Monster * selectMonster(int level); 105 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 106 | int setStartingPosition(Monster * monster, Room * room); 107 | int moveMonsters(Level * level); 108 | int pathfindingSeek(Position * start, Position * destination); 109 | int pathfindingRandom(Position * position); 110 | Monster * getMonsterAt(Position * position, Monster ** monsters); 111 | int killMonster(Monster * monster); 112 | 113 | void drawMonster(Monster * monster); 114 | 115 | 116 | int combat(Player * player, Monster * monster, int order); 117 | 118 | #endif -------------------------------------------------------------------------------- /Tutorial 24 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 24 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lmenu -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)windows/*.c\ 9 | $(SRCDIR)utils/*.c 10 | 11 | all: rogue 12 | 13 | rogue: $(SOURCES) 14 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 15 | 16 | run: 17 | ./rogue 18 | 19 | clean: 20 | rm rogue -------------------------------------------------------------------------------- /Tutorial 24 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 24 End/rogue: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wadsworj/rogue/be4105623afd5c9c683508c60cc935a4d98e9524/Tutorial 24 End/rogue -------------------------------------------------------------------------------- /Tutorial 24 End/rogue.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "follow_symlinks": true, 6 | "path": "src" 7 | }, 8 | { 9 | "follow_symlinks": true, 10 | "path": "include" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /Tutorial 24 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 24 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/game.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | void render(Game * game) 4 | { 5 | clear(); 6 | printGameHub(game->levels[game->currentLevel - 1]); 7 | drawLevel(game->levels[game->currentLevel - 1]); 8 | } 9 | 10 | int gameLoop(Game * game) 11 | { 12 | int ch = '\0'; 13 | Position * newPosition; 14 | Level * level; 15 | 16 | if (game->currentLevel == 0) 17 | { 18 | game->levels[game->currentLevel] = createLevel(1); 19 | game->currentLevel++; 20 | } 21 | level = game->levels[game->currentLevel - 1]; 22 | 23 | 24 | /* main game loop */ 25 | while (ch != 'q') 26 | { 27 | newPosition = handleInput(ch, level->user); 28 | checkPostion(newPosition, level); 29 | moveMonsters(level); 30 | 31 | render(game); 32 | 33 | if (level->user->health <= 0) 34 | { 35 | game->currentLevel = 0; 36 | return -1; 37 | } 38 | ch = getch(); 39 | } 40 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 6; 20 | newLevel->rooms = roomsSetUp(); 21 | connectDoors(newLevel); 22 | newLevel->tiles = saveLevelPositions(); 23 | 24 | newLevel->user = playerSetUp(); 25 | placePlayer(newLevel->rooms, newLevel->user); 26 | 27 | addMonsters(newLevel); 28 | 29 | return newLevel; 30 | } 31 | 32 | void drawLevel(Level * level) 33 | { 34 | int x, y, i; 35 | 36 | // printing tiles 37 | for (y = 0; y < MAX_HEIGHT; y++) 38 | { 39 | for (x = 0; x < MAX_WIDTH; x++) 40 | { 41 | mvaddch(y, x, level->tiles[y][x]); 42 | } 43 | } 44 | 45 | // print monsters 46 | for (i = 0; i < level->numberOfMonsters; i++) 47 | { 48 | drawMonster(level->monsters[i]); 49 | } 50 | 51 | drawPlayer(level->user); 52 | } 53 | 54 | Room ** roomsSetUp() 55 | { 56 | int x; 57 | Room ** rooms; 58 | rooms = malloc(sizeof(Room)*6); 59 | 60 | for (x = 0; x < 6; x++) 61 | { 62 | rooms[x] = createRoom(x, 4); 63 | drawRoom(rooms[x]); 64 | } 65 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 66 | //pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 67 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 68 | 69 | return rooms; 70 | } 71 | 72 | void connectDoors(Level * level) 73 | { 74 | int i, j; 75 | int randomRoom, randomDoor; 76 | int count; 77 | 78 | for (i = 0; i < level->numberOfRooms; i++) 79 | { 80 | for (j = 0; j < level->rooms[i]->numberOfDoors; j++) 81 | { 82 | if (level->rooms[i]->doors[j]->connected == 1) 83 | { 84 | continue; 85 | } 86 | 87 | count = 0; 88 | 89 | while (count < 2) 90 | { 91 | randomRoom = rand() % level->numberOfRooms; 92 | randomDoor = rand() % level->rooms[randomRoom]->numberOfDoors; 93 | 94 | if (level->rooms[randomRoom]->doors[randomDoor]->connected == 1 || randomRoom == i) 95 | { 96 | count++; 97 | continue; 98 | } 99 | 100 | pathFind(&level->rooms[randomRoom]->doors[randomDoor]->position, &level->rooms[i]->doors[j]->position); 101 | 102 | level->rooms[randomRoom]->doors[randomDoor]->connected = 1; 103 | level->rooms[i]->doors[j]->connected = 1; 104 | break; 105 | } 106 | 107 | 108 | 109 | } 110 | } 111 | 112 | } 113 | 114 | 115 | char ** saveLevelPositions() 116 | { 117 | int x, y; 118 | char ** positions; 119 | positions = malloc(sizeof(char *) * 25); 120 | 121 | for (y = 0; y < MAX_HEIGHT; y++) 122 | { 123 | positions[y] = malloc(sizeof(char) * 100); 124 | for (x = 0; x < MAX_WIDTH; x++) 125 | { 126 | positions[y][x] = mvinch(y, x); 127 | } 128 | } 129 | 130 | return positions; 131 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "mainMenu.h" 3 | 4 | 5 | void menuLoop() 6 | { 7 | int choice; 8 | char * choices[] = {"Start Game", "End Game"}; 9 | 10 | Game game; 11 | game.currentLevel = 0; 12 | 13 | while (true) 14 | { 15 | choice = mainMenu(2, choices); 16 | 17 | switch (choice) 18 | { 19 | case START_GAME: 20 | gameLoop(&game); 21 | clear(); 22 | break; 23 | case QUIT_GAME: 24 | return; 25 | break; 26 | } 27 | } 28 | } 29 | 30 | int main () 31 | { 32 | screenSetUp(); 33 | menuLoop(); 34 | endwin(); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | } 23 | 24 | Position * handleInput(int input, Player * user) 25 | { 26 | Position * newPosition; 27 | newPosition = malloc(sizeof(Position)); 28 | switch (input) 29 | { 30 | /* move up */ 31 | case 'w': 32 | case 'W': 33 | newPosition->y = user->position->y - 1; 34 | newPosition->x = user->position->x; 35 | break; 36 | 37 | /* move down */ 38 | case 's': 39 | case 'S': 40 | newPosition->y = user->position->y + 1; 41 | newPosition->x = user->position->x; 42 | break; 43 | 44 | /* move left */ 45 | case 'a': 46 | case 'A': 47 | newPosition->y = user->position->y; 48 | newPosition->x = user->position->x - 1; 49 | break; 50 | 51 | /* move right */ 52 | case 'd': 53 | case 'D': 54 | newPosition->y = user->position->y; 55 | newPosition->x = user->position->x + 1; 56 | break; 57 | 58 | default: 59 | break; 60 | 61 | } 62 | 63 | return newPosition; 64 | 65 | } 66 | 67 | /* check what is at next position */ 68 | int checkPostion(Position * newPosition, Level * level) 69 | { 70 | Player * user; 71 | user = level->user; 72 | int space; 73 | switch (mvinch(newPosition->y, newPosition->x)) 74 | { 75 | case '.': 76 | case '#': 77 | case '+': 78 | playerMove(newPosition, user, level->tiles); 79 | break; 80 | case 'X': 81 | case 'G': 82 | case 'T': 83 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 84 | default: 85 | break; 86 | } 87 | 88 | } 89 | 90 | int playerMove(Position * newPosition, Player * user, char ** level) 91 | { 92 | user->position->y = newPosition->y; 93 | user->position->x = newPosition->x; 94 | } 95 | 96 | void drawPlayer(Player * player) 97 | { 98 | mvprintw(player->position->y, player->position->x, "@"); 99 | move(player->position->y, player->position->x); 100 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid, int numberOfDoors) 4 | { 5 | int i; 6 | Room * newRoom; 7 | newRoom = malloc(sizeof(Room)); 8 | newRoom->numberOfDoors = numberOfDoors; 9 | 10 | switch (grid) 11 | { 12 | case 0: 13 | newRoom->position.x = 0; 14 | newRoom->position.y = 0; 15 | break; 16 | case 1: 17 | newRoom->position.x = 33; 18 | newRoom->position.y = 0; 19 | break; 20 | 21 | case 2: 22 | newRoom->position.x = 66; 23 | newRoom->position.y = 0; 24 | break; 25 | case 3: 26 | newRoom->position.x = 0; 27 | newRoom->position.y = 14; 28 | break; 29 | 30 | case 4: 31 | newRoom->position.x = 33; 32 | newRoom->position.y = 14; 33 | break; 34 | case 5: 35 | newRoom->position.x = 66; 36 | newRoom->position.y = 14; 37 | break; 38 | 39 | } 40 | 41 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 42 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 43 | 44 | /* offset */ 45 | newRoom->position.x += rand() % (30 - newRoom->width) + 1; 46 | newRoom->position.y += rand() % (10 - newRoom->height) + 1; 47 | 48 | newRoom->doors = malloc(sizeof(Door *) * numberOfDoors); 49 | 50 | for (i = 0; i < numberOfDoors; i++) 51 | { 52 | newRoom->doors[i] = malloc(sizeof(Door)); 53 | newRoom->doors[i]->connected = 0; 54 | } 55 | 56 | /* top door */ 57 | newRoom->doors[0]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 58 | newRoom->doors[0]->position.y = newRoom->position.y; 59 | 60 | /* left door */ 61 | newRoom->doors[1]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 62 | newRoom->doors[1]->position.x = newRoom->position.x; 63 | 64 | /* bottom door */ 65 | newRoom->doors[2]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 66 | newRoom->doors[2]->position.y = newRoom->position.y + newRoom->height - 1; 67 | 68 | /* right door */ 69 | newRoom->doors[3]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 70 | newRoom->doors[3]->position.x = newRoom->position.x + newRoom->width - 1; 71 | 72 | return newRoom; 73 | } 74 | 75 | int drawRoom(Room * room) 76 | { 77 | int x; 78 | int y; 79 | 80 | /* draw top and bottom */ 81 | for (x = room->position.x; x < room->position.x + room->width; x++) 82 | { 83 | mvprintw(room->position.y, x, "-"); /* top */ 84 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 85 | } 86 | 87 | /* draw floors and side walls */ 88 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 89 | { 90 | /* draw side walls */ 91 | mvprintw(y, room->position.x, "|"); 92 | mvprintw(y, room->position.x + room->width - 1, "|"); 93 | 94 | /* draw floors */ 95 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 96 | { 97 | mvprintw(y, x, "."); 98 | } 99 | } 100 | 101 | /* draw doors */ 102 | mvprintw(room->doors[0]->position.y, room->doors[0]->position.x, "+"); 103 | mvprintw(room->doors[1]->position.y, room->doors[1]->position.x, "+"); 104 | mvprintw(room->doors[2]->position.y, room->doors[2]->position.x, "+"); 105 | mvprintw(room->doors[3]->position.y, room->doors[3]->position.x, "+"); 106 | 107 | return 1; 108 | } 109 | -------------------------------------------------------------------------------- /Tutorial 24 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | MAX_HEIGHT = 25; 6 | MAX_WIDTH = 100; 7 | 8 | initscr(); 9 | noecho(); 10 | keypad(stdscr, TRUE); 11 | refresh(); 12 | 13 | srand(time(NULL)); 14 | 15 | return 1; 16 | 17 | } 18 | 19 | int printGameHub(Level * level) 20 | { 21 | mvprintw(25, 0, " Level: %d", level->level); 22 | printw(" Gold: %d", level->user->gold); 23 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 24 | printw(" Attack: %d", level->user->attack); 25 | printw(" Exp: %d", level->user->exp); 26 | printw(" "); 27 | 28 | return 1; 29 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | //getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 24 End/src/windows/mainMenu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mainMenu.h" 5 | 6 | 7 | 8 | void closeMenu(int numberItems, MENU * menu, ITEM ** items) 9 | { 10 | int i; 11 | unpost_menu(menu); 12 | free_menu(menu); 13 | for (i = 0; i < numberItems; i++) 14 | { 15 | free_item(items[i]); 16 | } 17 | } 18 | 19 | int mainMenu(int numberItems, char * choices[]) 20 | { 21 | int i, c; 22 | int value; 23 | MENU * menu; 24 | ITEM ** items = malloc(sizeof(**items)*numberItems); 25 | ITEM * current; 26 | 27 | for (i = 0; i < numberItems; i++) 28 | { 29 | items[i] = new_item( choices[i], ""); 30 | } 31 | items[i] = (ITEM*)NULL; 32 | 33 | menu = new_menu((ITEM**)items); 34 | post_menu(menu); 35 | refresh(); 36 | 37 | // menu loop 38 | while (true) 39 | { 40 | c = getch(); 41 | switch (c) 42 | { 43 | case KEY_DOWN: 44 | menu_driver(menu, REQ_DOWN_ITEM); 45 | break; 46 | case KEY_UP: 47 | menu_driver(menu, REQ_UP_ITEM); 48 | break; 49 | case 10: // enter key 50 | current = current_item(menu); 51 | value = item_index(current); 52 | closeMenu(numberItems, menu, items); 53 | return value; 54 | } 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /Tutorial 25 End/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 25 End/include/door.h: -------------------------------------------------------------------------------- 1 | #ifndef DOOR_H 2 | #define DOOR_H 3 | 4 | #include "position.h" 5 | 6 | 7 | typedef struct Door 8 | { 9 | Position position; 10 | int connected; 11 | } Door; 12 | 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /Tutorial 25 End/include/game.h: -------------------------------------------------------------------------------- 1 | #ifndef GAME_H 2 | #define GAME_H 3 | 4 | #include "level.h" 5 | 6 | typedef struct Game 7 | { 8 | struct Level * levels[10]; 9 | int currentLevel; 10 | } Game; 11 | 12 | void render(Game * game); 13 | void gameLoop(Game * game); 14 | 15 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/level.h: -------------------------------------------------------------------------------- 1 | #ifndef LEVEL_H 2 | #define LEVEL_H 3 | 4 | 5 | #include "position.h" 6 | #include "monster.h" 7 | #include "player.h" 8 | #include "room.h" 9 | 10 | 11 | 12 | 13 | typedef struct Level 14 | { 15 | char ** tiles; 16 | int level; 17 | int numberOfRooms; 18 | struct Room ** rooms; 19 | struct Monster ** monsters; 20 | int numberOfMonsters; 21 | struct Player * user; 22 | } Level; 23 | 24 | void drawLevel(Level * level); 25 | 26 | void addMonsters(Level * level); 27 | void moveMonsters(Level * level); 28 | 29 | 30 | /* level/map functions */ 31 | Level * createLevel(); 32 | Room ** roomsSetUp(); 33 | char ** saveLevelPositions(); 34 | void connectDoors(Level * level); 35 | 36 | void checkPostion(Position * newPosition, Level * level); 37 | 38 | 39 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/mainMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_MENU_H 2 | #define MAIN_MENU_H 3 | 4 | enum {START_GAME, QUIT_GAME}; 5 | 6 | int mainMenu(int numberItems, char * choices[]); 7 | 8 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/monster.h: -------------------------------------------------------------------------------- 1 | #ifndef MONSTER_H 2 | #define MONSTER_H 3 | 4 | #include "position.h" 5 | 6 | 7 | typedef struct Monster 8 | { 9 | char string[2]; 10 | char symbol; 11 | int health; 12 | int attack; 13 | int speed; 14 | int defence; 15 | int pathfinding; 16 | int alive; 17 | Position * position; 18 | } Monster; 19 | 20 | /* monster functions */ 21 | Monster * selectMonster(int level); 22 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 23 | void pathfindingSeek(Position * start, Position * destination); 24 | void pathfindingRandom(Position * position); 25 | Monster * getMonsterAt(Position * position, Monster ** monsters); 26 | void killMonster(Monster * monster); 27 | void drawMonster(Monster * monster); 28 | 29 | 30 | 31 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/player.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYER_H 2 | #define PLAYER_H 3 | 4 | #include "position.h" 5 | 6 | typedef struct Player 7 | { 8 | Position * position; 9 | int health; 10 | int attack; 11 | int gold; 12 | int maxHealth; 13 | int exp; 14 | // Room * room; 15 | } Player; 16 | 17 | /* player functions */ 18 | Player * playerSetUp(); 19 | Position * handleInput(int input, Player * user); 20 | void playerMove(Position * newPosition, Player * user, char ** level); 21 | void drawPlayer(Player * player); 22 | 23 | 24 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/position.h: -------------------------------------------------------------------------------- 1 | #ifndef POSITION_H 2 | #define POSITION_H 3 | 4 | 5 | typedef struct Position { 6 | int x; 7 | int y; 8 | // TILE_TYPE tile; 9 | } Position; 10 | 11 | 12 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | 9 | /************* Global Variables *************/ 10 | int MAX_HEIGHT; 11 | int MAX_WIDTH; 12 | 13 | 14 | 15 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/include/room.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOM_H 2 | #define ROOM_H 3 | 4 | #include "monster.h" 5 | #include "player.h" 6 | #include "position.h" 7 | #include "door.h" 8 | 9 | 10 | 11 | typedef struct Room 12 | { 13 | Position position; 14 | int height; 15 | int width; 16 | 17 | struct Door ** doors; 18 | int numberOfDoors; 19 | // Monster ** monsters; 20 | // Item ** items; 21 | } Room; 22 | 23 | /* room functions */ 24 | Room * createRoom(int grid, int numberOfDoors); 25 | int drawRoom(Room * room); 26 | 27 | 28 | void placePlayer(Room ** rooms, Player * user); 29 | void setStartingPosition(Monster * monster, Room * room); 30 | 31 | 32 | 33 | #endif 34 | 35 | -------------------------------------------------------------------------------- /Tutorial 25 End/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | #include "level.h" 5 | #include "position.h" 6 | #include "player.h" 7 | #include "monster.h" 8 | 9 | 10 | void pathFind(Position * start, Position * end); 11 | 12 | /* screen functions */ 13 | void screenSetUp(); 14 | void printGameHub(Level * level); 15 | void combat(Player * player, Monster * monster, int order); 16 | 17 | #endif -------------------------------------------------------------------------------- /Tutorial 25 End/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -Wall -lmenu -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)windows/*.c\ 9 | $(SRCDIR)utils/*.c 10 | 11 | all: rogue 12 | 13 | rogue: $(SOURCES) 14 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 15 | 16 | run: 17 | ./rogue 18 | 19 | clean: 20 | rm rogue -------------------------------------------------------------------------------- /Tutorial 25 End/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 25 End/rogue.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "follow_symlinks": true, 6 | "path": "src" 7 | }, 8 | { 9 | "follow_symlinks": true, 10 | "path": "include" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /Tutorial 25 End/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 25 End/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | 5 | void combat(Player * player, Monster * monster, int order) 6 | { 7 | /* player attacking */ 8 | if (order == 1) 9 | { 10 | monster->health -= player->attack; 11 | if (monster->health > 0) 12 | { 13 | player->health -= monster->attack; 14 | } 15 | else 16 | { 17 | killMonster(monster); 18 | player->exp++; 19 | } 20 | } 21 | /* monster attacking */ 22 | else 23 | { 24 | player->health -= monster->attack; 25 | if (player->health > 0) 26 | { 27 | monster->health -= player->attack; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/game.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "game.h" 3 | #include "level.h" 4 | #include "utils.h" 5 | 6 | 7 | 8 | void render(Game * game) 9 | { 10 | clear(); 11 | printGameHub(game->levels[game->currentLevel - 1]); 12 | drawLevel(game->levels[game->currentLevel - 1]); 13 | } 14 | 15 | void gameLoop(Game * game) 16 | { 17 | int ch = '\0'; 18 | Position * newPosition; 19 | Level * level; 20 | 21 | if (game->currentLevel == 0) 22 | { 23 | game->levels[game->currentLevel] = createLevel(1); 24 | game->currentLevel++; 25 | } 26 | level = game->levels[game->currentLevel - 1]; 27 | 28 | 29 | /* main game loop */ 30 | while (ch != 'q') 31 | { 32 | newPosition = handleInput(ch, level->user); 33 | checkPostion(newPosition, level); 34 | moveMonsters(level); 35 | 36 | render(game); 37 | 38 | if (level->user->health <= 0) 39 | { 40 | game->currentLevel = 0; 41 | return; 42 | } 43 | ch = getch(); 44 | } 45 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "mainMenu.h" 3 | #include "game.h" 4 | #include "utils.h" 5 | 6 | 7 | void menuLoop() 8 | { 9 | int choice; 10 | char * choices[] = {"Start Game", "End Game"}; 11 | 12 | Game game; 13 | game.currentLevel = 0; 14 | 15 | while (true) 16 | { 17 | choice = mainMenu(2, choices); 18 | 19 | switch (choice) 20 | { 21 | case START_GAME: 22 | gameLoop(&game); 23 | clear(); 24 | break; 25 | case QUIT_GAME: 26 | return; 27 | break; 28 | } 29 | } 30 | } 31 | 32 | int main () 33 | { 34 | screenSetUp(); 35 | menuLoop(); 36 | endwin(); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/monster.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "monster.h" 3 | 4 | 5 | 6 | Monster * selectMonster(int level) 7 | { 8 | int monster; 9 | switch (level) 10 | { 11 | case 1: 12 | case 2: 13 | case 3: 14 | monster = (rand() % 2) + 1; 15 | break; 16 | case 4: 17 | case 5: 18 | monster = (rand() % 2) + 2; 19 | break; 20 | case 6: 21 | monster = 3; 22 | break; 23 | } 24 | 25 | /* 26 | 27 | 1 Spider 28 | symbol: X 29 | levels: 1-3 30 | health: 2 31 | attack: 1 32 | speed: 1 33 | defence: 1 34 | pathfinding: 1 (random) 35 | 36 | 2 Goblin 37 | symbol: G 38 | levels: 1-5 39 | health: 5 40 | attack: 3 41 | speed: 1 42 | defence: 1 43 | pathfinding: 2 (seeking) 44 | 45 | 3 Troll 46 | symbol: T 47 | levels: 4-6 48 | health: 15 49 | attack: 5 50 | speed: 1 51 | defence: 1 52 | pathfinding: 1 (random) 53 | 54 | */ 55 | 56 | switch (monster) 57 | { 58 | case 1: /* spider */ 59 | return createMonster('X', 2, 1, 1, 1, 1); 60 | case 2: /* goblin */ 61 | return createMonster('G', 5, 3, 1, 1, 2); 62 | case 3: /* troll */ 63 | return createMonster('T', 15, 5, 1, 1, 1); 64 | default: 65 | return createMonster('O', 15, 5, 1, 1, 1); 66 | } 67 | 68 | 69 | 70 | } 71 | 72 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding) 73 | { 74 | Monster * newMonster; 75 | newMonster = malloc(sizeof(Monster)); 76 | 77 | 78 | newMonster->symbol = symbol; 79 | newMonster->health = health; 80 | newMonster->attack = attack; 81 | newMonster->speed = speed; 82 | newMonster->defence = defence; 83 | newMonster->pathfinding = pathfinding; 84 | newMonster->alive = 1; 85 | 86 | sprintf(newMonster->string, "%c", symbol); 87 | 88 | newMonster->position = malloc(sizeof(Position)); 89 | 90 | return newMonster; 91 | } 92 | 93 | void killMonster(Monster * monster) 94 | { 95 | monster->alive = 0; 96 | } 97 | 98 | 99 | 100 | void pathfindingRandom(Position * position) 101 | { 102 | int random; 103 | 104 | random = rand() % 5; 105 | 106 | switch (random) 107 | { 108 | /* step up */ 109 | case 0: 110 | if (mvinch(position->y - 1, position->x) == '.') 111 | { 112 | position->y = position->y - 1; 113 | } 114 | break; 115 | 116 | /* step down */ 117 | case 1: 118 | if (mvinch(position->y + 1, position->x) == '.') 119 | { 120 | position->y = position->y + 1; 121 | } 122 | break; 123 | 124 | /* step left */ 125 | case 2: 126 | if (mvinch(position->y, position->x - 1) == '.') 127 | { 128 | position->x = position->x - 1; 129 | } 130 | break; 131 | 132 | /* step right */ 133 | case 3: 134 | if (mvinch(position->y, position->x + 1) == '.') 135 | { 136 | position->x = position->x + 1; 137 | } 138 | break; 139 | 140 | /* do nothing */ 141 | case 4: 142 | break; 143 | 144 | } 145 | } 146 | 147 | void pathfindingSeek(Position * start, Position * destination) 148 | { 149 | /* step left */ 150 | if ((abs((start->x - 1) - destination->x) < abs(start->x - destination->x)) && (mvinch(start->y, start->x - 1) == '.')) 151 | { 152 | start->x = start->x - 1; 153 | 154 | /* step right */ 155 | } else if ((abs((start->x + 1) - destination->x) < abs(start->x - destination->x)) && (mvinch(start->y, start->x + 1) == '.')) 156 | { 157 | start->x = start->x + 1; 158 | 159 | /* step down */ 160 | } else if ((abs((start->y + 1) - destination->y) < abs(start->y - destination->y)) && (mvinch(start->y + 1, start->x) == '.')) 161 | { 162 | start->y = start->y + 1; 163 | 164 | /* step up */ 165 | } else if ((abs((start->y - 1) - destination->y) < abs(start->y - destination->y)) && (mvinch(start->y - 1, start->x) == '.')) 166 | { 167 | start->y = start->y - 1; 168 | } else 169 | { 170 | /* do nothing */ 171 | } 172 | } 173 | 174 | Monster * getMonsterAt(Position * position, Monster ** monsters) 175 | { 176 | int x; 177 | for (x = 0; x < 6; x++) 178 | { 179 | if ((position->y == monsters[x]->position->y) && (position->x == monsters[x]->position->x)) 180 | return monsters[x]; 181 | } 182 | 183 | return NULL; 184 | } 185 | 186 | void drawMonster(Monster * monster) 187 | { 188 | if (monster->alive) 189 | { 190 | mvprintw(monster->position->y, monster->position->x, monster->string); 191 | } 192 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "player.h" 3 | 4 | 5 | Player * playerSetUp() 6 | { 7 | Player * newPlayer; 8 | newPlayer = malloc(sizeof(Player)); 9 | newPlayer->position = malloc(sizeof(Position)); 10 | 11 | newPlayer->health = 20; 12 | newPlayer->attack = 1; 13 | newPlayer->gold = 0; 14 | newPlayer->exp = 0; 15 | newPlayer->maxHealth = 20; 16 | 17 | return newPlayer; 18 | } 19 | 20 | 21 | Position * handleInput(int input, Player * user) 22 | { 23 | Position * newPosition; 24 | newPosition = malloc(sizeof(Position)); 25 | switch (input) 26 | { 27 | /* move up */ 28 | case 'w': 29 | case 'W': 30 | newPosition->y = user->position->y - 1; 31 | newPosition->x = user->position->x; 32 | break; 33 | 34 | /* move down */ 35 | case 's': 36 | case 'S': 37 | newPosition->y = user->position->y + 1; 38 | newPosition->x = user->position->x; 39 | break; 40 | 41 | /* move left */ 42 | case 'a': 43 | case 'A': 44 | newPosition->y = user->position->y; 45 | newPosition->x = user->position->x - 1; 46 | break; 47 | 48 | /* move right */ 49 | case 'd': 50 | case 'D': 51 | newPosition->y = user->position->y; 52 | newPosition->x = user->position->x + 1; 53 | break; 54 | 55 | default: 56 | break; 57 | 58 | } 59 | 60 | return newPosition; 61 | 62 | } 63 | 64 | 65 | void playerMove(Position * newPosition, Player * user, char ** level) 66 | { 67 | user->position->y = newPosition->y; 68 | user->position->x = newPosition->x; 69 | } 70 | 71 | void drawPlayer(Player * player) 72 | { 73 | mvprintw(player->position->y, player->position->x, "@"); 74 | move(player->position->y, player->position->x); 75 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "room.h" 3 | 4 | Room * createRoom(int grid, int numberOfDoors) 5 | { 6 | int i; 7 | Room * newRoom; 8 | newRoom = malloc(sizeof(Room)); 9 | newRoom->numberOfDoors = numberOfDoors; 10 | 11 | switch (grid) 12 | { 13 | case 0: 14 | newRoom->position.x = 0; 15 | newRoom->position.y = 0; 16 | break; 17 | case 1: 18 | newRoom->position.x = 33; 19 | newRoom->position.y = 0; 20 | break; 21 | 22 | case 2: 23 | newRoom->position.x = 66; 24 | newRoom->position.y = 0; 25 | break; 26 | case 3: 27 | newRoom->position.x = 0; 28 | newRoom->position.y = 14; 29 | break; 30 | 31 | case 4: 32 | newRoom->position.x = 33; 33 | newRoom->position.y = 14; 34 | break; 35 | case 5: 36 | newRoom->position.x = 66; 37 | newRoom->position.y = 14; 38 | break; 39 | 40 | } 41 | 42 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 43 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 44 | 45 | /* offset */ 46 | newRoom->position.x += rand() % (30 - newRoom->width) + 1; 47 | newRoom->position.y += rand() % (10 - newRoom->height) + 1; 48 | 49 | newRoom->doors = malloc(sizeof(Door *) * numberOfDoors); 50 | 51 | for (i = 0; i < numberOfDoors; i++) 52 | { 53 | newRoom->doors[i] = malloc(sizeof(Door)); 54 | newRoom->doors[i]->connected = 0; 55 | } 56 | 57 | /* top door */ 58 | newRoom->doors[0]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 59 | newRoom->doors[0]->position.y = newRoom->position.y; 60 | 61 | /* left door */ 62 | newRoom->doors[1]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 63 | newRoom->doors[1]->position.x = newRoom->position.x; 64 | 65 | /* bottom door */ 66 | newRoom->doors[2]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 67 | newRoom->doors[2]->position.y = newRoom->position.y + newRoom->height - 1; 68 | 69 | /* right door */ 70 | newRoom->doors[3]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 71 | newRoom->doors[3]->position.x = newRoom->position.x + newRoom->width - 1; 72 | 73 | return newRoom; 74 | } 75 | 76 | int drawRoom(Room * room) 77 | { 78 | int x; 79 | int y; 80 | 81 | /* draw top and bottom */ 82 | for (x = room->position.x; x < room->position.x + room->width; x++) 83 | { 84 | mvprintw(room->position.y, x, "-"); /* top */ 85 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 86 | } 87 | 88 | /* draw floors and side walls */ 89 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 90 | { 91 | /* draw side walls */ 92 | mvprintw(y, room->position.x, "|"); 93 | mvprintw(y, room->position.x + room->width - 1, "|"); 94 | 95 | /* draw floors */ 96 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 97 | { 98 | mvprintw(y, x, "."); 99 | } 100 | } 101 | 102 | /* draw doors */ 103 | mvprintw(room->doors[0]->position.y, room->doors[0]->position.x, "+"); 104 | mvprintw(room->doors[1]->position.y, room->doors[1]->position.x, "+"); 105 | mvprintw(room->doors[2]->position.y, room->doors[2]->position.x, "+"); 106 | mvprintw(room->doors[3]->position.y, room->doors[3]->position.x, "+"); 107 | 108 | return 1; 109 | } 110 | 111 | 112 | void placePlayer(Room ** rooms, Player * user) 113 | { 114 | user->position->x = rooms[3]->position.x + 1; 115 | user->position->y = rooms[3]->position.y + 1; 116 | } 117 | 118 | 119 | 120 | void setStartingPosition(Monster * monster, Room * room) 121 | { 122 | monster->position->x = (rand() % (room->width - 2)) + room->position.x + 1; 123 | monster->position->y = (rand() % (room->height - 2)) + room->position.y + 1; 124 | } 125 | -------------------------------------------------------------------------------- /Tutorial 25 End/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | 5 | void screenSetUp() 6 | { 7 | MAX_HEIGHT = 25; 8 | MAX_WIDTH = 100; 9 | 10 | initscr(); 11 | noecho(); 12 | keypad(stdscr, TRUE); 13 | refresh(); 14 | 15 | srand(time(NULL)); 16 | } 17 | 18 | void printGameHub(Level * level) 19 | { 20 | mvprintw(25, 0, " Level: %d", level->level); 21 | printw(" Gold: %d", level->user->gold); 22 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 23 | printw(" Attack: %d", level->user->attack); 24 | printw(" Exp: %d", level->user->exp); 25 | printw(" "); 26 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | //getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 25 End/src/windows/mainMenu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mainMenu.h" 5 | 6 | 7 | 8 | void closeMenu(int numberItems, MENU * menu, ITEM ** items) 9 | { 10 | int i; 11 | unpost_menu(menu); 12 | free_menu(menu); 13 | for (i = 0; i < numberItems; i++) 14 | { 15 | free_item(items[i]); 16 | } 17 | } 18 | 19 | int mainMenu(int numberItems, char * choices[]) 20 | { 21 | int i, c; 22 | int value; 23 | MENU * menu; 24 | ITEM ** items = malloc(sizeof(**items)*numberItems); 25 | ITEM * current; 26 | 27 | for (i = 0; i < numberItems; i++) 28 | { 29 | items[i] = new_item( choices[i], ""); 30 | } 31 | items[i] = (ITEM*)NULL; 32 | 33 | menu = new_menu((ITEM**)items); 34 | post_menu(menu); 35 | refresh(); 36 | 37 | // menu loop 38 | while (true) 39 | { 40 | c = getch(); 41 | switch (c) 42 | { 43 | case KEY_DOWN: 44 | menu_driver(menu, REQ_DOWN_ITEM); 45 | break; 46 | case KEY_UP: 47 | menu_driver(menu, REQ_UP_ITEM); 48 | break; 49 | case 10: // enter key 50 | current = current_item(menu); 51 | value = item_index(current); 52 | closeMenu(numberItems, menu, items); 53 | return value; 54 | } 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/grid: -------------------------------------------------------------------------------- 1 | 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 2 | 1 | | 3 | 2 | | 4 | 3 | | 5 | 4 | | 6 | 5 | | 7 | 6 | | 8 | 7 | ----- | 9 | 8 | -...- | 10 | 9 | -...- | 11 | 10 | ----- | 12 | 11 | | 13 | 12 | | 14 | 13------------------------------|-------------------------------|--------------------------------------- 15 | 14 | | 16 | 15 | | 17 | 16 | | 18 | 17 | | 19 | 18 | | 20 | 19 | | 21 | 20 | | 22 | 21 | | 23 | 22 | | 24 | 23 | | 25 | 24 | | 26 | 25 Level: 1 Gold: 0 Hp: 20(25) Attack: 1 Exp: 20 27 | -------------------------------------------------------------------------------- /Tutorial 25 Start/include/game.h: -------------------------------------------------------------------------------- 1 | #ifndef GAME_H 2 | #define GAME_H 3 | 4 | 5 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/level.h: -------------------------------------------------------------------------------- 1 | #ifndef LEVEL_H 2 | #define LEVEL_H 3 | 4 | 5 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/mainMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef MAIN_MENU_H 2 | #define MAIN_MENU_H 3 | 4 | enum {START_GAME, QUIT_GAME}; 5 | 6 | int mainMenu(int numberItems, char * choices[]); 7 | 8 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/monster.h: -------------------------------------------------------------------------------- 1 | #ifndef MONSTER_H 2 | #define MONSTER_H 3 | 4 | 5 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/player.h: -------------------------------------------------------------------------------- 1 | #ifndef PLAYER_H 2 | #define PLAYER_H 3 | 4 | 5 | 6 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/position.h: -------------------------------------------------------------------------------- 1 | #ifndef POSITION_H 2 | #define POSITION_H 3 | 4 | 5 | 6 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/rogue.h: -------------------------------------------------------------------------------- 1 | #ifndef ROGUE_H 2 | #define ROGUE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | /************ Struct Definitions ***************/ 9 | 10 | typedef struct Game 11 | { 12 | struct Level * levels[10]; 13 | int currentLevel; 14 | } Game; 15 | 16 | typedef struct Level 17 | { 18 | char ** tiles; 19 | int level; 20 | int numberOfRooms; 21 | struct Room ** rooms; 22 | struct Monster ** monsters; 23 | int numberOfMonsters; 24 | struct Player * user; 25 | } Level; 26 | 27 | typedef struct Position { 28 | int x; 29 | int y; 30 | // TILE_TYPE tile; 31 | } Position; 32 | 33 | typedef struct Room 34 | { 35 | Position position; 36 | int height; 37 | int width; 38 | 39 | struct Door ** doors; 40 | int numberOfDoors; 41 | // Monster ** monsters; 42 | // Item ** items; 43 | } Room; 44 | 45 | typedef struct Door 46 | { 47 | Position position; 48 | int connected; 49 | } Door; 50 | 51 | typedef struct Player 52 | { 53 | Position * position; 54 | int health; 55 | int attack; 56 | int gold; 57 | int maxHealth; 58 | int exp; 59 | // Room * room; 60 | } Player; 61 | 62 | typedef struct Monster 63 | { 64 | char string[2]; 65 | char symbol; 66 | int health; 67 | int attack; 68 | int speed; 69 | int defence; 70 | int pathfinding; 71 | int alive; 72 | Position * position; 73 | } Monster; 74 | 75 | /************* Global Variables *************/ 76 | int MAX_HEIGHT; 77 | int MAX_WIDTH; 78 | 79 | /* screen functions */ 80 | int screenSetUp(); 81 | int printGameHub(Level * level); 82 | 83 | /* level/map functions */ 84 | Level * createLevel(); 85 | Room ** roomsSetUp(); 86 | char ** saveLevelPositions(); 87 | void connectDoors(Level * level); 88 | 89 | /* player functions */ 90 | Player * playerSetUp(); 91 | Position * handleInput(int input, Player * user); 92 | int checkPostion(Position * newPosition, Level * level); 93 | int playerMove(Position * newPosition, Player * user, char ** level); 94 | int placePlayer(Room ** rooms, Player * user); 95 | 96 | void drawPlayer(Player * player); 97 | 98 | /* room functions */ 99 | Room * createRoom(int grid, int numberOfDoors); 100 | int drawRoom(Room * room); 101 | 102 | /* monster functions */ 103 | int addMonsters(Level * level); 104 | Monster * selectMonster(int level); 105 | Monster * createMonster(char symbol, int health, int attack, int speed, int defence, int pathfinding); 106 | int setStartingPosition(Monster * monster, Room * room); 107 | int moveMonsters(Level * level); 108 | int pathfindingSeek(Position * start, Position * destination); 109 | int pathfindingRandom(Position * position); 110 | Monster * getMonsterAt(Position * position, Monster ** monsters); 111 | int killMonster(Monster * monster); 112 | 113 | void drawMonster(Monster * monster); 114 | 115 | 116 | int combat(Player * player, Monster * monster, int order); 117 | 118 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/include/room.h: -------------------------------------------------------------------------------- 1 | #ifndef ROOM_H 2 | #define ROOM_H 3 | 4 | 5 | #endif 6 | 7 | -------------------------------------------------------------------------------- /Tutorial 25 Start/include/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | 4 | void pathFind(Position * start, Position * end); 5 | 6 | 7 | #endif -------------------------------------------------------------------------------- /Tutorial 25 Start/makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -lmenu -lncurses -I$(IDIR) -g 3 | 4 | IDIR = ./include/ 5 | SRCDIR = ./src/ 6 | 7 | SOURCES = $(SRCDIR)*.c\ 8 | $(SRCDIR)windows/*.c\ 9 | $(SRCDIR)utils/*.c 10 | 11 | all: rogue 12 | 13 | rogue: $(SOURCES) 14 | $(CC) $(SOURCES) $(CFLAGS) -o $@ 15 | 16 | run: 17 | ./rogue 18 | 19 | clean: 20 | rm rogue -------------------------------------------------------------------------------- /Tutorial 25 Start/map.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 0 1 2 3 6 | + ___ + ___ + ___ + ___ + 7 | 0 | | | | | 8 | | | | | x | 9 | + ___ + ___ + ___ + ___ + 10 | 1 | | | | | 11 | | | s | | | 12 | + ___ + ___ + ___ + ___ + 13 | 2 | | | | | 14 | | | | | | 15 | + ___ + ___ + ___ + ___ + 16 | 17 | 0 1 2 3 18 | + ___ + ___ + ___ + ___ + 19 | 0 | | | | | 20 | | | | | <- | <- | 21 | + ___ + _V_ + ___ + ___ + 22 | 1 | | | | | 23 | | | s | | | 24 | + ___ + ___ + ___ + ___ + 25 | 2 | | | | | 26 | | | | | | 27 | + ___ + ___ + ___ + ___ + 28 | 29 | ^ 30 | frontier[y][x]: (1, 1), (1, 0), (0, 1), (1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (1, 3), (2, 2) 31 | 32 | cameFrom[1][1]: (-9, -9) 33 | cameFrom[1][0]: (1, 1) 34 | cameFrom[0][1]: (1, 1) 35 | cameFrom[1][2]: (1, 1) 36 | cameFrom[2][1]: (1, 1) 37 | cameFrom[0][0]: (1, 0) 38 | cameFrom[2][0]: (1, 2) 39 | cameFrom[0][2]: (0, 1) 40 | cameFrom[1][3]: (1, 2) 41 | cameFrom[2][2]: (1, 2) 42 | cameFrom[0][3]: (0, 2) 43 | cameFrom[][]: 44 | 45 | index 8 46 | count 11 47 | 48 | 49 | -------------------------------------------------------------------------------- /Tutorial 25 Start/rogue.sublime-project: -------------------------------------------------------------------------------- 1 | { 2 | "folders": 3 | [ 4 | { 5 | "follow_symlinks": true, 6 | "path": "src" 7 | }, 8 | { 9 | "follow_symlinks": true, 10 | "path": "include" 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /Tutorial 25 Start/sources.list: -------------------------------------------------------------------------------- 1 | # 2 | 3 | # deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 4 | 5 | #deb cdrom:[Debian GNU/Linux 6.0.10 _Squeeze_ - Official amd64 NETINST Binary-1 20140719-18:59]/ squeeze main 6 | 7 | deb http://ftp3.nrc.ca/debian/ squeeze main 8 | deb-src http://ftp3.nrc.ca/debian/ squeeze main 9 | 10 | deb http://security.debian.org/ squeeze/updates main 11 | deb-src http://security.debian.org/ squeeze/updates main 12 | 13 | # squeeze-updates, previously known as 'volatile' 14 | deb http://ftp3.nrc.ca/debian/ squeeze-updates main 15 | deb-src http://ftp3.nrc.ca/debian/ squeeze-updates main 16 | -------------------------------------------------------------------------------- /Tutorial 25 Start/src/combat.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int combat(Player * player, Monster * monster, int order) 4 | { 5 | /* player attacking */ 6 | if (order == 1) 7 | { 8 | monster->health -= player->attack; 9 | if (monster->health > 0) 10 | { 11 | player->health -= monster->attack; 12 | } 13 | else 14 | { 15 | killMonster(monster); 16 | player->exp++; 17 | } 18 | } 19 | /* monster attacking */ 20 | else 21 | { 22 | player->health -= monster->attack; 23 | if (player->health > 0) 24 | { 25 | monster->health -= player->attack; 26 | } 27 | } 28 | 29 | return 1; 30 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/game.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | void render(Game * game) 4 | { 5 | clear(); 6 | printGameHub(game->levels[game->currentLevel - 1]); 7 | drawLevel(game->levels[game->currentLevel - 1]); 8 | } 9 | 10 | int gameLoop(Game * game) 11 | { 12 | int ch = '\0'; 13 | Position * newPosition; 14 | Level * level; 15 | 16 | if (game->currentLevel == 0) 17 | { 18 | game->levels[game->currentLevel] = createLevel(1); 19 | game->currentLevel++; 20 | } 21 | level = game->levels[game->currentLevel - 1]; 22 | 23 | 24 | /* main game loop */ 25 | while (ch != 'q') 26 | { 27 | newPosition = handleInput(ch, level->user); 28 | checkPostion(newPosition, level); 29 | moveMonsters(level); 30 | 31 | render(game); 32 | 33 | if (level->user->health <= 0) 34 | { 35 | game->currentLevel = 0; 36 | return -1; 37 | } 38 | ch = getch(); 39 | } 40 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/level.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | // typedef struct Level 4 | // { 5 | // char ** tiles; 6 | // int numberOfRooms; 7 | // struct Room ** rooms; 8 | // struct Monster ** monsters; 9 | // int numberOfMonsters; 10 | // } Level; 11 | 12 | 13 | Level * createLevel(int level) 14 | { 15 | Level * newLevel; 16 | newLevel = malloc(sizeof(Level)); 17 | 18 | newLevel->level = level; 19 | newLevel->numberOfRooms = 6; 20 | newLevel->rooms = roomsSetUp(); 21 | connectDoors(newLevel); 22 | newLevel->tiles = saveLevelPositions(); 23 | 24 | newLevel->user = playerSetUp(); 25 | placePlayer(newLevel->rooms, newLevel->user); 26 | 27 | addMonsters(newLevel); 28 | 29 | return newLevel; 30 | } 31 | 32 | void drawLevel(Level * level) 33 | { 34 | int x, y, i; 35 | 36 | // printing tiles 37 | for (y = 0; y < MAX_HEIGHT; y++) 38 | { 39 | for (x = 0; x < MAX_WIDTH; x++) 40 | { 41 | mvaddch(y, x, level->tiles[y][x]); 42 | } 43 | } 44 | 45 | // print monsters 46 | for (i = 0; i < level->numberOfMonsters; i++) 47 | { 48 | drawMonster(level->monsters[i]); 49 | } 50 | 51 | drawPlayer(level->user); 52 | } 53 | 54 | Room ** roomsSetUp() 55 | { 56 | int x; 57 | Room ** rooms; 58 | rooms = malloc(sizeof(Room)*6); 59 | 60 | for (x = 0; x < 6; x++) 61 | { 62 | rooms[x] = createRoom(x, 4); 63 | drawRoom(rooms[x]); 64 | } 65 | //connectDoors(rooms[0]->doors[3], rooms[1]->doors[1]); 66 | //pathFind(rooms[0]->doors[3], rooms[1]->doors[1]); 67 | //connectDoors(rooms[1]->doors[2], rooms[0]->doors[0]); 68 | 69 | return rooms; 70 | } 71 | 72 | void connectDoors(Level * level) 73 | { 74 | int i, j; 75 | int randomRoom, randomDoor; 76 | int count; 77 | 78 | for (i = 0; i < level->numberOfRooms; i++) 79 | { 80 | for (j = 0; j < level->rooms[i]->numberOfDoors; j++) 81 | { 82 | if (level->rooms[i]->doors[j]->connected == 1) 83 | { 84 | continue; 85 | } 86 | 87 | count = 0; 88 | 89 | while (count < 2) 90 | { 91 | randomRoom = rand() % level->numberOfRooms; 92 | randomDoor = rand() % level->rooms[randomRoom]->numberOfDoors; 93 | 94 | if (level->rooms[randomRoom]->doors[randomDoor]->connected == 1 || randomRoom == i) 95 | { 96 | count++; 97 | continue; 98 | } 99 | 100 | pathFind(&level->rooms[randomRoom]->doors[randomDoor]->position, &level->rooms[i]->doors[j]->position); 101 | 102 | level->rooms[randomRoom]->doors[randomDoor]->connected = 1; 103 | level->rooms[i]->doors[j]->connected = 1; 104 | break; 105 | } 106 | 107 | 108 | 109 | } 110 | } 111 | 112 | } 113 | 114 | 115 | char ** saveLevelPositions() 116 | { 117 | int x, y; 118 | char ** positions; 119 | positions = malloc(sizeof(char *) * 25); 120 | 121 | for (y = 0; y < MAX_HEIGHT; y++) 122 | { 123 | positions[y] = malloc(sizeof(char) * 100); 124 | for (x = 0; x < MAX_WIDTH; x++) 125 | { 126 | positions[y][x] = mvinch(y, x); 127 | } 128 | } 129 | 130 | return positions; 131 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/main.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "mainMenu.h" 3 | 4 | 5 | void menuLoop() 6 | { 7 | int choice; 8 | char * choices[] = {"Start Game", "End Game"}; 9 | 10 | Game game; 11 | game.currentLevel = 0; 12 | 13 | while (true) 14 | { 15 | choice = mainMenu(2, choices); 16 | 17 | switch (choice) 18 | { 19 | case START_GAME: 20 | gameLoop(&game); 21 | clear(); 22 | break; 23 | case QUIT_GAME: 24 | return; 25 | break; 26 | } 27 | } 28 | } 29 | 30 | int main () 31 | { 32 | screenSetUp(); 33 | menuLoop(); 34 | endwin(); 35 | 36 | return 0; 37 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/player.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Player * playerSetUp() 4 | { 5 | Player * newPlayer; 6 | newPlayer = malloc(sizeof(Player)); 7 | newPlayer->position = malloc(sizeof(Position)); 8 | 9 | newPlayer->health = 20; 10 | newPlayer->attack = 1; 11 | newPlayer->gold = 0; 12 | newPlayer->exp = 0; 13 | newPlayer->maxHealth = 20; 14 | 15 | return newPlayer; 16 | } 17 | 18 | int placePlayer(Room ** rooms, Player * user) 19 | { 20 | user->position->x = rooms[3]->position.x + 1; 21 | user->position->y = rooms[3]->position.y + 1; 22 | } 23 | 24 | Position * handleInput(int input, Player * user) 25 | { 26 | Position * newPosition; 27 | newPosition = malloc(sizeof(Position)); 28 | switch (input) 29 | { 30 | /* move up */ 31 | case 'w': 32 | case 'W': 33 | newPosition->y = user->position->y - 1; 34 | newPosition->x = user->position->x; 35 | break; 36 | 37 | /* move down */ 38 | case 's': 39 | case 'S': 40 | newPosition->y = user->position->y + 1; 41 | newPosition->x = user->position->x; 42 | break; 43 | 44 | /* move left */ 45 | case 'a': 46 | case 'A': 47 | newPosition->y = user->position->y; 48 | newPosition->x = user->position->x - 1; 49 | break; 50 | 51 | /* move right */ 52 | case 'd': 53 | case 'D': 54 | newPosition->y = user->position->y; 55 | newPosition->x = user->position->x + 1; 56 | break; 57 | 58 | default: 59 | break; 60 | 61 | } 62 | 63 | return newPosition; 64 | 65 | } 66 | 67 | /* check what is at next position */ 68 | int checkPostion(Position * newPosition, Level * level) 69 | { 70 | Player * user; 71 | user = level->user; 72 | int space; 73 | switch (mvinch(newPosition->y, newPosition->x)) 74 | { 75 | case '.': 76 | case '#': 77 | case '+': 78 | playerMove(newPosition, user, level->tiles); 79 | break; 80 | case 'X': 81 | case 'G': 82 | case 'T': 83 | combat(user, getMonsterAt(newPosition, level->monsters), 1); 84 | default: 85 | break; 86 | } 87 | 88 | } 89 | 90 | int playerMove(Position * newPosition, Player * user, char ** level) 91 | { 92 | user->position->y = newPosition->y; 93 | user->position->x = newPosition->x; 94 | } 95 | 96 | void drawPlayer(Player * player) 97 | { 98 | mvprintw(player->position->y, player->position->x, "@"); 99 | move(player->position->y, player->position->x); 100 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/room.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | Room * createRoom(int grid, int numberOfDoors) 4 | { 5 | int i; 6 | Room * newRoom; 7 | newRoom = malloc(sizeof(Room)); 8 | newRoom->numberOfDoors = numberOfDoors; 9 | 10 | switch (grid) 11 | { 12 | case 0: 13 | newRoom->position.x = 0; 14 | newRoom->position.y = 0; 15 | break; 16 | case 1: 17 | newRoom->position.x = 33; 18 | newRoom->position.y = 0; 19 | break; 20 | 21 | case 2: 22 | newRoom->position.x = 66; 23 | newRoom->position.y = 0; 24 | break; 25 | case 3: 26 | newRoom->position.x = 0; 27 | newRoom->position.y = 14; 28 | break; 29 | 30 | case 4: 31 | newRoom->position.x = 33; 32 | newRoom->position.y = 14; 33 | break; 34 | case 5: 35 | newRoom->position.x = 66; 36 | newRoom->position.y = 14; 37 | break; 38 | 39 | } 40 | 41 | newRoom->height = rand() % 6 + 4; /* max size 9 */ 42 | newRoom->width = rand() % 14 + 4; /* max size 17 */ 43 | 44 | /* offset */ 45 | newRoom->position.x += rand() % (30 - newRoom->width) + 1; 46 | newRoom->position.y += rand() % (10 - newRoom->height) + 1; 47 | 48 | newRoom->doors = malloc(sizeof(Door *) * numberOfDoors); 49 | 50 | for (i = 0; i < numberOfDoors; i++) 51 | { 52 | newRoom->doors[i] = malloc(sizeof(Door)); 53 | newRoom->doors[i]->connected = 0; 54 | } 55 | 56 | /* top door */ 57 | newRoom->doors[0]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 58 | newRoom->doors[0]->position.y = newRoom->position.y; 59 | 60 | /* left door */ 61 | newRoom->doors[1]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 62 | newRoom->doors[1]->position.x = newRoom->position.x; 63 | 64 | /* bottom door */ 65 | newRoom->doors[2]->position.x = rand() % (newRoom->width - 2) + newRoom->position.x + 1; 66 | newRoom->doors[2]->position.y = newRoom->position.y + newRoom->height - 1; 67 | 68 | /* right door */ 69 | newRoom->doors[3]->position.y = rand() % (newRoom->height - 2) + newRoom->position.y + 1; 70 | newRoom->doors[3]->position.x = newRoom->position.x + newRoom->width - 1; 71 | 72 | return newRoom; 73 | } 74 | 75 | int drawRoom(Room * room) 76 | { 77 | int x; 78 | int y; 79 | 80 | /* draw top and bottom */ 81 | for (x = room->position.x; x < room->position.x + room->width; x++) 82 | { 83 | mvprintw(room->position.y, x, "-"); /* top */ 84 | mvprintw(room->position.y + room->height - 1, x, "-"); /* bottom */ 85 | } 86 | 87 | /* draw floors and side walls */ 88 | for (y = room->position.y + 1; y < room->position.y + room->height - 1; y++) 89 | { 90 | /* draw side walls */ 91 | mvprintw(y, room->position.x, "|"); 92 | mvprintw(y, room->position.x + room->width - 1, "|"); 93 | 94 | /* draw floors */ 95 | for (x = room->position.x + 1; x position.x + room->width - 1; x++) 96 | { 97 | mvprintw(y, x, "."); 98 | } 99 | } 100 | 101 | /* draw doors */ 102 | mvprintw(room->doors[0]->position.y, room->doors[0]->position.x, "+"); 103 | mvprintw(room->doors[1]->position.y, room->doors[1]->position.x, "+"); 104 | mvprintw(room->doors[2]->position.y, room->doors[2]->position.x, "+"); 105 | mvprintw(room->doors[3]->position.y, room->doors[3]->position.x, "+"); 106 | 107 | return 1; 108 | } 109 | -------------------------------------------------------------------------------- /Tutorial 25 Start/src/screen.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | 3 | int screenSetUp() 4 | { 5 | MAX_HEIGHT = 25; 6 | MAX_WIDTH = 100; 7 | 8 | initscr(); 9 | noecho(); 10 | keypad(stdscr, TRUE); 11 | refresh(); 12 | 13 | srand(time(NULL)); 14 | 15 | return 1; 16 | 17 | } 18 | 19 | int printGameHub(Level * level) 20 | { 21 | mvprintw(25, 0, " Level: %d", level->level); 22 | printw(" Gold: %d", level->user->gold); 23 | printw(" Hp: %d(%d)", level->user->health, level->user->maxHealth); 24 | printw(" Attack: %d", level->user->attack); 25 | printw(" Exp: %d", level->user->exp); 26 | printw(" "); 27 | 28 | return 1; 29 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/utils/pathFinding.c: -------------------------------------------------------------------------------- 1 | #include "rogue.h" 2 | #include "utils.h" 3 | 4 | void addPositionYX(int ** frontier, int frontierCount, int y, int x) 5 | { 6 | frontier[frontierCount][0] = y; 7 | frontier[frontierCount][1] = x; 8 | } 9 | 10 | 11 | int checkPosition(int y, int x) 12 | { 13 | char temp = mvinch(y, x); 14 | 15 | if (temp == '.' || temp == '|' || temp == '-') 16 | return 0; 17 | else 18 | return 1; 19 | } 20 | 21 | int addNeighbors(int ** frontier, int frontierCount, int *** cameFrom, int y, int x) 22 | { 23 | // north 24 | if (y > 0 && cameFrom[y - 1][x][0] < 0 && checkPosition(y - 1, x)) 25 | { 26 | addPositionYX(frontier, frontierCount, y - 1, x); 27 | frontierCount++; 28 | cameFrom[y - 1][x][0] = y; 29 | cameFrom[y - 1][x][1] = x; 30 | } 31 | 32 | // south 33 | if (y < (MAX_HEIGHT - 1) && cameFrom[y + 1][x][0] < 0 && checkPosition(y + 1, x)) 34 | { 35 | addPositionYX(frontier, frontierCount, y + 1, x); 36 | frontierCount++; 37 | cameFrom[y + 1][x][0] = y; 38 | cameFrom[y + 1][x][1] = x; 39 | } 40 | 41 | // east 42 | if (x < (MAX_WIDTH - 1) && cameFrom[y][x + 1][0] < 0 && checkPosition(y, x + 1)) 43 | { 44 | addPositionYX(frontier, frontierCount, y, x + 1); 45 | frontierCount++; 46 | cameFrom[y][x + 1][0] = y; 47 | cameFrom[y][x + 1][1] = x; 48 | } 49 | 50 | // west 51 | if (x > 0 && cameFrom[y][x - 1][0] < 0 && checkPosition(y, x - 1)) 52 | { 53 | addPositionYX(frontier, frontierCount, y, x - 1); 54 | frontierCount++; 55 | cameFrom[y][x - 1][0] = y; 56 | cameFrom[y][x - 1][1] = x; 57 | } 58 | 59 | return frontierCount; 60 | 61 | } 62 | 63 | void pathFind(Position * start, Position * end) 64 | { 65 | int i, j; 66 | int x, y; 67 | int tempY; 68 | int ** frontier = malloc(sizeof(int*) * MAX_HEIGHT * MAX_WIDTH); 69 | int *** cameFrom = malloc(sizeof(int**) * MAX_HEIGHT ); 70 | 71 | int frontierIndex = 0; 72 | int frontierCount = 0; 73 | 74 | for (i = 0; i < MAX_HEIGHT * MAX_WIDTH; i++) 75 | { 76 | frontier[i] = malloc(sizeof(int)*2); 77 | } 78 | 79 | for (i = 0; i < MAX_HEIGHT; i++) 80 | { 81 | cameFrom[i] = malloc(sizeof(int*)* MAX_WIDTH); 82 | for (j = 0; j < MAX_WIDTH; j++) 83 | { 84 | cameFrom[i][j] = malloc(sizeof(int)*2); 85 | cameFrom[i][j][0] = -1; 86 | cameFrom[i][j][1] = -1; 87 | } 88 | } 89 | 90 | // add start to cameFrom 91 | cameFrom[start->y][start->x][0] = -9; 92 | cameFrom[start->y][start->x][1] = -9; 93 | 94 | // add start position to frontier 95 | addPositionYX(frontier, frontierCount, start->y, start->x); 96 | frontierCount++; 97 | 98 | 99 | while (frontierIndex < frontierCount) 100 | { 101 | y = frontier[frontierIndex][0]; 102 | x = frontier[frontierIndex][1]; 103 | 104 | frontierIndex++; 105 | 106 | if (y == end->y && x == end->x) 107 | { 108 | break; 109 | } 110 | 111 | frontierCount = addNeighbors(frontier, frontierCount, cameFrom, y, x); 112 | } 113 | 114 | y = end->y; 115 | x = end->x; 116 | 117 | while (y != start->y || x != start->x) 118 | { 119 | tempY = y; 120 | y = cameFrom[tempY][x][0]; 121 | x = cameFrom[tempY][x][1]; 122 | mvprintw(y, x, "+"); 123 | //getch(); 124 | } 125 | } -------------------------------------------------------------------------------- /Tutorial 25 Start/src/windows/mainMenu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mainMenu.h" 5 | 6 | 7 | 8 | void closeMenu(int numberItems, MENU * menu, ITEM ** items) 9 | { 10 | int i; 11 | unpost_menu(menu); 12 | free_menu(menu); 13 | for (i = 0; i < numberItems; i++) 14 | { 15 | free_item(items[i]); 16 | } 17 | } 18 | 19 | int mainMenu(int numberItems, char * choices[]) 20 | { 21 | int i, c; 22 | int value; 23 | MENU * menu; 24 | ITEM ** items = malloc(sizeof(**items)*numberItems); 25 | ITEM * current; 26 | 27 | for (i = 0; i < numberItems; i++) 28 | { 29 | items[i] = new_item( choices[i], ""); 30 | } 31 | items[i] = (ITEM*)NULL; 32 | 33 | menu = new_menu((ITEM**)items); 34 | post_menu(menu); 35 | refresh(); 36 | 37 | // menu loop 38 | while (true) 39 | { 40 | c = getch(); 41 | switch (c) 42 | { 43 | case KEY_DOWN: 44 | menu_driver(menu, REQ_DOWN_ITEM); 45 | break; 46 | case KEY_UP: 47 | menu_driver(menu, REQ_UP_ITEM); 48 | break; 49 | case 10: // enter key 50 | current = current_item(menu); 51 | value = item_index(current); 52 | closeMenu(numberItems, menu, items); 53 | return value; 54 | } 55 | } 56 | 57 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # rogue 2 | A rogue/nethack like game written in C, using ncurses for graphics 3 | 4 | Tutorial 16 - Add a game hub to the screen 5 | Tutorial 17 - Randomly generated rooms 6 | Tutorial 18 - A quick modification to our makefile 7 | Tutorial 19 - Theory for breadth first search 8 | Tutorial 20 - Code our breadth first search algorithm 9 | Tutorial 21 - Solve the issue of our halls moving through walls. 10 | Tutorial 22 - Connecting of all rooms together. 11 | 12 | ![Rogue](rogue01.jpg) 13 | -------------------------------------------------------------------------------- /rogue01.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wadsworj/rogue/be4105623afd5c9c683508c60cc935a4d98e9524/rogue01.jpg --------------------------------------------------------------------------------