]> icculus.org git repositories - divverent/nexuiz.git/blob - qcsrc/gamec/ctf.c
additional stuffcmd for player name change
[divverent/nexuiz.git] / qcsrc / gamec / ctf.c
1
2 .entity flagcarried;
3
4 float FLAGSCORE_PICKUP        =  1;
5 float FLAGSCORE_RETURN        =  5; // returned by owner team
6 float FLAGSCORE_RETURNROGUE   = 10; // returned by rogue team
7 float FLAGSCORE_CAPTURE       =  5;
8 float FLAGSCORE_CAPTURE_TEAM  = 20;
9
10 float FLAG_BASE = 1;
11 float FLAG_CARRY = 2;
12 float FLAG_DROPPED = 3;
13
14 void() FlagThink;
15 void() FlagTouch;
16
17 void() place_flag =
18 {
19         self.mdl = self.model;
20         self.flags = FL_ITEM;
21         self.solid = SOLID_TRIGGER;
22         self.movetype = MOVETYPE_TOSS;
23         self.velocity = '0 0 0';
24         self.origin_z = self.origin_z + 6;
25         self.think = FlagThink;
26         self.touch = FlagTouch;
27         self.nextthink = time + 0.1;
28         self.cnt = FLAG_BASE;
29         self.mangle = self.angles;
30         self.effects = self.effects | EF_DIMLIGHT;
31         if (!droptofloor(0, 0))
32         {
33                 dprint("Flag fell out of level at ", vtos(self.origin), "\n");
34                 remove(self);
35                 return;
36         }
37         self.oldorigin = self.origin;
38 };
39
40 void(entity e) RegenFlag =
41 {
42         e.movetype = MOVETYPE_TOSS;
43         e.solid = SOLID_TRIGGER;
44         // TODO: play a sound here
45         sound (e, CHAN_AUTO, self.noise3, 1, ATTN_NONE);
46         setorigin(e, e.oldorigin);
47         e.angles = e.mangle;
48         e.cnt = FLAG_BASE;
49         e.owner = world;
50         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
51 };
52
53 void(entity e) ReturnFlag =
54 {
55         if (e.owner)
56         if (e.owner.flagcarried == e)
57                 e.owner.flagcarried = world;
58         e.owner = world;
59         RegenFlag(e);
60 };
61
62 void(entity e) DropFlag =
63 {
64         local entity p;
65
66         if (!e.owner)
67         {
68                 dprint("FLAG: drop - no owner?!?!\n");
69                 return;
70         }
71         p = e.owner;
72         if (p.flagcarried != e)
73         {
74                 dprint("FLAG: drop - owner is not carrying this flag??\n");
75                 return;
76         }
77         bprint(p.netname);
78         if (e.team == 5)
79                 bprint(" lost the RED flag\n");
80         else
81                 bprint(" lost the BLUE flag\n");
82         if (p.flagcarried == e)
83                 p.flagcarried = world;
84         e.owner = world;
85
86         e.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
87         e.solid = SOLID_TRIGGER;
88         e.movetype = MOVETYPE_TOSS;
89         // setsize(e, '-16 -16 0', '16 16 74');
90         setorigin(e, p.origin - '0 0 24');
91         e.cnt = FLAG_DROPPED;
92         e.velocity = '0 0 300';
93         e.pain_finished = time + 30;
94 };
95
96 void() FlagThink =
97 {
98         local entity e;
99         local vector v;
100         local float f;
101
102         self.nextthink = time + 0.1;
103
104         if (self.cnt == FLAG_BASE)
105                 return;
106
107         if (self.cnt == FLAG_DROPPED)
108         {
109                 if (time > self.pain_finished)
110                 {
111                         if (self.team == 5)
112                                 bprint("The RED flag has returned to base\n");
113                         else
114                                 bprint("The BLUE flag has returned to base\n");
115                         ReturnFlag(self);
116                 }
117                 return;
118         }
119
120         e = self.owner;
121         if (e.classname != "player" || (e.deadflag) || (e.flagcarried != self))
122         {
123                 DropFlag(self);
124                 return;
125         }
126
127         // borrowed from threewave CTF, because it would be way too much work
128
129         makevectors (e.angles);
130         v = v_forward;
131         v_z = 0 - v_z; // reverse z
132
133         f = 14;
134         /*
135         if (e.frame >= 29 && e.frame <= 40)
136         {
137                 if (e.frame >= 29 && e.frame <= 34)
138                 {
139                         //axpain
140                         if      (e.frame == 29) f = f + 2;
141                         else if (e.frame == 30) f = f + 8;
142                         else if (e.frame == 31) f = f + 12;
143                         else if (e.frame == 32) f = f + 11;
144                         else if (e.frame == 33) f = f + 10;
145                         else if (e.frame == 34) f = f + 4;
146                 }
147                 else if (e.frame >= 35 && e.frame <= 40)
148                 {
149                         // pain
150                         if      (e.frame == 35) f = f + 2;
151                         else if (e.frame == 36) f = f + 10;
152                         else if (e.frame == 37) f = f + 10;
153                         else if (e.frame == 38) f = f + 8;
154                         else if (e.frame == 39) f = f + 4;
155                         else if (e.frame == 40) f = f + 2;
156                 }
157         }
158         else if (e.frame >= 103 && e.frame <= 118)
159         {
160                 if      (e.frame >= 103 && e.frame <= 104) f = f + 6;  //nailattack
161                 else if (e.frame >= 105 && e.frame <= 106) f = f + 6;  //light
162                 else if (e.frame >= 107 && e.frame <= 112) f = f + 7;  //rocketattack
163                 else if (e.frame >= 112 && e.frame <= 118) f = f + 7;  //shotattack
164         }
165         */
166         self.angles = e.angles + '0 0 -45';
167         setorigin (self, e.origin + '0 0 -16' - f*v + v_right * 22);
168         self.nextthink = time + 0.01;
169 };
170
171 float   flagcaptimerecord;
172 .float  flagpickuptime;
173
174 void() FlagTouch =
175 {
176         local float t;
177         local entity head;
178         if (other.classname != "player")
179                 return;
180         if (other.health < 1) // ignore dead players
181                 return;
182
183         if (self.cnt == FLAG_CARRY)
184                 return;
185
186         if (self.cnt == FLAG_BASE)
187         if (other.team == self.team)
188         if (other.flagcarried) // he's got a flag
189         if (other.flagcarried.team != self.team) // capture
190         {
191                 t = time - other.flagpickuptime;
192                 if (flagcaptimerecord == 0)
193                 {
194                         if (other.flagcarried.team == 5)
195                                 bprint(other.netname, " captured the RED flag in ", ftos(t), " seconds\n");
196                         else
197                                 bprint(other.netname, " captured the BLUE flag in ", ftos(t), " seconds\n");
198                         flagcaptimerecord = t;
199                 }
200                 else if (t < flagcaptimerecord)
201                 {
202                         if (other.flagcarried.team == 5)
203                                 bprint(other.netname, " captured the RED flag in ", ftos(t), ", breaking the previous record of", ftos(flagcaptimerecord), " seconds\n");
204                         else
205                                 bprint(other.netname, " captured the BLUE flag in ", ftos(t), ", breaking the previous record of", ftos(flagcaptimerecord), " seconds\n");
206                         flagcaptimerecord = t;
207                 }
208                 else
209                 {
210                         if (other.flagcarried.team == 5)
211                                 bprint(other.netname, " captured the RED flag in ", ftos(t), ", failing to break the previous record of", ftos(flagcaptimerecord), " seconds\n");
212                         else
213                                 bprint(other.netname, " captured the BLUE flag in ", ftos(t), ", failing to break the previous record of", ftos(flagcaptimerecord), " seconds\n");
214                 }
215                 other.frags = other.frags + FLAGSCORE_CAPTURE;
216                 head = find(head, classname, "player");
217                 while (head)
218                 {
219                         if (head.team == self.goalentity.team)
220                                 head.frags = head.frags + FLAGSCORE_CAPTURE_TEAM;
221                         head = find(head, classname, "player");
222                 }
223                 sound (self, CHAN_AUTO, self.noise2, 1, ATTN_NONE);
224                 ReturnFlag(other.flagcarried);
225         }
226         if (self.cnt == FLAG_BASE)
227         if (other.team == 5 || other.team == 14) // only red and blue team can steal flags
228         if (other.team != self.team)
229         if (!other.flagcarried)
230         {
231                 // pick up
232                 other.flagpickuptime = time; // used for timing runs
233                 self.solid = SOLID_NOT;
234                 setorigin(self, self.origin); // relink
235                 self.owner = other;
236                 other.flagcarried = self;
237                 self.cnt = FLAG_CARRY;
238                 if (other.flagcarried.team == 5)
239                         bprint(other.netname, " got the RED flag\n");
240                 else
241                         bprint(other.netname, " got the BLUE flag\n");
242                 other.frags = other.frags + FLAGSCORE_PICKUP;
243                 sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
244                 return;
245         }
246
247         if (self.cnt == FLAG_DROPPED)
248         {
249                 self.flags = FL_ITEM; // clear FL_ONGROUND and any other junk
250                 if (other.team == self.team || (other.team != 5 && other.team != 14))
251                 {
252                         // return flag
253                         if (self.team == 5)
254                                 bprint(other.netname, " returned the RED flag\n");
255                         else
256                                 bprint(other.netname, " returned the BLUE flag\n");
257                         if (other.team == 5 || other.team == 14)
258                                 other.frags = other.frags + FLAGSCORE_RETURN;
259                         else
260                                 other.frags = other.frags + FLAGSCORE_RETURNROGUE;
261                         sound (self, CHAN_AUTO, self.noise1, 1, ATTN_NONE);
262                         ReturnFlag(self);
263                 }
264                 else if (!other.flagcarried)
265                 {
266                         // pick up
267                         other.flagpickuptime = time; // used for timing runs
268                         self.solid = SOLID_NOT;
269                         setorigin(self, self.origin); // relink
270                         self.owner = other;
271                         other.flagcarried = self;
272                         self.cnt = FLAG_CARRY;
273                         if (self.team == 5)
274                                 bprint(other.netname, " picked up the RED flag\n");
275                         else
276                                 bprint(other.netname, " picked up the BLUE flag\n");
277                         other.frags = other.frags + FLAGSCORE_PICKUP;
278                         sound (self, CHAN_AUTO, self.noise, 1, ATTN_NONE);
279                 }
280         }
281 };
282
283 /*QUAKED info_player_team1 (1 0 0) (-16 -16 -24) (16 16 24)
284 CTF Starting point for a player
285 in team one (Red).
286
287 Keys:
288 "angle"
289  viewing angle when spawning
290 */
291 void() info_player_team1 = {};//self.team = 4;self.classname = "info_player_start";info_player_start();};
292
293 /*QUAKED info_player_team2 (1 0 0) (-16 -16 -24) (16 16 24)
294 CTF Starting point for a player in
295 team two (Blue).
296
297 Keys:
298 "angle"
299  viewing angle when spawning
300 */
301 void() info_player_team2 = {};//self.team = 13;self.classname = "info_player_start";info_player_start();};
302
303 /*QUAKED item_flag_team1 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
304 CTF flag for team one (Red).
305 Multiple are allowed.
306
307 Keys:
308 "angle"
309  Angle the flag will point
310 (minus 90 degrees)
311 "model"
312  model to use, note this needs red and blue as skins 0 and 1
313  (default models/ctf/flag.md3)
314 "noise"
315  sound played when flag is picked up
316  (default ctf/take.wav)
317 "noise1"
318  sound played when flag is returned by a teammate
319  (default ctf/return.wav)
320 "noise2"
321  sound played when flag is captured
322  (default ctf/capture.wav)
323 "noise3"
324  sound played when flag is lost in the field and respawns itself
325  (default ctf/respawn.wav)
326 */
327
328 void() item_flag_team1 =
329 {
330         if (!cvar("g_ctf"))
331                 return;
332         self.team = 5; // color 4 team (red)
333         self.items = IT_KEY2; // gold key (redish enough)
334         self.skin = 0;
335         if (!self.model)
336                 self.model = "models/ctf/flag.md3";
337         if (!self.noise)
338                 self.noise = "ctf/take.wav";
339         if (!self.noise1)
340                 self.noise1 = "ctf/return.wav";
341         if (!self.noise2)
342                 self.noise2 = "ctf/capture.wav";
343         if (!self.noise3)
344                 self.noise3 = "ctf/respawn.wav";
345         precache_model (self.model);
346         setmodel (self, self.model);
347         precache_sound (self.noise);
348         precache_sound (self.noise1);
349         precache_sound (self.noise2);
350         precache_sound (self.noise3);
351         setsize(self, '-16 -16 0', '16 16 74');
352         self.nextthink = time + 0.2; // start after doors etc
353         self.think = place_flag;
354 };
355
356 /*QUAKED item_flag_team2 (0 0.5 0.8) (-48 -48 -24) (48 48 64)
357 CTF flag for team two (Blue).
358 Multiple are allowed.
359
360 Keys:
361 "angle"
362  Angle the flag will point
363 (minus 90 degrees)
364
365 */
366
367 void() item_flag_team2 =
368 {
369         if (!cvar("g_ctf"))
370                 return;
371         self.team = 14; // color 13 team (blue)
372         self.items = IT_KEY1; // silver key (bluish enough)
373         self.skin = 1;
374         if (!self.model)
375                 self.model = "models/ctf/flag.md3";
376         if (!self.noise)
377                 self.noise = "ctf/take.wav";
378         if (!self.noise1)
379                 self.noise1 = "ctf/return.wav";
380         if (!self.noise2)
381                 self.noise2 = "ctf/capture.wav";
382         if (!self.noise3)
383                 self.noise3 = "ctf/respawn.wav";
384         precache_model (self.model);
385         setmodel (self, self.model);
386         precache_sound (self.noise);
387         precache_sound (self.noise1);
388         precache_sound (self.noise2);
389         precache_sound (self.noise3);
390         setsize(self, '-16 -16 0', '16 16 74');
391         self.nextthink = time + 0.2; // start after doors etc
392         self.think = place_flag;
393 };
394