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
9 string campaign_index_var;
10 float checkrules_equality;
12 float CampaignBailout(string s)
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))");
24 string cvar_campaignwrapper_list; // string of format ; var value; var value; var value;
25 string cvar_string_campaignwrapper(string theCvar)
28 p = strstrofs(cvar_campaignwrapper_list, strcat("; ", theCvar, " "), 0);
30 return cvar_defstring(theCvar);
31 p += strlen(theCvar) + 3;
32 q = strstrofs(cvar_campaignwrapper_list, ";", p);
34 return cvar_defstring(theCvar);
35 return substring(cvar_campaignwrapper_list, p, q - p);
37 float cvar_campaignwrapper(string theCvar)
39 return stof(cvar_string_campaignwrapper(theCvar));
41 void cvar_set_campaignwrapper(string theCvar, string theValue)
43 if(cvar_string_campaignwrapper(theCvar) == theValue)
46 s = cvar_campaignwrapper_list;
47 cvar_campaignwrapper_list = strzone(strcat("; ", theCvar, " ", theValue, s));
49 //print(cvar_campaignwrapper_list, "\n");
52 float Campaign_Invalid()
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));
65 void CampaignPreInit()
69 if(autocvar_sv_cheats)
71 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
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)
80 CampaignBailout("unknown map");
84 baseskill = autocvar_g_campaign_skill;
85 baseskill = baseskill + campaign_botskill[0];
89 cvar_set("sv_public", "0");
90 cvar_set("pausable", "1");
92 cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
94 cvar = cvar_campaignwrapper;
95 cvar_string = cvar_string_campaignwrapper;
96 cvar_set = cvar_set_campaignwrapper;
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]));
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));
108 if(Campaign_Invalid())
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"));
118 void CampaignPostInit()
120 // now some sanity checks
121 if(Campaign_Invalid())
123 if(autocvar__campaign_testrun)
125 cvar_set("fraglimit", "0");
126 cvar_set("timelimit", "0.01");
127 cvar_set_normal("fraglimit", "0");
128 cvar_set_normal("timelimit", "0.01");
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]));
139 void CampaignSaveCvar(string cvarname, float value)
146 registercvar(cvarname, ftos(value));
147 cvar_set_normal(cvarname, ftos(value));
148 // note: cvarname must be remembered
150 fh = fopen("campaign.cfg", FILE_READ);
154 while((l = fgets(fh)))
156 len = tokenize_console(l);
161 if(argv(1) == cvarname)
163 contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
167 contents = strcat(contents, "set ", cvarname, " ", ftos(value), "\n");
168 fh = fopen("campaign.cfg", FILE_WRITE);
175 error("Cannot write to campaign file");
179 void CampaignPreIntermission()
188 head = findchain(classname, "player");
191 if(clienttype(head) == CLIENTTYPE_REAL)
201 if(autocvar__campaign_testrun)
204 bprint("Campaign test run, advancing level.\n");
206 else if(won == 1 && lost == 0 && checkrules_equality == 0)
208 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
211 bprint("Time's up! The current level has been LOST.\n");
217 bprint("The current level has been WON.\n");
221 else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
224 bprint("Time's up! The current level has been LOST.\n");
230 bprint("The current level has been LOST.\n");
234 if(campaign_won && cheatcount_total == 0 && !autocvar__campaign_testrun)
236 if(campaign_level == cvar_normal(campaign_index_var))
238 if(campaign_entries < 2)
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);
249 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
255 void CampaignPostIntermission()
257 // NOTE: campaign_won is 0 or 1, that is, points to the next level
259 if(campaign_won && campaign_entries < 2)
262 print("^2test run: campaign looks GOOD\n");
263 localcmd("togglemenu 1\n");
264 CampaignFile_Unload();
268 CampaignSetup(campaign_won);
269 CampaignFile_Unload();
270 strunzone(campaign_message);
271 strunzone(campaign_index_var);
272 strunzone(campaign_name);
278 void CampaignLevelWarp(float n)
281 n = campaign_level + 1;
282 CampaignFile_Unload();
283 CampaignFile_Load(n, 1);
287 error("Sorry, cheater. You are NOT WELCOME.");
288 CampaignFile_Unload();