-
Notifications
You must be signed in to change notification settings - Fork 1
/
f.c
356 lines (338 loc) · 7.18 KB
/
f.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include "f.h"
#include "save.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
static int size_enemis;
// clear the screen
void clear(){
system("cls||clear");
}
// show the commands
void help(){
printf(
"Usage :\n"
"\th -> show help\n"
"\tclear -> clear the screen\n"
"\texit -> quit\n"
"\tbattle -> start a battle\n"
"\tinvent -> manage team inventory\n"
"\tmodify team :\n"
"\t\tadd -> add a perso\n"
"\t\trem -> remove a perso\n"
"\t\torder -> change order of the team\n"
"\tshow :\n"
"\t\tsteam -> show the team\n"
"\t\tsintel -> show the team\n"
"\t\tssemi -> show the team\n"
"\t\tssilly -> show the team\n"
"\tsave :\n"
"\t\tload -> load a team from a file\n"
"\t\tsave -> save the team for later\n"
);
}
// allow the user to type command to interact with the program
int shell(perso** t){
char* cmd = malloc(_NAME_LENGHT);
while(strcmp(cmd,"exit")){
printf("(h for help)> ");
fgets(cmd, _NAME_LENGHT, stdin);
if(cmd[strlen(cmd)-1] != '\n')
_FLUSH
cmd[strlen(cmd)-1] = '\0';
if(!strcmp(cmd,"h")){
help();
} else if(!strcmp(cmd,"add")){
if(t == 0){
t = init();
}else{
add(t);
}
} else if(!strcmp(cmd,"battle")||!strcmp(cmd,"b")){
if(t == 0){
printf("No team to battle with...\n");
}else{
int diff = scan("Difficulty : ");
intel **enemis = malloc(sizeof(intel*)*diff+1);
enemis = init_battle(diff);
team_round(t,enemis);
free(enemis);
}
} else if (!strcmp(cmd,"invent")){
invent(t);
} else if(!strcmp(cmd,"rem")){
rem(t);
} else if(!strcmp(cmd,"order")){
set_order(t);
} else if(!strcmp(cmd,"steam")){
show_team(t);
} else if(!strcmp(cmd,"sintel")){
show_intel(intel_mobs);
} else if(!strcmp(cmd,"ssemi")){
show_semi(semi_mobs);
} else if(!strcmp(cmd,"ssilly")){
show_idiot(idiot_mobs);
} else if(!strcmp(cmd,"save")){
saveForLater(t);
} else if(!strcmp(cmd,"load")){
char* fname = malloc(_NAME_LENGHT);
printf("file : ");
fgets(fname, _NAME_LENGHT, stdin);
if(fname[strlen(fname)-1] != '\n')
_FLUSH
fname[strlen(fname)-1] = '\0';
t = loadForNow(fname);
free(fname);
} else if(!strcmp(cmd,"clear")){
clear();
}
}
free(cmd);
return 0;
}
// scan pour un int + gestion des erreurs
//
// return la valeur obtenu
int scan(char* s){
int buff = -1;
while(buff < 0){
printf("%s",s);
while(scanf("%d",&buff) == 0){
_FLUSH
printf("%s",s);
}
}
_FLUSH
return buff;
}
// demande les infos d'un perso
//
// prend comme argument un pointeur vers un perso
void ask(perso *p, int i){
printf("perso %d\t:\n",i+1);
printf("\tnom : ");
p->nom = malloc(_NAME_LENGHT); fgets(p->nom, _NAME_LENGHT, stdin);
if(p->nom[strlen(p->nom)-1] != '\n')
_FLUSH
p->nom[strlen(p->nom)-1] = '\0';
p->maxPV = scan("\tpv : ");
p->armure = scan("\tarmure : ");
p->degat = scan("\tdegat : ");
p->mana = scan("\tmana : ");
}
// créer les perso
//
// return un tableau de perso
perso** init(){
int nPlayer = scan("How many players : ");
if(nPlayer == 0){
return 0;
}
perso **p = malloc(sizeof(intel)*nPlayer);
for(int i = 0; i < nPlayer; i++){
p[i] = malloc(sizeof(intel));
ask(p[i],i);
p[i]->inv = 0;
}
return p;
}
// return the length of the perso array
int get_length(perso **t){
int i = 0;
while(t[i]!=NULL){
i++;
}
return i;
}
// add a perso
void add(perso** t){
clear();
int index = get_length(t);
t[index] = malloc(sizeof(perso));
ask(t[index],index);
}
// remove a perso
void rem(perso** t){
int index = askName(t, "who do you wanna remove : ");
while(index == -1){
index = askName(t, "who do you wanna remove : ");
}
if(index == -3){
return;
}
while(t[index]!=NULL){ // swich the indexes to remove the perso
t[index] = t[index+1];
index++;
}
}
// ask for a name and return the index of this one
//
// return -1 if not found
//
// return -3 if \n
int askName(perso** t, char* s){
if(t == 0){
return -3;
}
int i = 0;
printf("%s",s);
char* buff = malloc(_NAME_LENGHT); fgets(buff, _NAME_LENGHT, stdin);
if(!strcmp(buff,"\n")){ // if just press ENTER
return -3;
}
if(buff[strlen(buff)-1] != '\n') // si le nom est trop grand -> vide stdin
_FLUSH
buff[strlen(buff)-1] = '\0';
while(t[i]!=NULL){
if(!strcmp(buff,t[i]->nom)){ // if name in list of name
return i;
}
i++;
}
return -1;
}
// permet de changer l'ordre de la team
void set_order(perso** t){
int n,i = 0;
char* ask = malloc(200);
perso *buff = malloc(sizeof(perso));
while(t[i]!=NULL){
clear();
show_team(t);
sprintf(ask, "who takes the place of [%s] : ", t[i]->nom);
n = askName(t, ask);
if(n != -1 && n != -3) { // switch the two names
buff = t[i];
t[i] = t[n];
t[n] = buff;
i++;
}
n == -3 ? i++ : 0;
}
clear();
// printf("After order :");
// show_team(t);
}
// initialise the battle choosing random enemis
intel** init_battle(int diff){
if(diff == 0){
printf("combat with 0 mob isn't allowed...\n");
diff = 2;
}
size_enemis = diff;
srand(time(NULL));
int i,j;
int size = sizeof(intel_mobs)/sizeof(intel_mobs[0]);
size--;
intel** mobs = malloc(sizeof(intel*)*diff+1);
for(i = 0; i<diff;i++){
j = (rand()%size);
//memcpy(mobs[i],&intel_mobs[j],sizeof(intel));
//mobs[i] = &intel_mobs[j];
mobs[i] = malloc(sizeof(intel));
mobs[i]->nom = malloc(_NAME_LENGHT);
mobs[i]->nom = intel_mobs[j].nom;
mobs[i]->maxPV = intel_mobs[j].maxPV;
mobs[i]->armure = intel_mobs[j].armure;
mobs[i]->degat = intel_mobs[j].degat;
mobs[i]->mana = intel_mobs[j].mana;
}
mobs[diff] = 0;
return mobs;
}
// remove life
//
// who is to know who attaque who
//
// 1 -> p; 0 -> i
int attaque(perso* p, intel* i,int who){
if(p->maxPV == 0 || i->maxPV == 0){
return -1;
}
if(who == 1){
i->maxPV -= p->degat;
if(i->maxPV < 0){
i->maxPV = 0;
}
} else {
p->maxPV -= i->degat;
if(p->maxPV < 0){
p->maxPV = 0;
}
}
printf("%s -> %dpv\n%s -> %dpv\n",
p->nom,p->maxPV,
i->nom,i->maxPV);
return 0;
}
// ask players what to do until all enemis are dead
void player_turn(perso** t, int i, intel** e){
int n;
while(t[i]!=NULL){
printf(
"%s's turn :\n"
"\t1. attaque\n"
"\t2. defense\n"
"\t3. rien\n"
, t[i]->nom);
n=scan("> ");
while(n <= 0 || n >= 4){
n = scan("> ");
}
switch (n) {
case 1:
printf("Go go go %s\n",t[i]->nom);
n = scan("which enemi : ");
while(n < 0 || n > size_enemis-1){
n = scan("which enemi : ");
}
while(attaque(t[i],e[n],1)!=0){
n = scan("which enemi : ");
while(n < 0 || n > size_enemis-1){
n = scan("which enemi : ");
}
attaque(t[i], e[n],1);
}
break;
case 2:
printf("defending!\n");
//defense(t[i]);
break;
case 3:
printf("nothing...\n");
break;
}
if(enemisDown(e) == 0){
break;
}
i++;
show_enemis(e);
}
}
// return -1 if all enemis aren't down
//
// return 0 if all enemis are down
int enemisDown(intel** e){
int i=0;
while(e[i]!=NULL){
if(e[i]->maxPV != 0){
return -1;
}
i++;
}
return 0;
}
// handle the battle
void team_round(perso** t, intel** e){
clear();
show_team(t);
printf("VS\n");
show_enemis(e);
int i,n;
while(enemisDown(e)!=0){
i = 0;
player_turn(t,i,e);
}
printf("all enemis are dead...\n");
}