Disable the cvar wrapper system in the campaigns, since it doesn't affect a majority...
[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 = autocvar_g_campaign_skill;
102         baseskill = baseskill + campaign_botskill[0];
103         if(baseskill < 0)
104                 baseskill = 0;
105
106         campaign_forcewin = false;
107
108         cvar_set("sv_public", "0");
109         cvar_set("pausable", "1");
110
111 #if 0
112         cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
113 #else
114         string cvar_campaignwrapper_list = strcat("; ", campaign_mutators[0], "; ");
115         int argc = tokenizebyseparator(cvar_campaignwrapper_list, "; ");
116         if(argc > 0)
117         {
118                 for(int j = 0; j < argc; ++j)
119                 {
120                         string arg = argv(j);
121                         if(arg == "") continue;
122                         _MapInfo_Parse_Settemp(mapname, MAPINFO_SETTEMP_ACL_USER, 0, arg, 0); // no recursion!
123                 }
124         }
125 #endif
126
127 #if 0
128         cvar = cvar_campaignwrapper;
129         cvar_string = cvar_string_campaignwrapper;
130         cvar_set = cvar_set_campaignwrapper;
131         cvar_set("g_campaign", "1");
132         cvar_set("g_dm", "0");
133         cvar_set("skill", ftos(baseskill));
134         cvar_set("bot_number", ftos(campaign_bots[0]));
135 #else
136         cvar_settemp("g_campaign", "1");
137         cvar_settemp("g_dm", "0");
138         cvar_settemp("skill", ftos(baseskill));
139         cvar_settemp("bot_number", ftos(campaign_bots[0]));
140 #endif
141         MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
142
143         // copy sv_gravity cvar, as the engine needs it too (sorry, this will mess
144         // with the menu a little still...)
145         cvar_set_normal("sv_gravity", ftos(autocvar_sv_gravity));
146
147         if(Campaign_Invalid())
148                 return;
149
150         title = campaign_shortdesc[0];
151         title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
152         campaign_message = strzone(strcat(title, "\n^3\n", campaign_longdesc[0], "\n\n^1press jump to enter the game"));
153         strunzone(title);
154 }
155
156 void CampaignPostInit()
157 {
158         // now some sanity checks
159         if(Campaign_Invalid())
160                 return;
161         if(autocvar__campaign_testrun)
162         {
163                 cvar_set("fraglimit", "0");
164                 cvar_set("timelimit", "0.01");
165                 cvar_set_normal("fraglimit", "0");
166                 cvar_set_normal("timelimit", "0.01");
167         }
168         else
169         {
170                 cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
171                 cvar_set("timelimit", ftos(campaign_timelimit[0]));
172                 cvar_set_normal("fraglimit", ftos(campaign_fraglimit[0]));
173                 cvar_set_normal("timelimit", ftos(campaign_timelimit[0]));
174         }
175 }
176
177 void CampaignSaveCvar(string cvarname, float value)
178 {
179         float fh;
180         float len;
181         string contents;
182         string l;
183
184         registercvar(cvarname, ftos(value));
185         cvar_set_normal(cvarname, ftos(value));
186         // note: cvarname must be remembered
187
188         fh = fopen("campaign.cfg", FILE_READ);
189         contents = "";
190         if(fh >= 0)
191         {
192                 while((l = fgets(fh)))
193                 {
194                         len = tokenize_console(l);
195                         if(len != 3)
196                                 continue;
197                         if(argv(0) != "set")
198                                 continue;
199                         if(argv(1) == cvarname)
200                                 continue;
201                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
202                 }
203                 fclose(fh);
204         }
205         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
206         fh = fopen("campaign.cfg", FILE_WRITE);
207         if(fh >= 0)
208         {
209                 fputs(fh, contents);
210         }
211         else
212         {
213                 error("Cannot write to campaign file");
214         }
215 }
216
217 void CampaignPreIntermission()
218 {
219         int won = 0;
220         int lost = 0;
221         string savevar;
222
223         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
224                 if(it.winning)
225                         won += 1;
226                 else
227                         lost += 1;
228         });
229
230         if(autocvar__campaign_testrun)
231         {
232                 campaign_won = 1;
233                 bprint("Campaign test run, advancing level.\n");
234         }
235         else if(campaign_forcewin)
236         {
237                 campaign_won = 1;
238                 bprint("The current level has been WON.\n");
239         }
240         else if(won == 1 && lost == 0 && checkrules_equality == 0)
241         {
242                 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
243                 {
244                         campaign_won = 0;
245                         bprint("Time's up! The current level has been LOST.\n");
246                         // sound!
247                 }
248                 else
249                 {
250                         campaign_won = 1;
251                         bprint("The current level has been WON.\n");
252                         // sound!
253                 }
254         }
255         else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
256         {
257                 campaign_won = 0;
258                 bprint("Time's up! The current level has been LOST.\n");
259                 // sound!
260         }
261         else
262         {
263                 campaign_won = 0;
264                 bprint("The current level has been LOST.\n");
265                 // sound!
266         }
267
268         if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
269         {
270                 if(campaign_level == cvar_normal(campaign_index_var))
271                 {
272                         if(campaign_entries < 2)
273                         {
274                                 // I have won
275                                 savevar = strcat("g_campaign", campaign_name, "_won");
276                                 CampaignSaveCvar(savevar, 1);
277                                 // advance level (for menu to show it right)
278                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
279                         }
280                         else
281                         {
282                                 // advance level
283                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
284                         }
285                 }
286         }
287 }
288
289 void CampaignPostIntermission()
290 {
291         // NOTE: campaign_won is 0 or 1, that is, points to the next level
292
293         if(campaign_won && campaign_entries < 2)
294         {
295                 // last map won!
296                 LOG_DEBUG("^2test run: campaign looks GOOD");
297                 localcmd("togglemenu 1\n");
298                 CampaignFile_Unload();
299                 return;
300         }
301
302         CampaignSetup(campaign_won);
303         CampaignFile_Unload();
304         strunzone(campaign_message);
305         strunzone(campaign_index_var);
306         strunzone(campaign_name);
307         campaign_name = "";
308 }
309
310
311
312 void CampaignLevelWarp(float n)
313 {
314         if(n < 0)
315                 n = campaign_level + 1;
316         CampaignFile_Unload();
317         CampaignFile_Load(n, 1);
318         if(campaign_entries)
319                 CampaignSetup(0);
320         else
321                 error("Sorry, cheater. You are NOT WELCOME.");
322         CampaignFile_Unload();
323 }
324