]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/runematch.qc
add field .nottargeted to override targetname checks (for misc_follow on anything)
[divverent/nexuiz.git] / data / qcsrc / server / runematch.qc
1
2 float   RUNE_FIRST      = 1;
3 float   RUNE_STRENGTH   = 1;
4 float   RUNE_DEFENSE    = 2;
5 float   RUNE_REGEN      = 4;
6 float   RUNE_SPEED      = 8;
7 float   RUNE_VAMPIRE    = 16;
8 float   RUNE_LAST       = 16;
9
10 float   CURSE_FIRST     = 8192;
11 float   CURSE_WEAK      = 8192;
12 float   CURSE_VULNER    = 16384;
13 float   CURSE_VENOM     = 32768;
14 float   CURSE_SLOW      = 65536;
15 float   CURSE_EMPATHY   = 131072;
16 float   CURSE_LAST      = 131072;
17
18 /* rune ideas:
19
20         Doom/Death
21         Rune: When you damage enemies, you have a slight chance of instant-killing them (porportional to damage dealt / their health)
22         Curse: When you are damaged, you have a chance of being instant-killed
23
24         Vengence/Slothful
25         Rune: The lower your health below 100, the more damage you deal (does not decrease your damage if you're above 100)
26         Curse: The higher your health (up to 100), the less damage you deal (at 100 hp deal 1/5th damage)
27
28 */
29
30 /*QUAKED spawnfunc_runematch_spawn_point (1 0 0) (-16 -16 -24) (16 16 24)
31 spawn point for runes in runematch
32 */
33
34 void spawnfunc_runematch_spawn_point()
35 {
36         if(!g_runematch)
37         {
38                 remove(self);
39                 return;
40         }
41
42         droptofloor();
43         setorigin(self, self.origin + '0 0 35');
44 }
45
46 string RuneName(float r)
47 {
48         if(r == RUNE_STRENGTH)
49                 return "^1Strength^7";
50         if(r == RUNE_DEFENSE)
51                 return "^4Defense^7";
52         if(r == RUNE_REGEN)
53                 return "^2Vitality^7";
54         if(r == RUNE_SPEED)
55                 return "^3Speed^7";
56         if(r == RUNE_VAMPIRE)
57                 return "^6Vampire^7";
58
59         if(r == CURSE_WEAK)
60                 return "^1Weakness^7";
61         if(r == CURSE_VULNER)
62                 return "^4Vulnerability^7";
63         if(r == CURSE_VENOM)
64                 return "^2Venom^7";
65         if(r == CURSE_SLOW)
66                 return "^3Slow^7";
67         if(r == CURSE_EMPATHY)
68                 return "^6Empathy^7";
69         return strcat("^8[unnamed", ftos(r), "]^7");
70 }
71
72 vector RuneColormod(float r)
73 {
74         vector _color;
75         if(r == RUNE_STRENGTH)
76                 _color = '255 0 0';
77         if(r == RUNE_DEFENSE)
78                 _color = '0 0 255';//'0 102 255';//
79         if(r == RUNE_REGEN)
80                 _color = '0 204 0';//'0 255 0';
81         if(r == RUNE_SPEED)
82                 _color = 0.35*'185 185 0';//255 230 0';//'255 255 0';
83         if(r == RUNE_VAMPIRE)
84                 _color = '64 0 128';//'108 0 217';//'128 0 255';//'179 0 204';//
85
86         if(r == CURSE_WEAK)
87                 _color = '255 0 0';
88         if(r == CURSE_VULNER)
89                 _color = '0 0 255';//'0 102 255';//
90         if(r == CURSE_VENOM)
91                 _color = '0 204 0';//'0 255 0';
92         if(r == CURSE_SLOW)
93                 _color = 0.5*'185 185 0';//'255 255 0';
94         if(r == CURSE_EMPATHY)
95                 _color = '179 0 204';//'128 0 255';
96
97         return _color * (1 / 255) * cvar("g_runematch_rune_color_strength");
98 }
99
100 float count_rune_spawnpoints()
101 {
102         float num;
103         entity e;
104         num = 0;
105         e = findchain(classname, "runematch_spawn_point");
106         while(e)
107         {
108                 num = num + 1;
109                 e = e.chain;
110
111         }
112         return num;
113 }
114
115 entity rune_find_spawnpoint(float num, float r)
116 {
117         entity e;
118         e = world;
119         do
120         {
121                 e = find(e, classname, "runematch_spawn_point");
122                 if(!e)
123                         e = find(e, classname, "runematch_spawn_point");
124                 if(!e)
125                         break;
126
127                 if(r < 0)
128                 {
129                         return e; // emergency: prevent crash
130                 }
131
132                 if(e.owner != world)//e.wait > time) // already taken
133                         continue;
134
135                 if(r <= 1)
136                 {
137                         return e;
138                 }
139
140                 r = r - 1;
141
142         }while(e);
143         return world;
144 }
145
146 void rune_respawn();
147
148 void RuneCarriedThink()
149 {
150         float rcount, rnum;
151         vector ang;
152         entity rune;
153
154         if(self.owner.classname != "player" || time < restart_countdown)
155         {
156                 rune_respawn();
157                 return;
158         }
159
160         self.nextthink = time + 0.1;
161
162         // count runes my owner holds
163         rcount = 0;
164         rune = find(world, classname, "rune");
165         while(rune)
166         {
167                 if(rune.owner == self.owner)
168                         rcount = rcount + 1;
169                 if(rune == self)
170                         rnum = rcount;
171                 rune = find(rune, classname, "rune");
172         }
173
174         ang_y = rnum*(360 / rcount) + mod(time, 360)*45;//180;
175
176         makevectors(ang);
177
178         //setorigin(self, v_forward*32);
179         self.view_ofs = v_forward*32;
180 }
181
182 void rune_touch()
183 {
184         if(other.classname != "player" || other.health < 1)
185                 return;
186         if(self.wait > time)
187                 return; // "notouch" time isn't finished
188
189         // detach from the spawn point you're on
190         if(self.owner.classname == "runematch_spawn_point")
191         {
192                 self.owner.owner = world;
193                 self.owner = world;
194         }
195
196         self.owner = other;
197         self.enemy.owner = other;
198         //setattachment(self, other, "");
199         setattachment(self, world, "");
200
201         self.movetype = MOVETYPE_FOLLOW;
202         self.view_ofs = other.origin;
203
204         setorigin(self, '0 0 0');
205         other.runes = other.runes | self.runes | self.enemy.runes;
206
207         //self.think = SUB_Null;
208         //self.nextthink = 0;
209         self.think = RuneCarriedThink;
210         self.nextthink = time;
211         self.touch = SUB_Null;
212
213         self.solid = SOLID_NOT;
214         setorigin(self, self.origin);
215
216         //sprint(other, strcat("^3You have picked up ",
217         //      RuneName(self.runes & (RUNE_LAST*2-1)), " and "));
218         //sprint(other, strcat(RuneName(self.enemy.runes & (CURSE_WEAK | CURSE_VULNER | CURSE_VENOM | CURSE_SLOW | CURSE_EMPATHY)), "\n"));
219
220         bprint("^3", other.netname, "^7 has picked up ",
221                 RuneName(self.runes & (RUNE_LAST*2-1)), "^7 and ");
222         bprint(RuneName(self.enemy.runes & (CURSE_WEAK | CURSE_VULNER | CURSE_VENOM | CURSE_SLOW | CURSE_EMPATHY)), "\n");
223 }
224
225 void rune_respawn()
226 {
227         float num, r;
228         entity spot;
229         num = count_rune_spawnpoints();
230         r = ceil(random()*num);
231
232         if(self.owner.classname == "runematch_spawn_point")
233         {
234                 self.owner.owner = world;
235                 self.owner = world;
236         }
237
238
239         spot = rune_find_spawnpoint(num, r);
240         if(!spot)
241         {
242                 bprint("Warning: couldn't find spawn spot for rune respawn\n");
243                 return;
244         }
245
246         self.solid = SOLID_TRIGGER;
247
248         setorigin(self, spot.origin);
249         self.owner = spot;
250         spot.owner = self;
251
252         self.touch = rune_touch;
253
254         self.think = rune_respawn;
255         self.nextthink = time + cvar("g_runematch_shuffletime");//30 + random()*5; // fixme: cvar
256 }
257
258 entity FindRune(entity own, string clname, float r)
259 {
260         entity rune;
261         float _count, c;
262
263         c = _count = 0;
264         rune = world;
265
266         do
267         {
268                 rune = find(rune, classname, clname);
269                 if(!rune)
270                         rune = find(rune, classname, clname);
271                 if(!rune)
272                         break;
273                 if(rune.owner == own)
274                 {
275                         _count = _count + 1;
276                         if(_count >= r)
277                                 return rune;
278                         if(r <= 1)
279                                 return rune;
280                 }
281                 c = c + 1;
282         }while(c < 30);
283         return world;
284 }
285
286
287 void DropRune(entity pl, entity e)
288 {
289         //entity pl;
290
291         //pl = e.owner;
292         // detach from player
293         setattachment(e, world, "");
294         e.owner = world;
295         e.enemy.owner = world;
296         // don't instantly touch player again
297         e.wait = time + 1; // "notouch" time
298         e.movetype = MOVETYPE_TOSS;
299         e.solid = SOLID_TRIGGER;
300         // reposition itself if not picked up soon
301         e.think = rune_respawn;
302         e.nextthink = time + cvar("g_runematch_respawntime");//15 + random()*5; // fixme: cvar
303         e.touch = rune_touch;
304
305         pl.runes = pl.runes - (pl.runes & (e.runes | e.enemy.runes));
306
307         // toss from player
308         setorigin(e, pl.origin + '0 0 10');
309         e.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
310
311
312         bprint("^3", pl.netname, "^7 has lost ",
313                 RuneName(e.runes & (RUNE_LAST*2-1)), "^7 and ");
314         bprint(RuneName(e.enemy.runes & (CURSE_WEAK | CURSE_VULNER | CURSE_VENOM | CURSE_SLOW | CURSE_EMPATHY)), "\n");
315 }
316
317 float RuneMatchesCurse(float r, float c)
318 {
319         float cr;
320         if(r & RUNE_STRENGTH)
321                 cr = CURSE_WEAK;
322         else if(r & RUNE_DEFENSE)
323                 cr = CURSE_VULNER;
324         else if(r & RUNE_REGEN)
325                 cr = CURSE_VENOM;
326         else if(r & RUNE_SPEED)
327                 cr = CURSE_SLOW;
328         else if(r & RUNE_VAMPIRE)
329                 cr = CURSE_EMPATHY;
330         else return FALSE; // fixme: error?
331
332         if(c & cr)
333                 return TRUE;
334         return FALSE;
335 }
336
337 // player died, drop runes
338 // each rune should pair up with a random curse and then be tossed from the player
339 void DropAllRunes(entity pl)
340 {
341         entity rune, curse;
342         float rcount, ccount, r, c, rand, prevent_same, numtodrop, tries;
343
344         entity curse1, rune1, curse2, rune2;
345
346         rune = curse = world;
347         rcount = ccount = r = c = 0;
348         rune = find(rune, classname, "rune");
349         while(rune)
350         {
351                 if(rune.owner == pl)
352                         rcount = rcount + 1;
353                 rune = find(rune, classname, "rune");
354         }
355         curse = find(curse, classname, "curse");
356         while(curse)
357         {
358                 if(curse.owner == pl)
359                         ccount = ccount + 1;
360                 curse = find(curse, classname, "curse");
361         }
362
363         numtodrop = cvar("g_runematch_drop_runes_max");
364         prevent_same = !cvar("g_runematch_allow_same");
365
366         rune = curse = world;
367         do
368         {
369                 rune = find(rune, classname, "rune");
370                 if(!rune)
371                         break;
372                 if(rune.owner != pl)
373                         continue;
374
375
376                 // find a random curse
377                 tries = 15;
378                 if(ccount > 1 && prevent_same)
379                 {
380                         // avoid pairing runes and curses that match each other
381                         do{
382                                 rand = ceil(random()*ccount);
383                                 curse = FindRune(pl, "curse", rand);
384                                 tries = tries - 1;
385                         }while(RuneMatchesCurse(rune.runes, curse.runes) && tries > 0);
386                         if(tries <= 0)
387                         {
388                                 bprint("warning: couldn't prevent same rune\n");
389                         }
390                 }
391                 else
392                 {
393                                 rand = ceil(random()*ccount);
394                                 curse = FindRune(pl, "curse", rand);
395                 }
396
397                 if(!curse)
398                         error("Couldn't fine curse to bind rune to\n");
399
400                 // pair rune and curse
401
402                 rune1 = rune;
403                 curse1 = curse;
404                 rune2 = curse1.enemy;
405                 curse2 = rune1.enemy;
406
407                 if(rune1 != rune2) // not already attached to each other
408                 {
409                         rune1.enemy = curse1;
410                         curse1.enemy = rune1;
411                         setattachment(curse1, rune1, "");
412                         rune2.enemy = curse2;
413                         curse2.enemy = rune2;
414                         setattachment(curse2, rune2, "");
415                         //DropRune(pl, rune2);
416                         //ccount = ccount - 1;
417                         //rcount = rcount - 1;
418                 }
419                 DropRune(pl, rune1);
420
421                 if(numtodrop <=0)
422                 {
423                         rune1.think = rune_respawn;
424                         rune1.nextthink = time;
425                 }
426
427                 numtodrop = numtodrop - 1;
428
429                 ccount = ccount - 1;
430                 rcount = rcount - 1;
431
432         }while(rune);
433 }
434
435 void spawn_default_runespawnpoints()
436 {
437         entity spot, e;
438         spot = find(world, classname, "info_player_deathmatch");
439         while(spot)
440         {
441                 e = spawn();
442                 e.classname = "runematch_spawn_point";
443                 e.origin = spot.origin;
444                 spot = find(spot, classname, "info_player_deathmatch");
445         }
446 }
447
448 void spawn_runes()
449 {
450         float r, num, max_num, rn, cs, numrunes, runes_left, curses_left, tries, prevent_same;
451         entity e, spot;
452
453         if(self)
454                 remove(self);
455
456         // fixme: instead of placing them all now, why not
457         // simply create them all and let them call rune_respawn() as their think?
458
459         runes_left  = RUNE_STRENGTH | RUNE_DEFENSE | RUNE_REGEN | RUNE_SPEED | RUNE_VAMPIRE;
460         curses_left = CURSE_WEAK | CURSE_VULNER | CURSE_VENOM | CURSE_SLOW | CURSE_EMPATHY;
461         numrunes = 5;
462         max_num = num = count_rune_spawnpoints();
463
464         if(num < numrunes)
465         {
466                 spawn_default_runespawnpoints();
467         }
468
469         prevent_same = !cvar("g_runematch_allow_same");
470
471         max_num = num = count_rune_spawnpoints();
472
473         if(num < numrunes)
474                 error(strcat("not enough spawn points for runematch, need ", ftos(numrunes), " but found ", ftos(num), "\n"));
475
476         while(numrunes > 0)
477         {
478                 r = ceil(random()*numrunes);
479                 if(r == 1)
480                         rn = RUNE_STRENGTH;
481                 else if(r == 2)
482                         rn = RUNE_DEFENSE;
483                 else if(r == 3)
484                         rn = RUNE_REGEN;
485                 else if(r == 4)
486                         rn = RUNE_SPEED;
487                 else
488                         rn = RUNE_VAMPIRE;
489
490                 if(curses_left > 1 && prevent_same)
491                 {
492                         tries = 15;
493                         // avoid pairing runes and curses that match each other
494                         do{
495                                 r = ceil(random()*numrunes);
496                                 if(r == 1)
497                                         cs = CURSE_WEAK;
498                                 else if(r == 2)
499                                         cs = CURSE_VULNER;
500                                 else if(r == 3)
501                                         cs = CURSE_VENOM;
502                                 else if(r == 4)
503                                         cs = CURSE_SLOW;
504                                 else
505                                         cs = CURSE_EMPATHY;
506                                 tries = tries - 1;
507                         }while(RuneMatchesCurse(rn, cs) && tries > 0);
508                         if(tries <= 0)
509                         {
510                                 bprint("warning: couldn't prevent same rune\n");
511                         }
512                 }
513                 else
514                 {
515                         r = ceil(random()*numrunes);
516                         if(r == 1)
517                                 cs = CURSE_WEAK;
518                         else if(r == 2)
519                                 cs = CURSE_VULNER;
520                         else if(r == 3)
521                                 cs = CURSE_VENOM;
522                         else if(r == 4)
523                                 cs = CURSE_SLOW;
524                         else
525                                 cs = CURSE_EMPATHY;
526                 }
527
528
529
530                 if(num <= 1)
531                         r = 1;
532                 else
533                         r = ceil(random()*num*1.25);
534
535                 spot = rune_find_spawnpoint(num, r);
536
537                 if(spot == world)
538                 {
539                         error("failed to find runematch spawnpoint!\n");
540                 }
541
542                 // choose and spawn rune
543
544 /*              //rn = RUNE_FIRST;
545                 while(!runes_left & rn && rn <= RUNE_LAST)
546                         rn = rn * 2;
547                 if(rn > RUNE_LAST)
548                         error("couldn't select rune\n");
549                 runes_left = runes_left - rn;
550
551                 //cs = CURSE_FIRST;
552                 while(!curses_left & cs && cs <= CURSE_LAST)
553                         cs = cs * 2;
554                 if(cs > CURSE_LAST)
555                         error("couldn't select rune\n");
556                 curses_left = curses_left - cs;
557 */
558                 while(!runes_left & rn)
559                 {
560                         rn = rn * 2;
561                         if(rn > RUNE_LAST)
562                                 rn = RUNE_FIRST;
563                 }
564                 runes_left = runes_left - rn;
565
566                 while(!curses_left & cs)
567                 {
568                         cs = cs * 2;
569                         if(cs > CURSE_LAST)
570                                 cs = CURSE_FIRST;
571                 }
572                 curses_left = curses_left - cs;
573
574                 e = spawn();
575                 e.runes = rn;
576                 e.classname = "rune";
577                 e.touch = rune_touch;
578                 e.think = rune_respawn;
579                 e.nextthink = time + random();
580                 e.movetype = MOVETYPE_TOSS;
581                 e.solid = SOLID_TRIGGER;
582                 e.flags = FL_ITEM;
583                 setmodel(e, "models/runematch/rune.mdl"); // precision set below
584                 setorigin(e, spot.origin);
585                 setsize(e, '0 0 -35', '0 0 0');
586
587                 e.enemy = spawn();
588                 e.enemy.enemy = e;
589                 e.enemy.classname = "curse";
590                 e.enemy.runes = cs;
591                 //e.enemy.avelocity = '300 500 200';
592                 setmodel(e.enemy, "models/runematch/curse.mdl"); // precision set below
593                 setorigin(e, '0 0 0');
594                 setattachment(e.enemy, e, "");
595
596
597                 e.colormod = RuneColormod(rn);
598                 e.enemy.colormod = RuneColormod(cs);
599
600                 e.alpha = e.enemy.alpha = cvar("g_runematch_rune_alpha");//0.78;
601                 e.effects = e.enemy.effects = cvar("g_runematch_rune_effects") | EF_LOWPRECISION;//EF_ADDITIVE;// | EF_FULLBRIGHT;
602
603                 //e.glow_size = e.enemy.glow_size = cvar("g_runematch_rune_glow_size");
604                 //e.glow_color = e.enemy.glow_color = cvar("g_runematch_rune_glow_color");
605
606                 // this spot is taken
607                 spot.owner = e;
608                 e.owner = spot;
609
610
611                 //rn = RUNE_FIRST;
612                 //cs = CURSE_FIRST;
613
614                 numrunes = numrunes - 1;
615                 num = num - 1;
616         }
617 }
618
619 void runematch_init()
620 {
621         if(!g_runematch)
622                 return;
623
624         entity e;
625         e = spawn();
626         e.think = spawn_runes;
627         e.nextthink = time + 0.1;
628 }
629
630
631 float runematch_point_time;
632
633 // give points to players who are holding runes
634 void RuneMatchGivePoints()
635 {
636         entity rune;
637
638         if(!g_runematch || !cvar("g_runematch_pointamt"))
639                 return;
640
641         if(gameover)
642                 return;
643
644         if(runematch_point_time > time)
645                 return;
646
647         runematch_point_time = time + cvar("g_runematch_pointrate");
648
649         rune = world;
650         do
651         {
652                 rune = find(rune, classname, "rune");
653                 if(!rune)
654                         return;
655
656                 if(rune.owner.classname == "player")
657                 {
658                         UpdateFrags(rune.owner, cvar("g_runematch_pointamt"));
659                 }
660         }while(rune);
661 }
662
663 float RunematchHandleFrags(entity attacker, entity targ, float f)
664 {
665         entity head;
666         float arunes, trunes, newfrags;
667
668         if(f <= 0)
669                 return f;
670         if(attacker == targ)
671                 return f;
672
673         arunes = trunes = 0;
674
675         head = find(world, classname, "rune");
676         while(head)
677         {
678                 if(head.owner == attacker)
679                 {
680                         arunes = arunes + 1;
681                 }
682                 else if(head.owner == targ)
683                 {
684                         trunes = trunes + 1;
685                 }
686
687                 head = find(head, classname, "rune");
688         }
689
690         if(!arunes && !trunes)
691                 return f - 1 + cvar("g_runematch_frags_norune"); // don't give points to players when no runes are involved.
692
693         if(arunes)
694         {       // got a kill while holding runes
695                 newfrags = newfrags + cvar("g_runematch_frags_killedby_runeholder");//5;
696         }
697         if(trunes)
698         {       // killed an enemy holding runes
699                 newfrags = newfrags + cvar("g_runematch_frags_killed_runeholder");//5;
700         }
701         if(newfrags)
702                 f = f - 1 + newfrags;
703
704         return f;
705 }