-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.c
More file actions
256 lines (202 loc) · 5.33 KB
/
engine.c
File metadata and controls
256 lines (202 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/******************************************
* engine.c - main game engine for textgame.
* it takes care of core functions: menu, save
* and load state, player encounters and so on.
* story-line progression and actual happen in
* gameme.c
***********************£££££*"""**********/
#include <stdio.h>
#include<string.h>
#include <stdbool.h>
// funcs
int engine_init();
int get_and_fix_line(char line[30]);
// internal game funcs
//
static int game_banner(void);
static int game_banner_init(void);
static int new_game(void);
static int set_player_info();
static int player_attack(int dmg, int mp);
1
extern int NewQuest();
extern int NextQuest();
extern int PrevQuest();
extern int ListQuest();
// other funcs in game.c
// global vars
// internal vars
extern float ver = 1.1;
extern int user_choice;
// game mechanics vars
typedef enum {
KNIGHT,
MAGE,
NECROMANCER,
ARCHER,
ROUGE,
ASSASSIAN
} Class;
extern Class;
typedef enum {
// monster or npc races
Human,
Goblin,
Hobogoblin,
Imp,
Demon
} Race;
extern Race;
typedef struct PlayerType{
char name[30];
int hp,mp;
float gp;
class Class;
int AGI,STR,INT,CHA;
int DMG;
race player_race;
int number_of_encounters; // monsters defeated
char inventory[40];
} PlayerType;
extern PlayerType;
extern typedef struct NPC {
char name[30];
int hp,mp;
bool gender; //0 - F and 1 - M.
float gp;
bool is_enemy;
bool is_friend;
bool is_neutral;
} NPC;
extern typedef struct Monster{
char name[30];
int hp,mp;
int DMG;
// rop_type
race race;
class monster_class;
} Monster;
struct Quest {
int8_t quest_id[10];
char quest_name[30];
size_t reward_type;
// reward
struct &Quest *prev_quest;
} Quest;
// main
int engine_init(void /*argc*/){
fprintf(stdout,"Starting game\n");
game_banner();
int start_err = game_banner_init();
return 0;
// check for err
if (start_err != 0) {
fprintf(stderr, "Error Occured Displaying Banner. Error Code %d\n", start_err);
return -1;
}
}
int game_banner(void) {
//menu
printf("textgame - Echoes of The Ocean\n Version:%f\n", ver);
printf("1) New Game\n");
printf("2) Load Game\n");
printf("3) Options\n");
printf("4) Multiplayer\n");
printf("5) Quit\n");
return 0;
}
int game_banner_init(void) {
char line[50];
int running = 0; // running flag
while(running == 0) {
printf("> Choose:");
// take input and remove newline
/* fgets(line, sizeof(line), stdin);
line[strcspn(line, "\n")] = '\0';
sscanf(line, "%d", &user_choice); */
get_and_fix_line(line);
sscanf(line, "%d", &user_choice);
switch (user_choice) {
case 1:
fprintf(stdout,"Starting New Game\n");
if (new_game() != 0) {
fprintf(stderr, "(!) Error : Unable to Start New Game");
game_banner();
break;
};
running = 1;
break;
case 3:
printf("Options:\n");
printf("N/A\n");
printf("<- Type 'b' to go back\n");
get_and_fix_line(line);
if (strcmp(line, 'b') == 0) {
game_banner();
}
break;
case 5:
printf("Exiting\n");
return 0;
break;
default :
fprintf(stderr, "(!) Error : Invalid Choice. Pick Something Else.\n");
break;
}
}
}
// begin new game and get/set related info
int new_game(void){
// dialouge
printf("Beginning new game.....\n");
printf("Welcome to the lands of estoldonobarnia !.\n");
// set player info, class and others
if(set_player_info() != 0) {
// quit if err
fprintf(stderr,"(!) Error Occured: Error Setting Player Info.\n");
return -1;
};
return 0;
}
// sets related player information, returns 0 if ok and -1 if err
int set_player_info(void){
// initalize player
PlayerType Player;
PlayerType *ptr_player = &Player; // note: Player.name is ok but ptr_player.name is invalid as its a pointer and must bedeferenced.
// read line
char line[30]; // line buffer
printf(">Enter the name of your character:\n");
// remove newline and add to struct
get_and_fix_line(line);
strcpy((*ptr_player).name, line);
printf("\n>What is youre gender? %s", ptr_player->name);
// set class
printf("\n>What do you prefer? (1) a sword, (2) a bow, (3) a dagger, (4) posion or (5) a magic staff?. Choose:(1-5):\n");
get_and_fix_line(line);
sscanf(line, "%d", &userchoice);
switch (user_choice) {
case 1:
(*ptr_player).class = Class KNIGHT;
printf("You're interest in the sword has let you to becoming a KNIGHT\n");
printf("+5 DMG");
case 2:
ptr_player->class = ARCHER;
case 3:
(*ptr_player).class = ROUGE;
case 4:
ptr_player->class = ASSASSIAN;
}
return 0; }
// get input and fix and check errs
int get_and_fix_line(char line[30]) {
if(fgets(line, 30, stdin) == NULL) {
fprintf(stderr, "Empty Input or EOF\n");
return -1;
}; // remove newline
line[strcspn(line, "\n")] = '\0'; // check if empty input
if (line[0] == '\n'){
fprintf(stderr, "Empty.\n");
return 2;
};
return 0;
}