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