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