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