3 #include "miscfunctions.qh"
6 #include "../common/campaign_common.qh"
9 // _campaign_index: index of CURRENT map
10 // _campaign_name: name of the current campaign
11 // g_campaign(name)_index: index of current LAST map (saved)
12 // g_campaign_skill: bot skill offset
16 string campaign_index_var;
17 //float checkrules_equality;
19 float CampaignBailout(string s)
22 cvar_string = cvar_string_normal;
23 cvar_set = cvar_set_normal;
24 cvar_set("g_campaign", "0");
25 LOG_INFO("^4campaign initialization failed: ", s, "\n");
26 if(autocvar__campaign_testrun)
27 error("CAMPAIGN FAIL AHAHAHAHAHAHAHAHAH))");
31 string cvar_campaignwrapper_list; // string of format ; var value; var value; var value;
32 string cvar_string_campaignwrapper(string theCvar)
35 p = strstrofs(cvar_campaignwrapper_list, strcat("; ", theCvar, " "), 0);
37 return cvar_defstring(theCvar);
38 p += strlen(theCvar) + 3;
39 q = strstrofs(cvar_campaignwrapper_list, ";", p);
41 return cvar_defstring(theCvar);
42 return substring(cvar_campaignwrapper_list, p, q - p);
44 float cvar_campaignwrapper(string theCvar)
46 return stof(cvar_string_campaignwrapper(theCvar));
48 void cvar_set_campaignwrapper(string theCvar, string theValue)
50 if(cvar_string_campaignwrapper(theCvar) == theValue)
53 s = cvar_campaignwrapper_list;
54 cvar_campaignwrapper_list = strzone(strcat("; ", theCvar, " ", theValue, s));
56 //print(cvar_campaignwrapper_list, "\n");
59 float Campaign_Invalid()
61 string thismapname, wantedmapname;
62 thismapname = GetMapname();
63 wantedmapname = campaign_gametype[0];
64 if(MapInfo_CurrentGametype() != MapInfo_Type_FromString(wantedmapname))
65 return CampaignBailout("wrong game type!");
66 wantedmapname = campaign_mapname[0];
67 if(wantedmapname != thismapname)
68 return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
72 void CampaignPreInit()
76 campaign_level = autocvar__campaign_index;
77 campaign_name = strzone(autocvar__campaign_name);
78 campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
79 CampaignFile_Load(campaign_level, 2);
81 if(campaign_entries < 1)
83 CampaignBailout("unknown map");
87 if(autocvar_sv_cheats)
89 MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
90 CampaignFile_Unload();
91 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
95 baseskill = autocvar_g_campaign_skill;
96 baseskill = baseskill + campaign_botskill[0];
100 cvar_set("sv_public", "0");
101 cvar_set("pausable", "1");
103 cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
105 cvar = cvar_campaignwrapper;
106 cvar_string = cvar_string_campaignwrapper;
107 cvar_set = cvar_set_campaignwrapper;
109 cvar_set("g_campaign", "1");
110 cvar_set("g_dm", "0");
111 cvar_set("skill", ftos(baseskill));
112 cvar_set("bot_number", ftos(campaign_bots[0]));
113 MapInfo_SwitchGameType(MapInfo_Type_FromString(campaign_gametype[0]));
115 // copy sv_gravity cvar, as the engine needs it too (sorry, this will mess
116 // with the menu a little still...)
117 cvar_set_normal("sv_gravity", ftos(autocvar_sv_gravity));
119 if(Campaign_Invalid())
122 title = campaign_shortdesc[0];
123 title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
124 campaign_message = strzone(strcat(title, "\n^3\n", campaign_longdesc[0], "\n\n^1press jump to enter the game"));
128 void CampaignPostInit()
130 // now some sanity checks
131 if(Campaign_Invalid())
133 if(autocvar__campaign_testrun)
135 cvar_set("fraglimit", "0");
136 cvar_set("timelimit", "0.01");
137 cvar_set_normal("fraglimit", "0");
138 cvar_set_normal("timelimit", "0.01");
142 cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
143 cvar_set("timelimit", ftos(campaign_timelimit[0]));
144 cvar_set_normal("fraglimit", ftos(campaign_fraglimit[0]));
145 cvar_set_normal("timelimit", ftos(campaign_timelimit[0]));
149 void CampaignSaveCvar(string cvarname, float value)
156 registercvar(cvarname, ftos(value));
157 cvar_set_normal(cvarname, ftos(value));
158 // note: cvarname must be remembered
160 fh = fopen("campaign.cfg", FILE_READ);
164 while((l = fgets(fh)))
166 len = tokenize_console(l);
171 if(argv(1) == cvarname)
173 contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
177 contents = strcat(contents, "set ", cvarname, " ", ftos(value), "\n");
178 fh = fopen("campaign.cfg", FILE_WRITE);
185 error("Cannot write to campaign file");
189 void CampaignPreIntermission()
195 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it), LAMBDA(
202 if(autocvar__campaign_testrun)
205 bprint("Campaign test run, advancing level.\n");
207 else if(won == 1 && lost == 0 && checkrules_equality == 0)
209 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
212 bprint("Time's up! The current level has been LOST.\n");
218 bprint("The current level has been WON.\n");
222 else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
225 bprint("Time's up! The current level has been LOST.\n");
231 bprint("The current level has been LOST.\n");
235 if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
237 if(campaign_level == cvar_normal(campaign_index_var))
239 if(campaign_entries < 2)
242 savevar = strcat("g_campaign", campaign_name, "_won");
243 CampaignSaveCvar(savevar, 1);
244 // advance level (for menu to show it right)
245 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
250 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
256 void CampaignPostIntermission()
258 // NOTE: campaign_won is 0 or 1, that is, points to the next level
260 if(campaign_won && campaign_entries < 2)
263 LOG_DEBUG("^2test run: campaign looks GOOD");
264 localcmd("togglemenu 1\n");
265 CampaignFile_Unload();
269 CampaignSetup(campaign_won);
270 CampaignFile_Unload();
271 strunzone(campaign_message);
272 strunzone(campaign_index_var);
273 strunzone(campaign_name);
279 void CampaignLevelWarp(float n)
282 n = campaign_level + 1;
283 CampaignFile_Unload();
284 CampaignFile_Load(n, 1);
288 error("Sorry, cheater. You are NOT WELCOME.");
289 CampaignFile_Unload();