Merge branch 'master' into Mario/stats_eloranking
[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         cvar = cvar_normal;
25         cvar_string = cvar_string_normal;
26         cvar_set = cvar_set_normal;
27         cvar_set("g_campaign", "0");
28         LOG_INFO("^4campaign initialization failed: ", s);
29         if(autocvar__campaign_testrun)
30                 error("CAMPAIGN FAIL AHAHAHAHAHAHAHAHAH))");
31         return 1;
32 }
33
34 string cvar_campaignwrapper_list; // string of format ; var value; var value; var value;
35 string cvar_string_campaignwrapper(string theCvar)
36 {
37         float p, q;
38         p = strstrofs(cvar_campaignwrapper_list, strcat("; ", theCvar, " "), 0);
39         if(p < 0)
40                 return cvar_defstring(theCvar);
41         p += strlen(theCvar) + 3;
42         q = strstrofs(cvar_campaignwrapper_list, ";", p);
43         if(q < 0)
44                 return cvar_defstring(theCvar);
45         return substring(cvar_campaignwrapper_list, p, q - p);
46 }
47 float cvar_campaignwrapper(string theCvar)
48 {
49         return stof(cvar_string_campaignwrapper(theCvar));
50 }
51 void cvar_set_campaignwrapper(string theCvar, string theValue)
52 {
53         if(cvar_string_campaignwrapper(theCvar) == theValue)
54                 return;
55         string s = cvar_campaignwrapper_list;
56         cvar_campaignwrapper_list = strzone(strcat("; ", theCvar, " ", theValue, s));
57         strunzone(s);
58         //print(cvar_campaignwrapper_list, "\n");
59 }
60
61 float Campaign_Invalid()
62 {
63         string thismapname, wantedmapname;
64         thismapname = GetMapname();
65         wantedmapname = campaign_gametype[0];
66         if(MapInfo_CurrentGametype() != MapInfo_Type_FromString(wantedmapname))
67                 return CampaignBailout("wrong game type!");
68         wantedmapname = campaign_mapname[0];
69         if(wantedmapname != thismapname)
70                 return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
71         return 0;
72 }
73
74 void CampaignPreInit()
75 {
76         float baseskill;
77         string title;
78         campaign_level = autocvar__campaign_index;
79         campaign_name = strzone(autocvar__campaign_name);
80         campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
81         CampaignFile_Load(campaign_level, 2);
82
83         if(campaign_entries < 1)
84         {
85                 CampaignBailout("unknown map");
86                 return;
87         }
88
89         if(autocvar_sv_cheats)
90         {
91                 MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
92                 CampaignFile_Unload();
93                 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
94                 return;
95         }
96
97         baseskill = autocvar_g_campaign_skill;
98         baseskill = baseskill + campaign_botskill[0];
99         if(baseskill < 0)
100                 baseskill = 0;
101
102         campaign_forcewin = false;
103
104         cvar_set("sv_public", "0");
105         cvar_set("pausable", "1");
106
107         cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
108
109         cvar = cvar_campaignwrapper;
110         cvar_string = cvar_string_campaignwrapper;
111         cvar_set = cvar_set_campaignwrapper;
112
113         cvar_set("g_campaign", "1");
114         cvar_set("g_dm", "0");
115         cvar_set("skill", ftos(baseskill));
116         cvar_set("bot_number", ftos(campaign_bots[0]));
117         MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
118
119         // copy sv_gravity cvar, as the engine needs it too (sorry, this will mess
120         // with the menu a little still...)
121         cvar_set_normal("sv_gravity", ftos(autocvar_sv_gravity));
122
123         if(Campaign_Invalid())
124                 return;
125
126         title = campaign_shortdesc[0];
127         title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
128         campaign_message = strzone(strcat(title, "\n^3\n", campaign_longdesc[0], "\n\n^1press jump to enter the game"));
129         strunzone(title);
130 }
131
132 void CampaignPostInit()
133 {
134         // now some sanity checks
135         if(Campaign_Invalid())
136                 return;
137         if(autocvar__campaign_testrun)
138         {
139                 cvar_set("fraglimit", "0");
140                 cvar_set("timelimit", "0.01");
141                 cvar_set_normal("fraglimit", "0");
142                 cvar_set_normal("timelimit", "0.01");
143         }
144         else
145         {
146                 cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
147                 cvar_set("timelimit", ftos(campaign_timelimit[0]));
148                 cvar_set_normal("fraglimit", ftos(campaign_fraglimit[0]));
149                 cvar_set_normal("timelimit", ftos(campaign_timelimit[0]));
150         }
151 }
152
153 void CampaignSaveCvar(string cvarname, float value)
154 {
155         float fh;
156         float len;
157         string contents;
158         string l;
159
160         registercvar(cvarname, ftos(value));
161         cvar_set_normal(cvarname, ftos(value));
162         // note: cvarname must be remembered
163
164         fh = fopen("campaign.cfg", FILE_READ);
165         contents = "";
166         if(fh >= 0)
167         {
168                 while((l = fgets(fh)))
169                 {
170                         len = tokenize_console(l);
171                         if(len != 3)
172                                 continue;
173                         if(argv(0) != "set")
174                                 continue;
175                         if(argv(1) == cvarname)
176                                 continue;
177                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
178                 }
179                 fclose(fh);
180         }
181         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
182         fh = fopen("campaign.cfg", FILE_WRITE);
183         if(fh >= 0)
184         {
185                 fputs(fh, contents);
186         }
187         else
188         {
189                 error("Cannot write to campaign file");
190         }
191 }
192
193 void CampaignPreIntermission()
194 {
195         int won = 0;
196         int lost = 0;
197         string savevar;
198
199         FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), {
200                 if(it.winning)
201                         won += 1;
202                 else
203                         lost += 1;
204         });
205
206         if(autocvar__campaign_testrun)
207         {
208                 campaign_won = 1;
209                 bprint("Campaign test run, advancing level.\n");
210         }
211         else if(campaign_forcewin)
212         {
213                 campaign_won = 1;
214                 bprint("The current level has been WON.\n");
215         }
216         else if(won == 1 && lost == 0 && checkrules_equality == 0)
217         {
218                 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
219                 {
220                         campaign_won = 0;
221                         bprint("Time's up! The current level has been LOST.\n");
222                         // sound!
223                 }
224                 else
225                 {
226                         campaign_won = 1;
227                         bprint("The current level has been WON.\n");
228                         // sound!
229                 }
230         }
231         else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
232         {
233                 campaign_won = 0;
234                 bprint("Time's up! The current level has been LOST.\n");
235                 // sound!
236         }
237         else
238         {
239                 campaign_won = 0;
240                 bprint("The current level has been LOST.\n");
241                 // sound!
242         }
243
244         if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
245         {
246                 if(campaign_level == cvar_normal(campaign_index_var))
247                 {
248                         if(campaign_entries < 2)
249                         {
250                                 // I have won
251                                 savevar = strcat("g_campaign", campaign_name, "_won");
252                                 CampaignSaveCvar(savevar, 1);
253                                 // advance level (for menu to show it right)
254                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
255                         }
256                         else
257                         {
258                                 // advance level
259                                 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
260                         }
261                 }
262         }
263 }
264
265 void CampaignPostIntermission()
266 {
267         // NOTE: campaign_won is 0 or 1, that is, points to the next level
268
269         if(campaign_won && campaign_entries < 2)
270         {
271                 // last map won!
272                 LOG_DEBUG("^2test run: campaign looks GOOD");
273                 localcmd("togglemenu 1\n");
274                 CampaignFile_Unload();
275                 return;
276         }
277
278         CampaignSetup(campaign_won);
279         CampaignFile_Unload();
280         strunzone(campaign_message);
281         strunzone(campaign_index_var);
282         strunzone(campaign_name);
283         campaign_name = "";
284 }
285
286
287
288 void CampaignLevelWarp(float n)
289 {
290         if(n < 0)
291                 n = campaign_level + 1;
292         CampaignFile_Unload();
293         CampaignFile_Load(n, 1);
294         if(campaign_entries)
295                 CampaignSetup(0);
296         else
297                 error("Sorry, cheater. You are NOT WELCOME.");
298         CampaignFile_Unload();
299 }
300