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("campaign initialization failed: ", s, "\n");
22 string cvar_campaignwrapper_list; // string of format ; var value; var value; var value;
23 string cvar_string_campaignwrapper(string theCvar)
26 p = strstrofs(cvar_campaignwrapper_list, strcat("; ", theCvar, " "), 0);
28 return cvar_defstring(theCvar);
29 p += strlen(theCvar) + 3;
30 q = strstrofs(cvar_campaignwrapper_list, ";", p);
32 return cvar_defstring(theCvar);
33 return substring(cvar_campaignwrapper_list, p, q - p);
35 float cvar_campaignwrapper(string theCvar)
37 return stof(cvar_string_campaignwrapper(theCvar));
39 void cvar_set_campaignwrapper(string theCvar, string theValue)
41 if(cvar_string_campaignwrapper(theCvar) == theValue)
44 s = cvar_campaignwrapper_list;
45 cvar_campaignwrapper_list = strzone(strcat("; ", theCvar, " ", theValue, s));
47 //print(cvar_campaignwrapper_list, "\n");
50 float Campaign_Invalid()
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));
63 void CampaignPreInit()
67 if(autocvar_sv_cheats)
69 CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
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)
78 CampaignBailout("unknown map");
82 baseskill = autocvar_g_campaign_skill;
83 baseskill = baseskill + campaign_botskill[0];
87 cvar_set("sv_public", "0");
88 cvar_set("pausable", "1");
90 cvar_campaignwrapper_list = strzone(strcat("; ", campaign_mutators[0], "; "));
92 cvar = cvar_campaignwrapper;
93 cvar_string = cvar_string_campaignwrapper;
94 cvar_set = cvar_set_campaignwrapper;
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]));
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));
106 if(Campaign_Invalid())
109 title = campaign_shortdesc[0];
110 title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
111 campaign_message = strzone(strcat("\n\n\n\n\n\n\n\n\n\n^1\n", title, "\n^3\n", campaign_longdesc[0], "\n\n^1press jump to enter the game"));
116 void CampaignPostInit()
118 // now some sanity checks
119 if(Campaign_Invalid())
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]));
127 void CampaignSaveCvar(string cvarname, float value)
134 registercvar(cvarname, ftos(value));
135 cvar_set_normal(cvarname, ftos(value));
136 // note: cvarname must be remembered
138 fh = fopen("campaign.cfg", FILE_READ);
142 while((l = fgets(fh)))
144 len = tokenize_console(l);
149 if(argv(1) == cvarname)
151 contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
155 contents = strcat(contents, "set ", cvarname, " ", ftos(value), "\n");
156 fh = fopen("campaign.cfg", FILE_WRITE);
163 error("Cannot write to campaign file");
167 void CampaignPreIntermission()
172 local string savevar;
176 head = findchain(classname, "player");
179 if(clienttype(head) == CLIENTTYPE_REAL)
189 if(won == 1 && lost == 0 && checkrules_equality == 0 && cheatcount_total == 0)
191 if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
194 bprint("Time's up! The current level has been LOST.\n");
200 bprint("The current level has been WON.\n");
204 else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
207 bprint("Time's up! The current level has been LOST.\n");
213 bprint("The current level has been LOST.\n");
219 if(campaign_entries < 2)
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);
227 else if(campaign_level == cvar_normal(campaign_index_var))
230 CampaignSaveCvar(campaign_index_var, campaign_level + 1);
235 void CampaignPostIntermission()
237 // NOTE: campaign_won is 0 or 1, that is, points to the next level
239 if(campaign_won && campaign_entries < 2)
242 localcmd("togglemenu 1\n");
243 CampaignFile_Unload();
247 CampaignSetup(campaign_won);
248 CampaignFile_Unload();
249 strunzone(campaign_message);
250 strunzone(campaign_index_var);
251 strunzone(campaign_name);
257 void CampaignLevelWarp(float n)
259 CampaignFile_Unload();
260 CampaignFile_Load(n, 1);
264 error("Sorry, cheater. You are NOT WELCOME.");
265 CampaignFile_Unload();