-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
147 lines (124 loc) · 3.61 KB
/
game.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
/* The game settings file parser
* vim:set cin sm ts=8 sw=4 sts=4: - Sec <sec@42.org>
* $Id: game.c,v 1.8 2003/12/08 16:36:30 sec Exp $
*/
#include "brillion.h"
/* Game file format:
first line has to be "Brillion <version>"
only <version>==42 is currently supported
once:
"lives \d+" (number of lives)
many:
"bg \w+" (background image filename)
"level \w+" (level setup filename)
*/
a_game* read_game(const char * file){
FILE * f;
a_game * game;
a_level level;
int levelnum=0;
int num;
int error=0;
int x;
/* for parsing */
char* word[9];
#define LINELEN 80
char* trgt;
unsigned int space;
assert(file);
if(chdir(file)){
fprintf(stderr,"Game '%s': ",file);perror("");
return(NULL);
};
f=fopen("Settings","r");
if(!f){
fprintf(stderr,"Game '%s': Settings: ",file);perror("");
return(NULL);
};
word[0]=(char*)malloc(LINELEN+1);
if( fscanf(f,"%10s %d ",word[0],&num) < 2 ||
strncmp(word[0],"Brillion",8) || (num != 42)){
fprintf(stderr,"Game '%s': Corrupt file format\n",file);
free(word[0]);
return(NULL);
};
game=calloc(1,sizeof(a_game));
game->layout=calloc(1,sizeof(a_layout));
while(!error && (fgets(word[0],LINELEN,f)!=NULL)){
if((num=strlen(word[0]))==LINELEN){
fprintf(stderr,"Game '%s': Line too long\n",file);
error++;
}else{
x=0;space=0;
for(trgt=word[0];x<9 && trgt<word[0]+num;trgt++) {
if( *trgt == ' ' || *trgt == '\t' || *trgt == '\r' || *trgt == '\n'){
*trgt=0;
space=1;
}else{
if(space){
space=0;
word[++x]=trgt;
};
};
};
word[++x]=NULL;
if(word[0][0] == '#'){ /* Handle comments */
word[0][0]=0; x=1;
}
/* for(num=0;num<x;num++) fprintf(stderr,"%d: '%s'\n",num,word[num]); */
#define BEG_PARSE(s,z) if(!strncmp(word[0],s,strlen(s)+1)){if((z+1)!=x){fprintf(stderr,"Directive %s requires %d words\n",s,(z+1));error++;}else
#define OR_PARSE(s,z) }else if(!strncmp(word[0],s,strlen(s)+1)){if((z+1)!=x){fprintf(stderr,"Directive %s requires %d words\n",s,(z+1));error++;}else
#define END_PARSE }else
BEG_PARSE("",0){
/* printf("empty line!\n"); */
}OR_PARSE("cpts",2){
game->layout->pts.x=atoi(word[1]);
game->layout->pts.y=atoi(word[2]);
}OR_PARSE("ctime",2){
game->layout->time.x=atoi(word[1]);
game->layout->time.y=atoi(word[2]);
}OR_PARSE("cblocks",2){
game->layout->blocks.x=atoi(word[1]);
game->layout->blocks.y=atoi(word[2]);
}OR_PARSE("clives",2){
game->layout->lives.x=atoi(word[1]);
game->layout->lives.y=atoi(word[2]);
}OR_PARSE("clevel",2){
game->layout->level.x=atoi(word[1]);
game->layout->level.y=atoi(word[2]);
}OR_PARSE("lives",1){
game->lives=atoi(word[1]);
}OR_PARSE("bg",1){
printf("bg not impl.\n");
}OR_PARSE("level",1){
if(levelnum>=game->maxlevel){
game->maxlevel+=21;
game->levels=realloc(game->levels,
game->maxlevel*(sizeof(a_level*)));
};
level.name=(char*)malloc(strlen(word[1])+1);
strcpy(level.name,word[1]);
game->levels[levelnum]=(a_level*)malloc(sizeof(a_level));
memcpy(game->levels[levelnum],&level,sizeof(a_level));
levelnum++;
}END_PARSE{
fprintf(stderr,"'%s' is not a valid command\n",word[0]);
error++;
};
}; /* end if line !too long */
}; /* end while */
game->maxlevel=levelnum;
if(game->lives == 0){
fprintf(stderr,"Defaulting to 3 lives/game\n");
game->lives=3;
};
if(error){
fprintf(stderr,"Game '%s': Parse error at '%s'\n",file,word[0]);
free(game);
free(word[0]);
return(NULL);
};
free(word[0]);
game->scores=read_scores();
return game;
}