336d825a7f58cc2ba4aa99ae1a45d81781293b80
[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 int Campaign_GetLevelNum()
79 {
80         return campaign_level + 1;
81 }
82
83 string Campaign_GetMessage()
84 {
85         return strcat(campaign_shortdesc[0], "\n^3\n", campaign_longdesc[0]);
86 }
87
88 void CampaignPreInit()
89 {
90         float baseskill;
91         campaign_level = autocvar__campaign_index;
92         campaign_name = strzone(autocvar__campaign_name);
93         campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
94         CampaignFile_Load(campaign_level, 2);
95
96         if(campaign_entries < 1)
97         {
98                 CampaignBailout("unknown map");
99                 return;
100         }
101
102         if(autocvar_sv_cheats)
103         {
104                 MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
105                 CampaignFile_Unload();
106                 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
107                 return;
108         }
109
110         baseskill = max(0, autocvar_g_campaign_skill + campaign_botskill[0]);
111         campaign_forcewin = false;
112
113         cvar_set("sv_public", "0");
114         cvar_set("pausable", "1");
115
116 #if 0
117         cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
118 #else
119         string cvar_campaignwrapper_list = strcat("; ", campaign_mutators[0], "; ");
120         int argc = tokenizebyseparator(cvar_campaignwrapper_list, "; ");
121         if(argc > 0)
122         {
123                 for(int j = 0; j < argc; ++j)
124                 {
125                         string arg = argv(j);
126                         if(arg == "") continue;
127                         _MapInfo_Parse_Settemp(mapname, MAPINFO_SETTEMP_ACL_USER, 0, arg, 0); // no recursion!
128                 }
129         }
130 #endif
131
132 #if 0
133         cvar = cvar_campaignwrapper;
134         cvar_string = cvar_string_campaignwrapper;
135         cvar_set = cvar_set_campaignwrapper;
136         cvar_set("g_campaign", "1");
137         cvar_set("g_dm", "0");
138         cvar_set("skill", ftos(baseskill));
139         cvar_set("bot_number", ftos(campaign_bots[0]));
140 #else
141         cvar_settemp("g_campaign", "1");
142         cvar_settemp("g_dm", "0");
143         cvar_settemp("skill", ftos(baseskill));
144         cvar_settemp("bot_number", ftos(campaign_bots[0]));
145 #endif
146         MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
147
148         // copy sv_gravity cvar, as the engine needs it too (sorry, this will mess
149         // with the menu a little still...)
150         cvar_set_normal("sv_gravity", ftos(autocvar_sv_gravity));
151
152         if(Campaign_Invalid())
153                 return;
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("leadlimit", "0");
165                 cvar_set("timelimit", "0.01");
166                 cvar_set_normal("fraglimit", "0");
167                 cvar_set_normal("leadlimit", "0");
168                 cvar_set_normal("timelimit", "0.01");
169         }
170         else
171         {
172                 // "default" uses implicit values, "" or "0" means no limit
173                 tokenizebyseparator(campaign_fraglimit[0], "+");
174                 if(argv(0) != "default") {
175                         cvar_set("fraglimit", argv(0));
176                         cvar_set_normal("fraglimit", argv(0));
177                 }
178                 if(argv(1) != "default") {
179                         cvar_set("leadlimit", argv(1));
180                         cvar_set_normal("leadlimit", argv(1));
181                 }
182                 if(campaign_timelimit[0] != "default") {
183                         cvar_set("timelimit", campaign_timelimit[0]);
184                         cvar_set_normal("timelimit", campaign_timelimit[0]);
185                 }
186         }
187 }
188
189 void CampaignSaveCvar(string cvarname, float value)
190 {
191         float fh;
192         float len;
193         string contents;
194         string l;
195
196         registercvar(cvarname, ftos(value));
197         cvar_set_normal(cvarname, ftos(value));
198         // note: cvarname must be remembered
199
200         fh = fopen("campaign.cfg", FILE_READ);
201         contents = "";
202         if(fh >= 0)
203         {
204                 while((l = fgets(fh)))
205                 {
206                         len = tokenize_console(l);
207                         if(len != 3)
208                                 continue;
209                         if(argv(0) != "set")
210                                 continue;
211                         if(argv(1) == cvarname)
212                                 continue;
213                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
214                 }
215                 fclose(fh);
216         }
217         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
218         fh = fopen("campaign.cfg", FILE_WRITE);
219         if(fh >= 0)
220         {
221                 fputs(fh, contents);
222         }
223         else
224         {
225                 error("Cannot write to campaign file");
226         }
227 }
228
229 void CampaignPreIntermission()
230 {
231         int won = 0;
232         int lost = 0;
233         string savevar;
234
235         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
236                 if(it.winning)
237                         won += 1;
238                 else
239                         lost += 1;
240         });
241
242         if(autocvar__campaign_testrun)
243         {
244                 campaign_won = 1;
245                 bprint("Campaign test run, advancing level.\n");
246         }
247         else if(campaign_forcewin)
248         {
249                 campaign_won = 1;
250                 bprint("The current level has been WON.\n");
251         }
252         else if(won == 1 && lost == 0 && checkrules_equality == 0)
253         {
254                 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
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 = 1;
263                         bprint("The current level has been WON.\n");
264                         // sound!
265                 }
266         }
267         else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
268         {
269                 campaign_won = 0;
270                 bprint("Time's up! The current level has been LOST.\n");
271                 // sound!
272         }
273         else
274         {
275                 campaign_won = 0;
276                 bprint("The current level has been LOST.\n");
277                 // sound!
278         }
279
280         if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
281         {
282                 if(campaign_level == cvar_normal(campaign_index_var))
283                 {
284                         if(campaign_entries < 2)
285                         {
286                                 // I have won
287                                 savevar = strcat("g_campaign", campaign_name, "_won");
288                                 CampaignSaveCvar(savevar, 1);
289                                 // advance level (for menu to show it right)
290                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
291                         }
292                         else
293                         {
294                                 // advance level
295                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
296                         }
297                 }
298         }
299 }
300
301 void CampaignPostIntermission()
302 {
303         // NOTE: campaign_won is 0 or 1, that is, points to the next level
304
305         if(campaign_won && campaign_entries < 2)
306         {
307                 // last map won!
308                 LOG_DEBUG("^2test run: campaign looks GOOD");
309                 localcmd("togglemenu 1\n");
310                 CampaignFile_Unload();
311                 return;
312         }
313
314         CampaignSetup(campaign_won);
315         CampaignFile_Unload();
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