game mode "keyhunt", still in testing
[divverent/nexuiz.git] / data / qcsrc / server / keyhunt.qc
1 string STR_ITEM_KH_KEY = "item_kh_key";
2 #define FOR_EACH_KH_KEY(v) for(v = world; (v = find(v, classname, STR_ITEM_KH_KEY)); )
3
4 typedef void(void) kh_Think_t;
5 var kh_Think_t kh_Controller_Thinkfunc;
6 string kh_Controller_Waitmsg;
7
8 float kh_Team_ByID(float t)
9 {
10         if(t == 0) return COLOR_TEAM1;
11         if(t == 1) return COLOR_TEAM2;
12         if(t == 2) return COLOR_TEAM3;
13         if(t == 3) return COLOR_TEAM4;
14         return 0;
15 }
16
17 entity kh_controller;
18 float kh_tracking_enabled;
19 float kh_teams;
20 .entity kh_next, kh_prev; // linked list
21 .float kh_droptime;
22
23 float kh_sprite_dropped, kh_sprite_finish, kh_sprite_red, kh_sprite_blue, kh_sprite_pink, kh_sprite_yellow, kh_sprite_friend;
24
25 float kh_GetCarrierSprite(float t)
26 {
27         if(t == COLOR_TEAM1) return kh_sprite_red;
28         if(t == COLOR_TEAM2) return kh_sprite_blue;
29         if(t == COLOR_TEAM3) return kh_sprite_pink;
30         if(t == COLOR_TEAM4) return kh_sprite_yellow;
31         return 0;
32 }
33
34 void kh_Controller_SetThink(float t, string msg, kh_Think_t func)
35 {
36         kh_Controller_Thinkfunc = func;
37         kh_controller.cnt = t;
38         if(kh_Controller_Waitmsg != "")
39                 strunzone(kh_Controller_Waitmsg);
40         if(msg == "")
41                 kh_Controller_Waitmsg = "";
42         else
43                 kh_Controller_Waitmsg = strzone(msg);
44         if(t == 0)
45                 kh_controller.nextthink = time; // force
46 }
47
48 void kh_Controller_Think()
49 {
50         entity e;
51         if(self.cnt > 0)
52         {
53                 if(kh_Controller_Waitmsg != "")
54                 {
55                         string s;
56                         if(substring(kh_Controller_Waitmsg, strlen(kh_Controller_Waitmsg)-1, 1) == " ")
57                                 s = strcat(kh_Controller_Waitmsg, ftos(self.cnt));
58                         else
59                                 s = kh_Controller_Waitmsg;
60
61                         dprint(s, "\n");
62
63                         FOR_EACH_PLAYER(e)
64                                 if(clienttype(e) == CLIENTTYPE_REAL)
65                                         centerprint_atprio(e, CENTERPRIO_SPAM, s);
66                 }
67                 self.cnt -= 1;
68         }
69         else if(self.cnt == 0)
70         {
71                 FOR_EACH_PLAYER(e)
72                         if(clienttype(e) == CLIENTTYPE_REAL)
73                                 centerprint_expire(e, CENTERPRIO_SPAM);
74                 self.cnt -= 1;
75                 kh_Controller_Thinkfunc();
76         }
77         self.nextthink = time + 1;
78 }
79
80 void kh_Log()
81 {
82         string s;
83         if(!cvar("sv_eventlog"))
84                 return;
85         // TODO ...
86         GameLogEcho(s, FALSE);
87 }
88
89 // frags f: take from cvar * f
90 // frags 0: no frags
91 void kh_Scores_Event(entity player, entity key, string what, float frags_player, float frags_owner)
92 {
93         float basefrags;
94         basefrags = cvar(strcat("g_balance_keyhunt_score_", what));
95
96         if(frags_player)
97                 player.frags = player.frags + floor(0.5 + basefrags * frags_player);
98         if(frags_owner)
99                 key.owner.frags = key.owner.frags + floor(0.5 + basefrags * frags_owner);
100 }
101
102 void kh_Key_Attach(entity key, float wpchange)
103 {
104         setattachment(key, key.owner, "");
105         setorigin(key, '0 0 0'); // fixed later in think
106         key.angles = '0 0 0';
107         key.flags = 0;
108         key.solid = SOLID_NOT;
109         key.movetype = MOVETYPE_NONE;
110         key.team = key.owner.team;
111         key.nextthink = time;
112 }
113
114 void kh_Key_Detach(entity key, float wpchange)
115 {
116         setattachment(key, world, "");
117         makevectors(key.owner.angles);
118         setorigin(key, key.owner.origin + key.origin_x * v_forward - key.origin_y * v_right + key.origin_z * v_up);
119         key.angles_y = key.owner.angles_y;
120         key.aiment = world;
121         key.flags = FL_ITEM;
122         key.solid = SOLID_TRIGGER;
123         key.movetype = MOVETYPE_TOSS;
124         key.pain_finished = time + cvar("g_balance_keyhunt_delay_return");
125         // let key.team stay
126 }
127
128 void kh_Key_AssignTo(entity key, entity player, float wpchange)
129 {
130         if(key.owner == player)
131                 return;
132
133         if(key.owner)
134         {
135                 kh_Key_Detach(key, wpchange);
136
137                 // remove from linked list
138                 if(key.kh_next)
139                         key.kh_next.kh_prev = key.kh_prev;
140                 key.kh_prev.kh_next = key.kh_next;
141                 key.kh_next = world;
142                 key.kh_prev = world;
143
144                 if(key.owner.kh_next == world)
145                 {
146                         // No longer a key carrier
147                         if(wpchange)
148                                 WaypointSprite_Kill(key.owner.waypointsprite_attachedforcarrier);
149                         else
150                                 WaypointSprite_DetachCarrier(key.owner);
151                 }
152         }
153
154         key.owner = player;
155
156         if(player)
157         {
158                 // insert into linked list
159                 key.kh_next = player.kh_next;
160                 key.kh_prev = player;
161                 player.kh_next = key;
162                 if(key.kh_next)
163                         key.kh_next.kh_prev = key;
164
165                 kh_Key_Attach(key, wpchange);
166
167                 if(key.kh_next == world)
168                 {
169                         // player is now a key carrier
170                         WaypointSprite_AttachCarrier("", player);
171                         player.waypointsprite_attachedforcarrier.waypointsprite_for_player = kh_KeyCarrier_waypointsprite_for_player;
172                         player.waypointsprite_attachedforcarrier.team = player.team;
173                 }
174         }
175
176         key.pusher = world;
177 }
178
179 void kh_Key_Spawn(entity initial_owner, float angle)
180 {
181         entity key;
182         key = spawn();
183         key.classname = STR_ITEM_KH_KEY;
184         key.touch = kh_Key_Touch;
185         key.think = kh_Key_Think;
186         key.nextthink = time;
187         key.items = IT_KEY1 | IT_KEY2;
188         key.cnt = angle;
189         setmodel(key, "models/keyhunt/key3.md3");
190         setsize(key, '0 0 -24', '0 0 25');
191
192         switch(initial_owner.team)
193         {
194                 case COLOR_TEAM1:
195                         key.netname = "^1red key";
196                         key.colormod = '1.73 0.10 0.10';
197                         break;
198                 case COLOR_TEAM2:
199                         key.netname = "^4blue key";
200                         key.colormod = '0.10 0.10 1.73';
201                         break;
202                 case COLOR_TEAM3:
203                         key.netname = "^6pink key";
204                         key.colormod = '1.22 0.10 1.22';
205                         break;
206                 case COLOR_TEAM4:
207                         key.netname = "^3yellow key";
208                         key.colormod = '1.22 1.22 0.10';
209                         break;
210                 default:
211                         key.netname = "NETGIER key";
212                         key.colormod = '1.00 1.00 1.00';
213                         break;
214         }
215
216         sprint(initial_owner, strcat("You got the ^2", key.netname, "\n"));
217
218         WaypointSprite_AttachCarrier("", key);
219         key.waypointsprite_attachedforcarrier.waypointsprite_for_player = kh_Key_waypointsprite_for_player;
220
221         kh_Key_AssignTo(key, initial_owner, TRUE);
222 }
223
224 void kh_Key_Remove(entity key)
225 {
226         entity o;
227         o = key.owner;
228         kh_Key_AssignTo(key, world, FALSE);
229         if(o) // it was attached
230                 WaypointSprite_Kill(key.waypointsprite_attachedforcarrier);
231         else // it was dropped
232                 WaypointSprite_DetachCarrier(key);
233
234         remove(key);
235 }
236
237 void kh_Key_Collect(entity key, entity player)
238 {
239         entity head;
240
241         kh_Scores_Event(player, key, "collect", 1, 0);
242         bprint(player.netname, "^7 collected the ", key.netname, "\n");
243         kh_Key_AssignTo(key, player, TRUE);
244
245         FOR_EACH_KH_KEY(key)
246                 if(!key.owner || key.team != player.team)
247                         goto notallowned;
248         FOR_EACH_PLAYER(head)
249         {
250                 if(head.team == player.team)
251                         if(head.kh_next)
252                                 centerprint(head, "All keys are in your team's hands!\n\nMeet the other key carriers ^1NOW^7!\n");
253                         else
254                                 centerprint(head, "All keys are in your team's hands!\n\nHelp the key carriers to meet!\n");
255                 else
256                         centerprint(head, "All keys are in the enemy's hands!\n\nInterfere ^1NOW^7!\n");
257         }
258 :notallowned
259 }
260
261 void kh_Key_DropAll(entity player)
262 {
263         entity key;
264         entity mypusher;
265         mypusher = world;
266         if(player.pusher)
267                 if(time < player.pushltime)
268                         mypusher = player.pusher;
269         while((key = player.kh_next))
270         {
271                 kh_Scores_Event(player, key, "losekey", 0, 0);
272                 bprint(player.netname, "^7 lost the ", key.netname, "\n");
273                 kh_Key_AssignTo(key, world, TRUE);
274                 key.pusher = player.pusher;
275                 key.pushltime = player.pushltime;
276         }
277 }
278
279 void kh_Key_Touch()
280 {
281         if(self.owner) // already carried
282                 return;
283         if(other.classname != "player")
284                 return;
285         if(other.deadflag != DEAD_NO)
286                 return;
287         if(other == self.enemy)
288                 if(time < self.kh_droptime + cvar("g_balance_keyhunt_delay_collect"))
289                         return; // you just dropped it!
290         kh_Key_Collect(self, other);
291 }
292
293 void kh_Key_Think()
294 {
295         if(self.owner)
296         {
297                 makevectors('0 1 0' * (self.cnt + math_mod(time, 360) * 45));
298                 setorigin(self, v_forward * 16);
299
300                 if(self.owner.buttonuse)
301                 if(time >= self.owner.kh_droptime + cvar("g_balance_keyhunt_delay_drop"))
302                 {
303                         self.owner.kh_droptime = time;
304                         self.kh_droptime = time; // prevent collecting this one for some time
305                         self.enemy = self.owner;
306                         self.pusher = world;
307                         kh_Scores_Event(self.owner, self, "dropkey", 0, 0);
308                         bprint(self.owner.netname, "^7 dropped the ", self.netname, "\n");
309                         kh_Key_AssignTo(self, world, TRUE);
310                 }
311         }
312
313         // if in nodrop or time over, end the round
314         if(!self.owner)
315                 if(time > self.pain_finished)
316                         kh_LoserTeam(self.team, self);
317         
318         if(self.owner)
319         {
320                 entity key;
321                 vector p;
322                 float teem;
323                 teem = self.team;
324                 p = self.owner.origin;
325                 FOR_EACH_KH_KEY(key)
326                 {
327                         if(key.owner == self.owner)
328                                 continue;
329                         if(key.owner)
330                         if(key.team == teem)
331                         if(vlen(key.owner.origin - p) < cvar("g_balance_keyhunt_maxdist"))
332                                 continue;
333                         goto not_winning;
334                 }
335                 kh_WinnerTeam(teem);
336 :not_winning
337         }
338
339         self.nextthink = time + 0.1;
340 }
341
342 void kh_WinnerTeam(float teem)
343 {
344         // all key carriers get some points
345         entity key;
346         float first;
347         float score;
348         score = 1.0 / kh_teams;
349         first = TRUE;
350         FOR_EACH_KH_KEY(key)
351         {
352                 kh_Scores_Event(key.owner, key, "capture", score, 0);
353                 if(key.owner.kh_next == key)
354                 {
355                         if(!first)
356                                 bprint("^7, ");
357                         bprint(key.owner.netname);
358                         first = FALSE;
359                 }
360         }
361         bprint("^7 captured the keys for the ", ColoredTeamName(teem), "\n");
362         kh_FinishRound();
363 }
364
365 void kh_LoserTeam(float teem, entity lostkey)
366 {
367         entity player, key, attacker;
368         float players;
369         float keys;
370         float score;
371
372         attacker = world;
373         if(lostkey.pusher)
374                 if(player.pusher.team != player.team)
375                         if(player.pusher.classname == "player")
376                                 attacker = lostkey.pusher;
377
378         players = keys = 0;
379
380         if(attacker)
381         {
382                 kh_Scores_Event(attacker, world, "push", 1, 0);
383                 centerprint(attacker, "Your push is the best!\n\n\n");
384                 bprint("The ", ColoredTeamName(teem), "^7 could not take care of the ", lostkey.netname, "\n");
385         }
386         else
387         {
388                 FOR_EACH_PLAYER(player)
389                         if(player.team != teem)
390                                 ++players;
391                 
392                 FOR_EACH_KH_KEY(key)
393                         if(key.owner && key.team != teem)
394                                 ++keys;
395
396                 score = 1.0 / (keys * cvar("g_balance_keyhunt_score_destroyed_ownfactor") + players);
397
398                 FOR_EACH_PLAYER(player)
399                         if(player.team != teem)
400                                 kh_Scores_Event(player, world, "destroyed", score, 0);
401
402                 FOR_EACH_KH_KEY(key)
403                         if(key.owner && key.team != teem)
404                                 kh_Scores_Event(key.owner, world, "destroyed", score * cvar("g_balance_keyhunt_score_destroyed_ownfactor"), 0);
405
406                 bprint("The ", ColoredTeamName(teem), "^7 could not take care of the ", lostkey.netname, "\n");
407         }
408
409         kh_FinishRound();
410 }
411
412 void kh_FinishRound()
413 {
414         // prepare next round
415         entity key;
416         FOR_EACH_KH_KEY(key)
417                 kh_Key_Remove(key);
418
419         kh_Controller_SetThink(cvar("g_balance_keyhunt_delay_round"), "Round starts in ", kh_StartRound);
420 }
421
422 float kh_EnoughPlayers()
423 {
424         float i, players, teem;
425         entity player;
426         
427         // find a random player per team
428         for(i = 0; i < kh_teams; ++i)
429         {
430                 teem = kh_Team_ByID(i);
431                 players = 0;
432                 FOR_EACH_PLAYER(player)
433                         if(player.deadflag == DEAD_NO)
434                                 if(player.team == teem)
435                                         ++players;
436                 if(players == 0)
437                         return FALSE;
438         }
439         return TRUE;
440 }
441
442 void kh_WaitForPlayers()
443 {
444         if(kh_EnoughPlayers())
445                 kh_Controller_SetThink(cvar("g_balance_keyhunt_delay_round"), "Round starts in ", kh_StartRound);
446         else
447                 kh_Controller_SetThink(1, "Waiting for players to join...", kh_WaitForPlayers);
448 }
449
450 void kh_StartRound()
451 {
452         float i, players, teem;
453         entity player;
454
455         if(!kh_EnoughPlayers())
456         {
457                 kh_Controller_SetThink(1, "Waiting for players to join...", kh_WaitForPlayers);
458                 return;
459         }
460
461         for(i = 0; i < kh_teams; ++i)
462         {
463                 teem = kh_Team_ByID(i);
464                 players = 0;
465                 entity my_player;
466                 FOR_EACH_PLAYER(player)
467                         if(player.deadflag == DEAD_NO)
468                                 if(player.team == teem)
469                                 {
470                                         ++players;
471                                         if(random() * players <= 1)
472                                                 my_player = player;
473                                 }
474                 kh_Key_Spawn(my_player, 360 * i / kh_teams);
475         }
476
477         kh_tracking_enabled = FALSE;
478         kh_Controller_SetThink(cvar("g_balance_keyhunt_delay_tracking"), "Scanning frequency range...", kh_EnableTrackingDevice);
479 }
480
481 void kh_setstatus()
482 {
483         if(kh_teams)
484         {
485                 float kh_KEY;
486                 kh_KEY = (IT_RED_FLAG_TAKEN | IT_RED_FLAG_LOST | IT_BLUE_FLAG_TAKEN | IT_BLUE_FLAG_LOST); // the one impossible combination
487                 if(self.kh_next)
488                         self.items = self.items | kh_KEY;
489                 else
490                         self.items = self.items - (self.items & kh_KEY);
491         }
492 }
493
494 void kh_EnableTrackingDevice()
495 {
496         kh_tracking_enabled = TRUE;
497 }
498
499 float kh_Key_waypointsprite_for_player(entity e)
500 {
501         if(!kh_tracking_enabled)
502                 return 0;
503         if(!self.owner)
504                 return kh_sprite_dropped;
505         if(!self.owner.owner)
506                 return kh_sprite_dropped;
507         return 0; // draw only when key is not owned
508 }
509
510 float kh_KeyCarrier_waypointsprite_for_player(entity e)
511 {
512         entity key;
513         
514         if(e.classname != "player" || self.team != e.team)
515                 if(!kh_tracking_enabled)
516                         return 0;
517
518         FOR_EACH_KH_KEY(key)
519                 if(!key.owner || key.team != e.team)
520                 {
521                         if(self.team == e.team)
522                                 return kh_sprite_friend;
523                         else
524                                 return kh_GetCarrierSprite(self.team);
525                 }
526
527         return kh_sprite_finish;
528 }
529
530 float kh_HandleFrags(entity attacker, entity targ, float f)
531 {
532         float newfrags;
533
534         if(f <= 0)
535                 return f;
536         if(attacker == targ)
537                 return f;
538
539         if(targ.kh_next)
540                 f = f - 1 + cvar("g_balance_keyhunt_score_carrierfrag");
541
542         if(newfrags)
543                 f = f - 1 + newfrags;
544         return f;
545 }
546
547 void kh_init()
548 {
549         precache_model("models/sprites/key-dropped.sp2");
550         precache_model("models/sprites/keycarrier-finish.sp2");
551         precache_model("models/sprites/keycarrier-friend.sp2");
552         precache_model("models/sprites/keycarrier-red.sp2");
553         precache_model("models/sprites/keycarrier-blue.sp2");
554         precache_model("models/sprites/keycarrier-pink.sp2");
555         precache_model("models/sprites/keycarrier-yellow.sp2");
556         precache_model("models/keyhunt/key3.md3");
557
558         // setup variables
559         kh_teams = cvar("g_keyhunt_teams_override");
560         if(kh_teams < 2)
561                 kh_teams = cvar("g_keyhunt_teams");
562         if(kh_teams < 2)
563                 kh_teams = 2;
564
565         // make a KH entity for controlling the game
566         kh_controller = spawn();
567         kh_controller.think = kh_Controller_Think;
568         kh_Controller_SetThink(0, "", kh_WaitForPlayers);
569
570         setmodel(kh_controller, "models/sprites/key-dropped.sp2");
571         kh_sprite_dropped = kh_controller.modelindex;
572         setmodel(kh_controller, "models/sprites/keycarrier-finish.sp2");
573         kh_sprite_finish = kh_controller.modelindex;
574         setmodel(kh_controller, "models/sprites/keycarrier-friend.sp2");
575         kh_sprite_friend = kh_controller.modelindex;
576         setmodel(kh_controller, "models/sprites/keycarrier-red.sp2");
577         kh_sprite_red = kh_controller.modelindex;
578         setmodel(kh_controller, "models/sprites/keycarrier-blue.sp2");
579         kh_sprite_blue = kh_controller.modelindex;
580         setmodel(kh_controller, "models/sprites/keycarrier-pink.sp2");
581         kh_sprite_pink = kh_controller.modelindex;
582         setmodel(kh_controller, "models/sprites/keycarrier-yellow.sp2");
583         kh_sprite_yellow = kh_controller.modelindex;
584         setmodel(kh_controller, "");
585 }
586
587 void kh_finalize()
588 {
589         // to be called before intermission
590         kh_FinishRound();
591         remove(kh_controller);
592         kh_controller = world;
593 }