]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/gamec/campaign.c
Improved handling of winning the last level.
[divverent/nexuiz.git] / data / qcsrc / server / gamec / campaign.c
1 // campaign cvars:
2 //   _campaign_index: index of CURRENT map
3 //   g_campaign_index: index of current LAST map (saved)
4 //   g_campaign_skill: bot skill offset
5
6 float campaign_level;
7 float campaign_won;
8
9 void(string s) CampaignBailout =
10 {
11         //bprint(strcat("campaign initialization failed: ", s, "\n"));
12         cvar_set("g_campaign", "0");
13         error(strcat("campaign initialization failed: ", s, "\n"));
14         return;
15 }
16
17 string fstrunzone(string s)
18 {
19         string t;
20         t = strcat(s);
21         strunzone(s);
22         return t;
23 }
24
25 // NOTE: s may not be a tempstring here
26 string Campaign_wordwrap(string s, float l)
27 {
28         string t;
29         string word;
30
31         float lleft;
32         float i;
33
34         float startidx;
35
36         startidx = 0;
37
38         t = strzone("");
39
40         lleft = l;
41         for(i = 0; i <= strlen(s); ++i)
42         {
43                 if(i != strlen(s) && substring(s, i, 1) != " ")
44                         continue;
45
46                 word = substring(s, startidx, i - startidx);
47                 startidx = i + 1;
48                 
49                 if(word == "+++")
50                 {
51                         t = fstrunzone(t);
52                         t = strzone(strcat(t, "\n"));
53                         lleft = l;
54                 }
55                 else if(strlen(word) < lleft)
56                 {
57                         t = fstrunzone(t);
58                         if(lleft != l)
59                         {
60                                 t = strcat(t, " ");
61                                 lleft = lleft - 1;
62                         }
63                         t = strzone(strcat(t, word));
64                         lleft = lleft - strlen(word);
65                 }
66                 else
67                 {
68                         t = fstrunzone(t);
69                         t = strzone(strcat(t, "\n", word));
70                         lleft = l - strlen(word);
71                 }
72         }
73         t = fstrunzone(t);
74         return t;
75 }
76
77 void() CampaignPreInit =
78 {
79         float baseskill;
80         string title;
81         campaign_level = cvar("_campaign_index");
82         CampaignFile_Load(campaign_level, 2);
83         if(campaign_entries < 1)
84                 return CampaignBailout("unknown map");
85         cvar_set("bot_number", ftos(campaign_bots[0]));
86
87         baseskill = cvar("g_campaign_skill");
88         cvar_set("skill", ftos(campaign_botskill[0] + baseskill));
89
90         title = campaign_shortdesc[0];
91         campaign_message = strzone(strcat("\n\n\n\n\n\n\n\n\n\n^1\n", title, "\n^3\n", Campaign_wordwrap(campaign_longdesc[0], 50)));
92 }
93
94 string GetMapname();
95 void() CampaignPostInit =
96 {
97         // now some sanity checks
98         string thismapname;
99         thismapname = GetMapname();
100         if(campaign_mapcfgname[0] != thismapname)
101                 return CampaignBailout(strcat("wrong map: ", campaign_mapcfgname[0], " != ", thismapname));
102         cvar_set("fraglimit", ftos(campaign_fraglimit[0]));
103         cvar_set("timelimit", "0");
104 }
105
106 void() CampaignPreIntermission =
107 {
108         entity head;
109         float won;
110         float lost;
111
112         won = 0;
113         
114         head = findchain(classname, "player");
115         while(head)
116         {
117                 if(clienttype(head) == CLIENTTYPE_REAL)
118                 {
119                         if(head.winning)
120                                 won = won + 1;
121                         else
122                                 lost = lost + 1;
123                 }
124                 head = head.chain;
125         }
126
127         if(won == 1 && lost == 0)
128         {
129                 campaign_won = 1;
130                 bprint("The current level has been WON.\n");
131                 // sound!
132         }
133         else
134         {
135                 campaign_won = 0;
136                 bprint("The current level has been LOST.\n");
137                 // sound!
138         }
139
140         if(campaign_won)
141         {
142                 if(campaign_level == cvar("g_campaign_index"))
143                 {
144                         // advance level
145                         localcmd("set g_campaign_index ");
146                         localcmd(ftos(campaign_level + 1));
147                         localcmd("\n");
148                 }
149                 if(campaign_entries < 2)
150                 {
151                         localcmd("set g_campaign_index 0\n");
152                         localcmd("set g_campaign_won 1\n");
153                 }
154         }
155 }
156
157 void() CampaignPostIntermission =
158 {
159         // NOTE: campaign_won is 0 or 1, that is, points to the next level
160
161         if(campaign_won && campaign_entries < 2)
162         {
163                 // last map won!
164                 localcmd("togglemenu\n");
165                 CampaignFile_Unload();
166                 return;
167         }
168
169         CampaignSetup(campaign_won);
170         CampaignFile_Unload();
171         strunzone(campaign_message);
172 }
173
174
175
176 void(float n) CampaignLevelWarp =
177 {
178         if(!cvar("sv_cheats"))
179                 return;
180         CampaignFile_Unload();
181         CampaignFile_Load(n, 1);
182         if(campaign_entries)
183                 CampaignSetup(0);
184         else
185                 error("Sorry, cheater. You are NOT WELCOME.");
186         CampaignFile_Unload();
187 }