Xonotic
campaign.qc
Go to the documentation of this file.
1 #include "campaign.qh"
2 
4 #include <common/mapinfo.qh>
5 #include <common/stats.qh>
6 #include <common/weapons/_all.qh>
7 #include <server/cheats.qh>
8 #include <server/intermission.qh>
9 #include <server/world.qh>
10 
11 // campaign cvars:
12 // _campaign_index: index of CURRENT map
13 // _campaign_name: name of the current campaign
14 // g_campaign(name)_index: index of current LAST map (saved)
15 // g_campaign_skill: bot skill offset
16 
20 //float checkrules_equality;
21 
22 float CampaignBailout(string s)
23 {
24  cvar_set("g_campaign", "0");
25  LOG_INFO("^4campaign initialization failed: ", s);
27  error("CAMPAIGN FAIL AHAHAHAHAHAHAHAHAH))");
28  return 1;
29 }
30 
32 {
33  string thismapname, wantedmapname;
34  thismapname = GetMapname();
35  wantedmapname = campaign_gametype[0];
36  if(MapInfo_CurrentGametype() != MapInfo_Type_FromString(wantedmapname, false))
37  return CampaignBailout("wrong game type!");
38  wantedmapname = campaign_mapname[0];
39  if(wantedmapname != thismapname)
40  return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
41  return 0;
42 }
43 
45 {
46  return campaign_level + 1;
47 }
48 
50 {
51  return strcat(campaign_shortdesc[0], "\n^3\n", campaign_longdesc[0]);
52 }
53 
55 {
56  float baseskill;
59  campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
61 
62  if(campaign_entries < 1)
63  {
64  CampaignBailout("unknown map");
65  return;
66  }
67 
69  {
72  CampaignBailout("JOLLY CHEATS AHAHAHAHAHAHAH))");
73  return;
74  }
75 
76  baseskill = max(0, autocvar_g_campaign_skill + campaign_botskill[0]);
77  campaign_forcewin = false;
78 
79  cvar_set("sv_public", "0");
80  cvar_set("pausable", "1");
81 
82  string cvar_campaignwrapper_list = strcat("; ", campaign_mutators[0], "; ");
83  int argc = tokenizebyseparator(cvar_campaignwrapper_list, "; ");
84  if(argc > 0)
85  {
86  for(int j = 0; j < argc; ++j)
87  {
88  string arg = argv(j);
89  if(arg == "") continue;
91  }
92  }
93 
94  cvar_settemp("g_campaign", "1");
95  cvar_settemp("g_dm", "0");
96  cvar_settemp("skill", ftos(baseskill));
97  cvar_settemp("bot_number", ftos(campaign_bots[0]));
98 
100 
101  if(Campaign_Invalid())
102  return;
103 }
104 
106 {
107  // now some sanity checks
108  if(Campaign_Invalid())
109  return;
111  {
112  cvar_set("fraglimit", "0");
113  cvar_set("leadlimit", "0");
114  cvar_set("timelimit", "0.01");
115  }
116  else
117  {
118  // "default" uses implicit values, "" or "0" means no limit
120  if(argv(0) != "default") {
121  cvar_set("fraglimit", argv(0));
122  }
123  if(argv(1) != "default") {
124  cvar_set("leadlimit", argv(1));
125  }
126  if(campaign_timelimit[0] != "default") {
127  cvar_set("timelimit", campaign_timelimit[0]);
128  }
129  }
130 }
131 
132 void CampaignSaveCvar(string cvarname, float value)
133 {
134  float fh;
135  float len;
136  string contents;
137  string l;
138 
139  registercvar(cvarname, ftos(value));
140  cvar_set(cvarname, ftos(value));
141  // note: cvarname must be remembered
142 
143  fh = fopen("campaign.cfg", FILE_READ);
144  contents = "";
145  if(fh >= 0)
146  {
147  while((l = fgets(fh)))
148  {
149  len = tokenize_console(l);
150  if(len != 3)
151  continue;
152  if(argv(0) != "set")
153  continue;
154  if(argv(1) == cvarname)
155  continue;
156  contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
157  }
158  fclose(fh);
159  }
160  contents = strcat(contents, "set ", cvarname, " ", ftos(value), "\n");
161  fh = fopen("campaign.cfg", FILE_WRITE);
162  if(fh >= 0)
163  {
164  fputs(fh, contents);
165  }
166  else
167  {
168  error("Cannot write to campaign file");
169  }
170 }
171 
173 {
174  int won = 0;
175  int lost = 0;
176  string savevar;
177 
179  if(it.winning)
180  won += 1;
181  else
182  lost += 1;
183  });
184 
186  {
187  campaign_won = 1;
188  bprint("Campaign test run, advancing level.\n");
189  }
190  else if(campaign_forcewin)
191  {
192  campaign_won = 1;
193  bprint("The current level has been WON.\n");
194  }
195  else if(won == 1 && lost == 0 && checkrules_equality == 0)
196  {
197  if(autocvar_timelimit != 0 && autocvar_fraglimit != 0 && time > autocvar_timelimit * 60) // checks if the timelimit has expired.
198  {
199  campaign_won = 0;
200  bprint("Time's up! The current level has been LOST.\n");
201  // sound!
202  }
203  else
204  {
205  campaign_won = 1;
206  bprint("The current level has been WON.\n");
207  // sound!
208  }
209  }
210  else if(autocvar_timelimit != 0 && time > autocvar_timelimit * 60)
211  {
212  campaign_won = 0;
213  bprint("Time's up! The current level has been LOST.\n");
214  // sound!
215  }
216  else
217  {
218  campaign_won = 0;
219  bprint("The current level has been LOST.\n");
220  // sound!
221  }
222 
224  {
226  {
227  if(campaign_entries < 2)
228  {
229  // I have won
230  savevar = strcat("g_campaign", campaign_name, "_won");
231  CampaignSaveCvar(savevar, 1);
232  // advance level (for menu to show it right)
234  }
235  else
236  {
237  // advance level
239  }
240  }
241  }
242 }
243 
245 {
246  // NOTE: campaign_won is 0 or 1, that is, points to the next level
247 
248  if(campaign_won && campaign_entries < 2)
249  {
250  // last map won!
251  LOG_DEBUG("^2test run: campaign looks GOOD");
252  localcmd("togglemenu 1\n");
254  return;
255  }
256 
261  campaign_name = "";
262 }
263 
264 
265 
266 void CampaignLevelWarp(float n)
267 {
268  if(n < 0)
269  n = campaign_level + 1;
271  CampaignFile_Load(n, 1);
272  if(campaign_entries)
273  CampaignSetup(0);
274  else
275  error("Sorry, cheater. You are NOT WELCOME.");
277 }
278 
float CampaignBailout(string s)
Definition: campaign.qc:22
float Campaign_Invalid()
Definition: campaign.qc:31
void CampaignPreInit()
Definition: campaign.qc:54
string campaign_name
float campaign_won
Definition: campaign.qc:18
string campaign_shortdesc[CAMPAIGN_MAX_ENTRIES]
string campaign_index_var
Definition: campaign.qc:19
float cvar_settemp(string tmp_cvar, string tmp_value)
Definition: util.qc:696
string GetMapname()
Definition: intermission.qc:18
void CampaignSetup(int n)
#define FOREACH_CLIENT(cond, body)
Definition: utils.qh:49
const float FILE_READ
Definition: csprogsdefs.qc:231
string campaign_timelimit[CAMPAIGN_MAX_ENTRIES]
float campaign_botskill[CAMPAIGN_MAX_ENTRIES]
#define IS_REAL_CLIENT(v)
Definition: utils.qh:17
int Campaign_GetLevelNum()
Definition: campaign.qc:44
string mapname
Definition: csprogsdefs.qc:26
Gametype MapInfo_Type_FromString(string gtype, bool dowarn)
Definition: mapinfo.qc:589
Gametype MapInfo_CurrentGametype()
Definition: mapinfo.qc:1150
bool autocvar__campaign_testrun
Definition: campaign.qh:3
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
void CampaignPostIntermission()
Definition: campaign.qc:244
int autocvar_g_campaign_skill
Definition: campaign.qh:8
float campaign_bots[CAMPAIGN_MAX_ENTRIES]
float cheatcount_total
Definition: cheats.qh:10
string campaign_mutators[CAMPAIGN_MAX_ENTRIES]
#define LOG_INFO(...)
Definition: log.qh:70
void CampaignPostInit()
Definition: campaign.qc:105
int autocvar_sv_cheats
Definition: cheats.qh:5
int campaign_entries
#define tokenize_console
Definition: dpextensions.qh:24
float campaign_level
Definition: campaign.qc:17
const float FILE_WRITE
Definition: csprogsdefs.qc:233
int autocvar__campaign_index
Definition: campaign.qh:4
string campaign_gametype[CAMPAIGN_MAX_ENTRIES]
string campaign_longdesc[CAMPAIGN_MAX_ENTRIES]
void CampaignSaveCvar(string cvarname, float value)
Definition: campaign.qc:132
void CampaignLevelWarp(float n)
Definition: campaign.qc:266
#define tokenizebyseparator
Definition: dpextensions.qh:21
void CampaignPreIntermission()
Definition: campaign.qc:172
bool campaign_forcewin
Definition: campaign.qh:29
float checkrules_equality
Definition: world.qh:31
#define MAPINFO_SETTEMP_ACL_USER
Definition: mapinfo.qh:205
string campaign_mapname[CAMPAIGN_MAX_ENTRIES]
float time
Definition: csprogsdefs.qc:16
float CampaignFile_Load(float offset, float entries)
string Campaign_GetMessage()
Definition: campaign.qc:49
void CampaignFile_Unload()
void MapInfo_SwitchGameType(Gametype t)
Definition: mapinfo.qc:1178
void _MapInfo_Parse_Settemp(string pFilename, string acl, float type, string s, float recurse)
Definition: mapinfo.qc:627
string campaign_fraglimit[CAMPAIGN_MAX_ENTRIES]
#define IS_PLAYER(v)
Definition: utils.qh:9
#define LOG_DEBUG(...)
Definition: log.qh:85
string autocvar__campaign_name
Definition: campaign.qh:5