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