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