Make sure g_campaign is always reset to 0 on disconnection (without relying on the...
[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("timelimit", "0.01");
164                 cvar_set_normal("fraglimit", "0");
165                 cvar_set_normal("timelimit", "0.01");
166         }
167         else
168         {
169                 cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
170                 cvar_set("timelimit", ftos(campaign_timelimit[0]));
171                 cvar_set_normal("fraglimit", ftos(campaign_fraglimit[0]));
172                 cvar_set_normal("timelimit", ftos(campaign_timelimit[0]));
173         }
174 }
175
176 void CampaignSaveCvar(string cvarname, float value)
177 {
178         float fh;
179         float len;
180         string contents;
181         string l;
182
183         registercvar(cvarname, ftos(value));
184         cvar_set_normal(cvarname, ftos(value));
185         // note: cvarname must be remembered
186
187         fh = fopen("campaign.cfg", FILE_READ);
188         contents = "";
189         if(fh >= 0)
190         {
191                 while((l = fgets(fh)))
192                 {
193                         len = tokenize_console(l);
194                         if(len != 3)
195                                 continue;
196                         if(argv(0) != "set")
197                                 continue;
198                         if(argv(1) == cvarname)
199                                 continue;
200                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
201                 }
202                 fclose(fh);
203         }
204         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
205         fh = fopen("campaign.cfg", FILE_WRITE);
206         if(fh >= 0)
207         {
208                 fputs(fh, contents);
209         }
210         else
211         {
212                 error("Cannot write to campaign file");
213         }
214 }
215
216 void CampaignPreIntermission()
217 {
218         int won = 0;
219         int lost = 0;
220         string savevar;
221
222         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
223                 if(it.winning)
224                         won += 1;
225                 else
226                         lost += 1;
227         });
228
229         if(autocvar__campaign_testrun)
230         {
231                 campaign_won = 1;
232                 bprint("Campaign test run, advancing level.\n");
233         }
234         else if(campaign_forcewin)
235         {
236                 campaign_won = 1;
237                 bprint("The current level has been WON.\n");
238         }
239         else if(won == 1 && lost == 0 && checkrules_equality == 0)
240         {
241                 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
242                 {
243                         campaign_won = 0;
244                         bprint("Time's up! The current level has been LOST.\n");
245                         // sound!
246                 }
247                 else
248                 {
249                         campaign_won = 1;
250                         bprint("The current level has been WON.\n");
251                         // sound!
252                 }
253         }
254         else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
255         {
256                 campaign_won = 0;
257                 bprint("Time's up! The current level has been LOST.\n");
258                 // sound!
259         }
260         else
261         {
262                 campaign_won = 0;
263                 bprint("The current level has been LOST.\n");
264                 // sound!
265         }
266
267         if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
268         {
269                 if(campaign_level == cvar_normal(campaign_index_var))
270                 {
271                         if(campaign_entries < 2)
272                         {
273                                 // I have won
274                                 savevar = strcat("g_campaign", campaign_name, "_won");
275                                 CampaignSaveCvar(savevar, 1);
276                                 // advance level (for menu to show it right)
277                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
278                         }
279                         else
280                         {
281                                 // advance level
282                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
283                         }
284                 }
285         }
286 }
287
288 void CampaignPostIntermission()
289 {
290         // NOTE: campaign_won is 0 or 1, that is, points to the next level
291
292         if(campaign_won && campaign_entries < 2)
293         {
294                 // last map won!
295                 LOG_DEBUG("^2test run: campaign looks GOOD");
296                 localcmd("togglemenu 1\n");
297                 CampaignFile_Unload();
298                 return;
299         }
300
301         CampaignSetup(campaign_won);
302         CampaignFile_Unload();
303         strunzone(campaign_message);
304         strunzone(campaign_index_var);
305         strunzone(campaign_name);
306         campaign_name = "";
307 }
308
309
310
311 void CampaignLevelWarp(float n)
312 {
313         if(n < 0)
314                 n = campaign_level + 1;
315         CampaignFile_Unload();
316         CampaignFile_Load(n, 1);
317         if(campaign_entries)
318                 CampaignSetup(0);
319         else
320                 error("Sorry, cheater. You are NOT WELCOME.");
321         CampaignFile_Unload();
322 }
323