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