]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/campaign.qc
campaign settings now stored in data/campaign.cfg so they get preserved
[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(string s) CampaignBailout =
13 {
14         cvar_set("g_campaign", "0");
15         ServerConsoleEcho(strcat("campaign initialization failed: ", s), TRUE);
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         wantedmapname = strcat(wantedmapname, "_", campaign_mapname[0]);
54         if(wantedmapname != thismapname)
55                 return CampaignBailout(strcat("wrong map: ", wantedmapname, " != ", thismapname));
56         cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
57         cvar_set("timelimit", "0");
58 }
59
60 void CampaignSaveCvar(string cvarname, float value)
61 {
62         float fh;
63         float len;
64         string contents;
65         string l;
66
67         cvar_set(cvarname, ftos(value));
68         // note: cvarname must be remembered
69
70         fh = fopen("campaign.cfg", FILE_READ);
71         contents = "";
72         if(fh >= 0)
73         {
74                 while((l = fgets(fh)))
75                 {
76                         cvarname = strcat(cvarname);
77                         contents = strcat(contents);
78                         len = tokenize(l);
79                         if(len != 3)
80                                 continue;
81                         if(argv(0) != "set")
82                                 continue;
83                         if(argv(1) == cvarname)
84                                 continue;
85                         contents = strcat(contents, "set ", argv(1), " ", argv(2), "\n");
86                 }
87                 fclose(fh);
88         }
89         contents = strcat(contents, "set ", cvarname,  " ", ftos(value), "\n");
90         fh = fopen("campaign.cfg", FILE_WRITE);
91         if(fh >= 0)
92         {
93                 fputs(fh, contents);
94         }
95         else
96         {
97                 error("Cannot write to campaign file");
98         }
99 }
100
101 void() CampaignPreIntermission =
102 {
103         entity head;
104         float won;
105         float lost;
106         local string savevar;
107
108         won = 0;
109
110         head = findchain(classname, "player");
111         while(head)
112         {
113                 if(clienttype(head) == CLIENTTYPE_REAL)
114                 {
115                         if(head.winning)
116                                 won = won + 1;
117                         else
118                                 lost = lost + 1;
119                 }
120                 head = head.chain;
121         }
122
123         if(won == 1 && lost == 0 && checkrules_equality == 0)
124         {
125                 campaign_won = 1;
126                 bprint("The current level has been WON.\n");
127                 // sound!
128         }
129         else
130         {
131                 campaign_won = 0;
132                 bprint("The current level has been LOST.\n");
133                 // sound!
134         }
135
136         if(campaign_won)
137         {
138                 if(campaign_entries < 2)
139                 {
140                         // I have won
141                         savevar = strcat("g_campaign", campaign_name, "_won");
142                         CampaignSaveCvar(savevar, 1);
143                 }
144                 else if(campaign_level == cvar(campaign_index_var))
145                 {
146                         // advance level
147                         CampaignSaveCvar(campaign_index_var, campaign_level + 1);
148                 }
149         }
150 }
151
152 void() CampaignPostIntermission =
153 {
154         // NOTE: campaign_won is 0 or 1, that is, points to the next level
155
156         if(campaign_won && campaign_entries < 2)
157         {
158                 // last map won!
159                 localcmd("togglemenu\n");
160                 CampaignFile_Unload();
161                 return;
162         }
163
164         CampaignSetup(campaign_won);
165         CampaignFile_Unload();
166         strunzone(campaign_message);
167         strunzone(campaign_index_var);
168         strunzone(campaign_name);
169         campaign_name = "";
170 }
171
172
173
174 void(float n) CampaignLevelWarp =
175 {
176         if(!cvar("sv_cheats"))
177                 return;
178         CampaignFile_Unload();
179         CampaignFile_Load(n, 1);
180         if(campaign_entries)
181                 CampaignSetup(0);
182         else
183                 error("Sorry, cheater. You are NOT WELCOME.");
184         CampaignFile_Unload();
185 }