]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/campaign.qc
Experimental new tokenizer (should now be 1:1 compatible to the console parsing)...
[divverent/nexuiz.git] / data / 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 void CampaignBailout(string s)
13 {
14         cvar_set("g_campaign", "0");
15         print("campaign initialization failed: ", s, "\n");
16         return;
17 }
18
19 void CampaignPreInit()
20 {
21         float baseskill;
22         string title;
23         campaign_level = cvar("_campaign_index");
24         campaign_name = strzone(cvar_string("_campaign_name"));
25         campaign_index_var = strzone(strcat("g_campaign", campaign_name, "_index"));
26         CampaignFile_Load(campaign_level, 2);
27         if(campaign_entries < 1)
28                 return CampaignBailout("unknown map");
29         cvar_set("bot_number", ftos(campaign_bots[0]));
30
31         baseskill = cvar("g_campaign_skill");
32         baseskill = baseskill + campaign_botskill[0];
33         if(baseskill < 0)
34                 baseskill = 0;
35         cvar_set("skill", ftos(baseskill));
36
37         cvar_set("sv_public", "0");
38         cvar_set("pausable", "1");
39
40         title = campaign_shortdesc[0];
41         title = strzone(strcat("Level ", ftos(campaign_level + 1), ": ", title));
42         campaign_message = strzone(strcat("\n\n\n\n\n\n\n\n\n\n^1\n", title, "\n^3\n", wordwrap(campaign_longdesc[0], 50), "\n\n^1press jump to enter the game"));
43         strunzone(title);
44 }
45
46 string GetMapname();
47 void CampaignPostInit()
48 {
49         // now some sanity checks
50         string thismapname, wantedmapname;
51         thismapname = GetMapname();
52         wantedmapname = campaign_gametype[0];
53         if(MapInfo_CurrentGametype() != MapInfo_Type_FromString(wantedmapname))
54                 return CampaignBailout("wrong game type!");
55         wantedmapname = campaign_mapname[0];
56         if(wantedmapname != thismapname)
57                 return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
58         cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
59         cvar_set("timelimit", "0");
60 }
61
62 void CampaignSaveCvar(string cvarname, float value)
63 {
64         float fh;
65         float len;
66         string contents;
67         string l;
68
69         registercvar(cvarname, ftos(value));
70         cvar_set(cvarname, ftos(value));
71         // note: cvarname must be remembered
72
73         fh = fopen("campaign.cfg", FILE_READ);
74         contents = "";
75         if(fh >= 0)
76         {
77                 while((l = fgets(fh)))
78                 {
79                         len = tokenize_sane(l);
80                         if(len != 3)
81                                 continue;
82                         if(argv(0) != "set")
83                                 continue;
84                         if(argv(1) == cvarname)
85                                 continue;
86                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
87                 }
88                 fclose(fh);
89         }
90         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
91         fh = fopen("campaign.cfg", FILE_WRITE);
92         if(fh >= 0)
93         {
94                 fputs(fh, contents);
95         }
96         else
97         {
98                 error("Cannot write to campaign file");
99         }
100 }
101
102 void CampaignPreIntermission()
103 {
104         entity head;
105         float won;
106         float lost;
107         local string savevar;
108
109         won = 0;
110
111         head = findchain(classname, "player");
112         while(head)
113         {
114                 if(clienttype(head) == CLIENTTYPE_REAL)
115                 {
116                         if(head.winning)
117                                 won = won + 1;
118                         else
119                                 lost = lost + 1;
120                 }
121                 head = head.chain;
122         }
123
124         if(won == 1 && lost == 0 && checkrules_equality == 0)
125         {
126                 campaign_won = 1;
127                 bprint("The current level has been WON.\n");
128                 // sound!
129         }
130         else
131         {
132                 campaign_won = 0;
133                 bprint("The current level has been LOST.\n");
134                 // sound!
135         }
136
137         if(campaign_won)
138         {
139                 if(campaign_entries < 2)
140                 {
141                         // I have won
142                         savevar = strcat("g_campaign", campaign_name, "_won");
143                         CampaignSaveCvar(savevar, 1);
144                         // advance level (for menu to show it right)
145                         CampaignSaveCvar(campaign_index_var, campaign_level + 1);
146                 }
147                 else if(campaign_level == cvar(campaign_index_var))
148                 {
149                         // advance level
150                         CampaignSaveCvar(campaign_index_var, campaign_level + 1);
151                 }
152         }
153 }
154
155 void CampaignPostIntermission()
156 {
157         // NOTE: campaign_won is 0 or 1, that is, points to the next level
158
159         if(campaign_won && campaign_entries < 2)
160         {
161                 // last map won!
162                 localcmd("togglemenu 1\n");
163                 CampaignFile_Unload();
164                 return;
165         }
166
167         CampaignSetup(campaign_won);
168         CampaignFile_Unload();
169         strunzone(campaign_message);
170         strunzone(campaign_index_var);
171         strunzone(campaign_name);
172         campaign_name = "";
173 }
174
175
176
177 void CampaignLevelWarp(float n)
178 {
179         if(!sv_cheats)
180                 return;
181         CampaignFile_Unload();
182         CampaignFile_Load(n, 1);
183         if(campaign_entries)
184                 CampaignSetup(0);
185         else
186                 error("Sorry, cheater. You are NOT WELCOME.");
187         CampaignFile_Unload();
188 }