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