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