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