]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/campaign.qc
Allow campaign levels to specify leadlimit
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / campaign.qc
1 #include "campaign.qh"
2
3 #include "defs.qh"
4
5 #include "cheats.qh"
6 #include "miscfunctions.qh"
7 #include "g_world.qh"
8
9 #include "../common/campaign_common.qh"
10
11 // campaign cvars:
12 //   _campaign_index: index of CURRENT map
13 //   _campaign_name: name of the current campaign
14 //   g_campaign(name)_index: index of current LAST map (saved)
15 //   g_campaign_skill: bot skill offset
16
17 float campaign_level;
18 float campaign_won;
19 string campaign_index_var;
20 //float checkrules_equality;
21
22 float CampaignBailout(string s)
23 {
24 #if 0
25         cvar = cvar_normal;
26         cvar_string = cvar_string_normal;
27         cvar_set = cvar_set_normal;
28 #endif
29         cvar_set("g_campaign", "0");
30         LOG_INFO("^4campaign initialization failed: ", s);
31         if(autocvar__campaign_testrun)
32                 error("CAMPAIGN FAIL AHAHAHAHAHAHAHAHAH))");
33         return 1;
34 }
35
36 #if 0
37 string cvar_campaignwrapper_list; // string of format ; var value; var value; var value;
38 string cvar_string_campaignwrapper(string theCvar)
39 {
40         float p, q;
41         p = strstrofs(cvar_campaignwrapper_list, strcat("; ", theCvar, " "), 0);
42         if(p < 0)
43                 return cvar_defstring(theCvar);
44         p += strlen(theCvar) + 3;
45         q = strstrofs(cvar_campaignwrapper_list, ";", p);
46         if(q < 0)
47                 return cvar_defstring(theCvar);
48         return substring(cvar_campaignwrapper_list, p, q - p);
49 }
50 float cvar_campaignwrapper(string theCvar)
51 {
52         return stof(cvar_string_campaignwrapper(theCvar));
53 }
54 void cvar_set_campaignwrapper(string theCvar, string theValue)
55 {
56         if(cvar_string_campaignwrapper(theCvar) == theValue)
57                 return;
58         string s = cvar_campaignwrapper_list;
59         cvar_campaignwrapper_list = strzone(strcat("; ", theCvar, " ", theValue, s));
60         strunzone(s);
61         //print(cvar_campaignwrapper_list, "\n");
62 }
63 #endif
64
65 float Campaign_Invalid()
66 {
67         string thismapname, wantedmapname;
68         thismapname = GetMapname();
69         wantedmapname = campaign_gametype[0];
70         if(MapInfo_CurrentGametype() != MapInfo_Type_FromString(wantedmapname))
71                 return CampaignBailout("wrong game type!");
72         wantedmapname = campaign_mapname[0];
73         if(wantedmapname != thismapname)
74                 return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
75         return 0;
76 }
77
78 void CampaignPreInit()
79 {
80         float baseskill;
81         string title;
82         campaign_level = autocvar__campaign_index;
83         campaign_name = strzone(autocvar__campaign_name);
84         campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
85         CampaignFile_Load(campaign_level, 2);
86
87         if(campaign_entries < 1)
88         {
89                 CampaignBailout("unknown map");
90                 return;
91         }
92
93         if(autocvar_sv_cheats)
94         {
95                 MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
96                 CampaignFile_Unload();
97                 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
98                 return;
99         }
100
101         baseskill = max(0, autocvar_g_campaign_skill + campaign_botskill[0]);
102         campaign_forcewin = false;
103
104         cvar_set("sv_public", "0");
105         cvar_set("pausable", "1");
106
107 #if 0
108         cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
109 #else
110         string cvar_campaignwrapper_list = strcat("; ", campaign_mutators[0], "; ");
111         int argc = tokenizebyseparator(cvar_campaignwrapper_list, "; ");
112         if(argc > 0)
113         {
114                 for(int j = 0; j < argc; ++j)
115                 {
116                         string arg = argv(j);
117                         if(arg == "") continue;
118                         _MapInfo_Parse_Settemp(mapname, MAPINFO_SETTEMP_ACL_USER, 0, arg, 0); // no recursion!
119                 }
120         }
121 #endif
122
123 #if 0
124         cvar = cvar_campaignwrapper;
125         cvar_string = cvar_string_campaignwrapper;
126         cvar_set = cvar_set_campaignwrapper;
127         cvar_set("g_campaign", "1");
128         cvar_set("g_dm", "0");
129         cvar_set("skill", ftos(baseskill));
130         cvar_set("bot_number", ftos(campaign_bots[0]));
131 #else
132         // since g_campaign is already set to 1 by the menu when starting a campaign level, we first need
133         // to set it back to 0 before settemping it to 1 so it'll be correctly restored to 0 on disconnection
134         cvar_set("g_campaign", "0");
135         cvar_settemp("g_campaign", "1");
136         cvar_settemp("g_dm", "0");
137         cvar_settemp("skill", ftos(baseskill));
138         cvar_settemp("bot_number", ftos(campaign_bots[0]));
139 #endif
140         MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
141
142         // copy sv_gravity cvar, as the engine needs it too (sorry, this will mess
143         // with the menu a little still...)
144         cvar_set_normal("sv_gravity", ftos(autocvar_sv_gravity));
145
146         if(Campaign_Invalid())
147                 return;
148
149         title = campaign_shortdesc[0];
150         title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
151         campaign_message = strzone(strcat(title, "\n^3\n", campaign_longdesc[0], "\n\n^1press jump to enter the game"));
152         strunzone(title);
153 }
154
155 void CampaignPostInit()
156 {
157         // now some sanity checks
158         if(Campaign_Invalid())
159                 return;
160         if(autocvar__campaign_testrun)
161         {
162                 cvar_set("fraglimit", "0");
163                 cvar_set("leadlimit", "0");
164                 cvar_set("timelimit", "0.01");
165                 cvar_set_normal("fraglimit", "0");
166                 cvar_set_normal("leadlimit", "0");
167                 cvar_set_normal("timelimit", "0.01");
168         }
169         else
170         {
171                 // "default" uses implicit values, "" or "0" means no limit
172                 tokenizebyseparator(campaign_fraglimit[0], "+");
173                 if(argv(0) != "default") {
174                         cvar_set("fraglimit", argv(0));
175                         cvar_set_normal("fraglimit", argv(0));
176                 }
177                 if(argv(1) != "default") {
178                         cvar_set("leadlimit", argv(1));
179                         cvar_set_normal("leadlimit", argv(1));
180                 }
181                 if(campaign_timelimit[0] != "default") {
182                         cvar_set("timelimit", campaign_timelimit[0]);
183                         cvar_set_normal("timelimit", campaign_timelimit[0]);
184                 }
185         }
186 }
187
188 void CampaignSaveCvar(string cvarname, float value)
189 {
190         float fh;
191         float len;
192         string contents;
193         string l;
194
195         registercvar(cvarname, ftos(value));
196         cvar_set_normal(cvarname, ftos(value));
197         // note: cvarname must be remembered
198
199         fh = fopen("campaign.cfg", FILE_READ);
200         contents = "";
201         if(fh >= 0)
202         {
203                 while((l = fgets(fh)))
204                 {
205                         len = tokenize_console(l);
206                         if(len != 3)
207                                 continue;
208                         if(argv(0) != "set")
209                                 continue;
210                         if(argv(1) == cvarname)
211                                 continue;
212                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
213                 }
214                 fclose(fh);
215         }
216         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
217         fh = fopen("campaign.cfg", FILE_WRITE);
218         if(fh >= 0)
219         {
220                 fputs(fh, contents);
221         }
222         else
223         {
224                 error("Cannot write to campaign file");
225         }
226 }
227
228 void CampaignPreIntermission()
229 {
230         int won = 0;
231         int lost = 0;
232         string savevar;
233
234         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
235                 if(it.winning)
236                         won += 1;
237                 else
238                         lost += 1;
239         });
240
241         if(autocvar__campaign_testrun)
242         {
243                 campaign_won = 1;
244                 bprint("Campaign test run, advancing level.\n");
245         }
246         else if(campaign_forcewin)
247         {
248                 campaign_won = 1;
249                 bprint("The current level has been WON.\n");
250         }
251         else if(won == 1 && lost == 0 && checkrules_equality == 0)
252         {
253                 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
254                 {
255                         campaign_won = 0;
256                         bprint("Time's up! The current level has been LOST.\n");
257                         // sound!
258                 }
259                 else
260                 {
261                         campaign_won = 1;
262                         bprint("The current level has been WON.\n");
263                         // sound!
264                 }
265         }
266         else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
267         {
268                 campaign_won = 0;
269                 bprint("Time's up! The current level has been LOST.\n");
270                 // sound!
271         }
272         else
273         {
274                 campaign_won = 0;
275                 bprint("The current level has been LOST.\n");
276                 // sound!
277         }
278
279         if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
280         {
281                 if(campaign_level == cvar_normal(campaign_index_var))
282                 {
283                         if(campaign_entries < 2)
284                         {
285                                 // I have won
286                                 savevar = strcat("g_campaign", campaign_name, "_won");
287                                 CampaignSaveCvar(savevar, 1);
288                                 // advance level (for menu to show it right)
289                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
290                         }
291                         else
292                         {
293                                 // advance level
294                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
295                         }
296                 }
297         }
298 }
299
300 void CampaignPostIntermission()
301 {
302         // NOTE: campaign_won is 0 or 1, that is, points to the next level
303
304         if(campaign_won && campaign_entries < 2)
305         {
306                 // last map won!
307                 LOG_DEBUG("^2test run: campaign looks GOOD");
308                 localcmd("togglemenu 1\n");
309                 CampaignFile_Unload();
310                 return;
311         }
312
313         CampaignSetup(campaign_won);
314         CampaignFile_Unload();
315         strunzone(campaign_message);
316         strunzone(campaign_index_var);
317         strunzone(campaign_name);
318         campaign_name = "";
319 }
320
321
322
323 void CampaignLevelWarp(float n)
324 {
325         if(n < 0)
326                 n = campaign_level + 1;
327         CampaignFile_Unload();
328         CampaignFile_Load(n, 1);
329         if(campaign_entries)
330                 CampaignSetup(0);
331         else
332                 error("Sorry, cheater. You are NOT WELCOME.");
333         CampaignFile_Unload();
334 }
335