2019908cdb8b2d595cd6d6b876b337bd9faf7448
[matthijs/ABM2.git] / ABM2 / SchemeReader.cpp
1 // stdafx.cpp : source file that includes just the standard includes\r
2 // Bomberman_SchemeReader.pch will be the pre-compiled header\r
3 // stdafx.obj will contain the pre-compiled type information\r
4 #define _CRT_SECURE_NO_DEPRECATE\r
5 #define min(a, b)  (((a) < (b)) ? (a) : (b)) \r
6 \r
7 #include "SchemeReader.h"\r
8 #include <fstream>\r
9 \r
10 using namespace std;\r
11 \r
12 // TODO: reference any additional headers you need in STDAFX.H\r
13 // and not in this file\r
14 \r
15 int StrIndex(char* source, int start, int length, char* dest) {\r
16         if (source == NULL || dest == NULL) return 0;\r
17         int stringlength = strlen(source);\r
18         int copylen = min(length, stringlength);\r
19         if (start > stringlength) return 0;//stupid check\r
20         if (start+length > strlen(source)) length = stringlength - start;\r
21         strncpy(dest, source+start, length);\r
22         dest[length]=0;\r
23         return length;\r
24 }\r
25 \r
26 SchemeReader::SchemeReader(char *Filename) {\r
27         char ReadBuffer[256];\r
28         int num=0, counter;\r
29         char rowcontents[PLAYGROUND_NUMFIELD_X+1];\r
30         int startx,starty,team;\r
31         int BornWith, HasOverride, Override, Forbidden;\r
32 \r
33         ifstream SchemeFile (Filename, ifstream::in);\r
34         while (!(SchemeFile.eof())) {\r
35                 SchemeFile.getline(ReadBuffer, 256);\r
36                 switch(ReadBuffer[0]) {\r
37                         case '-': {\r
38                                 switch(ReadBuffer[1]) {\r
39                                         case 'R': {\r
40                                                 //PlayField data\r
41                                                 sscanf(ReadBuffer, "-R,%d,%s", &num, rowcontents);\r
42                                                 for (counter=0; counter < strlen(rowcontents); counter++) {\r
43                                                         switch(rowcontents[counter]) {\r
44                                                                 case '#': \r
45                                                                         PlayField[num*PLAYGROUND_NUMFIELD_X+counter] = SOLID;\r
46                                                                         break;\r
47                                                                 case ':':\r
48                                                                         PlayField[num*PLAYGROUND_NUMFIELD_X+counter] = BRICK;\r
49                                                                         break;\r
50                                                                 case '.':\r
51                                                                         PlayField[num*PLAYGROUND_NUMFIELD_X+counter] = BLANK;\r
52                                                         }\r
53                                                 }\r
54                                                 break;\r
55                                         }\r
56                                         case 'S': {\r
57                                                 //Player Starting position data\r
58                                                 sscanf(ReadBuffer, "-S,%d,%d,%d,%d", &num, &startx, &starty, &team);\r
59                                                 Player[num].startx=startx;\r
60                                                 Player[num].starty=starty;\r
61                                                 Player[num].team=team;\r
62                                                 break;\r
63                                         }\r
64                                         case 'P': {\r
65                                                 //Powerup data\r
66                                                 sscanf(ReadBuffer, "-P,%d,%d,%d,%d,%d", &num, &BornWith, &HasOverride, &Override, &Forbidden);\r
67                                                 Powerups[num].BornWith=BornWith;\r
68                                                 Powerups[num].HasOverride=HasOverride;\r
69                                                 Powerups[num].Override=Override;\r
70                                                 Powerups[num].Forbidden=Forbidden;\r
71                                                 break;\r
72                                         }\r
73                                         case 'N': {\r
74                                                 //Scheme Name\r
75                                                 StrIndex(ReadBuffer, 3,256, Name);\r
76                                                 break;\r
77                                         }\r
78                                         case 'B': {\r
79                                                 //Brick Density\r
80                                                 sscanf(ReadBuffer, "-B,%d", &BrickDensity);\r
81                                                 break;\r
82                                         }\r
83                                         case 'V': {\r
84                                                 //Version\r
85                                                 //ignore\r
86                                                 break;\r
87                                         }\r
88                                 }\r
89                                 break;\r
90                         }\r
91                 }\r
92         }\r
93         SchemeFile.close();\r
94 }