]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - data/qcsrc/common/campaign_file.qc
Initial checkout of Vore Tournament 0.1.alpha.
[voretournament/voretournament.git] / data / qcsrc / common / campaign_file.qc
1 // CampaignFileLoad(offset, n)\r
2 // - Loads campaign level data (up to n entries starting at offset)\r
3 //   into the globals\r
4 // - Returns the number of entries successfully read\r
5 float CampaignFile_Load(float offset, float n)\r
6 {\r
7         float fh;\r
8         float lineno;\r
9         float entlen;\r
10         float i;\r
11         string l, a;\r
12         string fn;\r
13 \r
14         if(n > CAMPAIGN_MAX_ENTRIES)\r
15                 n = CAMPAIGN_MAX_ENTRIES;\r
16 \r
17         campaign_offset = offset;\r
18         campaign_entries = 0;\r
19         campaign_title = string_null;\r
20 \r
21         fn = strcat("maps/campaign", campaign_name, ".txt");\r
22         fh = fopen(fn, FILE_READ);\r
23         if(fh >= 0)\r
24         {\r
25                 for(lineno = 0; (l = fgets(fh)); )\r
26                 {\r
27                         if(strlen(l) == 0)\r
28                                 continue; // empty line\r
29                         if(substring(l, 0, 11) == "//campaign:")\r
30                                 campaign_title = substring(l, 11, strlen(l) - 11);\r
31                         if(substring(l, 0, 2) == "//")\r
32                                 continue; // comment\r
33                         if(substring(l, 0, 12) == "\"//campaign:")\r
34                                 campaign_title = substring(l, 12, strlen(l) - 13);\r
35                         if(substring(l, 0, 3) == "\"//")\r
36                                 continue; // comment\r
37                         if(lineno >= offset)\r
38                         {\r
39                                 entlen = tokenize(l); // using insane tokenizer for CSV\r
40 \r
41 #define CAMPAIGN_GETARG0                  if(i >= entlen)\r
42 #define CAMPAIGN_GETARG1 CAMPAIGN_GETARG0     error("syntax error in campaign file: line has not enough fields");\r
43 #define CAMPAIGN_GETARG2 CAMPAIGN_GETARG1 a = argv(++i);\r
44 #define CAMPAIGN_GETARG3 CAMPAIGN_GETARG2 if(a == ",")\r
45 #define CAMPAIGN_GETARG4 CAMPAIGN_GETARG3     a = "";\r
46 #define CAMPAIGN_GETARG5 CAMPAIGN_GETARG4 else\r
47 #define CAMPAIGN_GETARG  CAMPAIGN_GETARG5     ++i\r
48 // What you're seeing here is what people will do when your compiler supports\r
49 // C-style macros but no line continuations.\r
50 \r
51                                 i = -1; // starts at -1 so I don't need postincrement; that is, i points to BEFORE the current arg!\r
52                                 CAMPAIGN_GETARG; campaign_gametype[campaign_entries] = strzone(a);\r
53                                 CAMPAIGN_GETARG; campaign_mapname[campaign_entries] = strzone(a);\r
54                                 CAMPAIGN_GETARG; campaign_bots[campaign_entries] = stof(a);\r
55                                 CAMPAIGN_GETARG; campaign_botskill[campaign_entries] = stof(a);\r
56                                 CAMPAIGN_GETARG; campaign_fraglimit[campaign_entries] = stof(a);\r
57                                 CAMPAIGN_GETARG; campaign_mutators[campaign_entries] = strzone(a);\r
58                                 CAMPAIGN_GETARG; campaign_shortdesc[campaign_entries] = strzone(a);\r
59                                 CAMPAIGN_GETARG; campaign_longdesc[campaign_entries] = strzone(strreplace("\\n", "\n", a));\r
60                                 campaign_entries = campaign_entries + 1;\r
61 \r
62                                 if(campaign_entries >= n)\r
63                                         break;\r
64                         }\r
65                         lineno = lineno + 1;\r
66                 }\r
67                 fclose(fh);\r
68         }\r
69 \r
70         campaign_title = strzone(campaign_title);\r
71 \r
72         return campaign_entries;\r
73 }\r
74 \r
75 void CampaignFile_Unload()\r
76 {\r
77         float i;\r
78         if(campaign_title)\r
79         {\r
80                 strunzone(campaign_title);\r
81                 for(i = 0; i < campaign_entries; ++i)\r
82                 {\r
83                         strunzone(campaign_gametype[i]);\r
84                         strunzone(campaign_mapname[i]);\r
85                         strunzone(campaign_mutators[i]);\r
86                         strunzone(campaign_shortdesc[i]);\r
87                         strunzone(campaign_longdesc[i]);\r
88                 }\r
89                 campaign_entries = 0;\r
90                 campaign_title = string_null;\r
91         }\r
92 }\r