]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
Mario scored against another gamemode specific check
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_keepaway.qc
1 #include "gamemode_keepaway.qh"
2
3 #include "gamemode.qh"
4
5 int autocvar_g_keepaway_ballcarrier_effects;
6 float autocvar_g_keepaway_ballcarrier_damage;
7 float autocvar_g_keepaway_ballcarrier_force;
8 float autocvar_g_keepaway_ballcarrier_highspeed;
9 float autocvar_g_keepaway_ballcarrier_selfdamage;
10 float autocvar_g_keepaway_ballcarrier_selfforce;
11 float autocvar_g_keepaway_noncarrier_damage;
12 float autocvar_g_keepaway_noncarrier_force;
13 float autocvar_g_keepaway_noncarrier_selfdamage;
14 float autocvar_g_keepaway_noncarrier_selfforce;
15 bool autocvar_g_keepaway_noncarrier_warn;
16 int autocvar_g_keepaway_score_bckill;
17 int autocvar_g_keepaway_score_killac;
18 int autocvar_g_keepaway_score_timepoints;
19 float autocvar_g_keepaway_score_timeinterval;
20 float autocvar_g_keepawayball_damageforcescale;
21 int autocvar_g_keepawayball_effects;
22 float autocvar_g_keepawayball_respawntime;
23 int autocvar_g_keepawayball_trail_color;
24
25 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame
26 {
27         if(e.ballcarried)
28                 if(IS_SPEC(other))
29                         return false; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
30
31         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
32
33         return true;
34 }
35
36 void ka_EventLog(string mode, entity actor) // use an alias for easy changing and quick editing later
37 {
38         if(autocvar_sv_eventlog)
39                 GameLogEcho(strcat(":ka:", mode, ((actor != world) ? (strcat(":", ftos(actor.playerid))) : "")));
40 }
41
42 void ka_TouchEvent();
43 void ka_RespawnBall() // runs whenever the ball needs to be relocated
44 {SELFPARAM();
45         if(gameover) { return; }
46         vector oldballorigin = self.origin;
47
48         if(!MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
49         {
50                 entity spot = SelectSpawnPoint(true);
51                 setorigin(self, spot.origin);
52                 self.angles = spot.angles;
53         }
54
55         makevectors(self.angles);
56         self.movetype = MOVETYPE_BOUNCE;
57         self.velocity = '0 0 200';
58         self.angles = '0 0 0';
59         self.effects = autocvar_g_keepawayball_effects;
60         self.touch = ka_TouchEvent;
61         self.think = ka_RespawnBall;
62         self.nextthink = time + autocvar_g_keepawayball_respawntime;
63
64         Send_Effect(EFFECT_ELECTRO_COMBO, oldballorigin, '0 0 0', 1);
65         Send_Effect(EFFECT_ELECTRO_COMBO, self.origin, '0 0 0', 1);
66
67         WaypointSprite_Spawn(WP_KaBall, 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
68         WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
69
70         sound(self, CH_TRIGGER, SND_KA_RESPAWN, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
71 }
72
73 void ka_TimeScoring()
74 {SELFPARAM();
75         if(self.owner.ballcarried)
76         { // add points for holding the ball after a certain amount of time
77                 if(autocvar_g_keepaway_score_timepoints)
78                         PlayerScore_Add(self.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
79
80                 PlayerScore_Add(self.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
81                 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
82         }
83 }
84
85 void ka_TouchEvent() // runs any time that the ball comes in contact with something
86 {SELFPARAM();
87         if(gameover) { return; }
88         if(!self) { return; }
89         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
90         { // The ball fell off the map, respawn it since players can't get to it
91                 ka_RespawnBall();
92                 return;
93         }
94         if(other.deadflag != DEAD_NO) { return; }
95         if(other.frozen) { return; }
96         if (!IS_PLAYER(other))
97         {  // The ball just touched an object, most likely the world
98                 Send_Effect(EFFECT_BALL_SPARKS, self.origin, '0 0 0', 1);
99                 sound(self, CH_TRIGGER, SND_KA_TOUCH, VOL_BASE, ATTEN_NORM);
100                 return;
101         }
102         else if(self.wait > time) { return; }
103
104         // attach the ball to the player
105         self.owner = other;
106         other.ballcarried = self;
107         setattachment(self, other, "");
108         setorigin(self, '0 0 0');
109
110         // make the ball invisible/unable to do anything/set up time scoring
111         self.velocity = '0 0 0';
112         self.movetype = MOVETYPE_NONE;
113         self.effects |= EF_NODRAW;
114         self.touch = func_null;
115         self.think = ka_TimeScoring;
116         self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
117         self.takedamage = DAMAGE_NO;
118
119         // apply effects to player
120         other.glow_color = autocvar_g_keepawayball_trail_color;
121         other.glow_trail = true;
122         other.effects |= autocvar_g_keepaway_ballcarrier_effects;
123
124         // messages and sounds
125         ka_EventLog("pickup", other);
126         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_PICKUP, other.netname);
127         Send_Notification(NOTIF_ALL_EXCEPT, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP, other.netname);
128         Send_Notification(NOTIF_ONE, other, MSG_CENTER, CENTER_KEEPAWAY_PICKUP_SELF);
129         sound(self.owner, CH_TRIGGER, SND_KA_PICKEDUP, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
130
131         // scoring
132         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
133
134         // waypoints
135         WaypointSprite_AttachCarrier(WP_KaBallCarrier, other, RADARICON_FLAGCARRIER);
136         other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
137         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
138         WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);
139         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
140 }
141
142 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
143 {
144         entity ball;
145         ball = plyr.ballcarried;
146
147         if(!ball) { return; }
148
149         // reset the ball
150         setattachment(ball, world, "");
151         ball.movetype = MOVETYPE_BOUNCE;
152         ball.wait = time + 1;
153         ball.touch = ka_TouchEvent;
154         ball.think = ka_RespawnBall;
155         ball.nextthink = time + autocvar_g_keepawayball_respawntime;
156         ball.takedamage = DAMAGE_YES;
157         ball.effects &= ~EF_NODRAW;
158         setorigin(ball, plyr.origin + '0 0 10');
159         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
160         ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P
161         ball.owner = world;
162
163         // reset the player effects
164         plyr.glow_trail = false;
165         plyr.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
166
167         // messages and sounds
168         ka_EventLog("dropped", plyr);
169         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_KEEPAWAY_DROPPED, plyr.netname);
170         Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_KEEPAWAY_DROPPED, plyr.netname);
171         sound(other, CH_TRIGGER, SND_KA_DROPPED, VOL_BASE, ATTEN_NONE); // ATTEN_NONE (it's a sound intended to be heard anywhere)
172
173         // scoring
174         // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
175
176         // waypoints
177         WaypointSprite_Spawn(WP_KaBall, 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER);
178         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
179         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
180         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
181 }
182
183 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
184 {SELFPARAM();
185         if((self.owner) && (IS_PLAYER(self.owner)))
186                 ka_DropEvent(self.owner);
187
188         if(time < game_starttime)
189         {
190                 self.think = ka_RespawnBall;
191                 self.touch = func_null;
192                 self.nextthink = game_starttime;
193         }
194         else
195                 ka_RespawnBall();
196 }
197
198
199 // ================
200 // Bot player logic
201 // ================
202
203 void havocbot_goalrating_ball(float ratingscale, vector org)
204 {SELFPARAM();
205         float t;
206         entity ball_owner;
207         ball_owner = ka_ball.owner;
208
209         if (ball_owner == self)
210                 return;
211
212         // If ball is carried by player then hunt them down.
213         if (ball_owner)
214         {
215                 t = (self.health + self.armorvalue) / (ball_owner.health + ball_owner.armorvalue);
216                 navigation_routerating(ball_owner, t * ratingscale, 2000);
217         }
218         else // Ball has been dropped so collect.
219                 navigation_routerating(ka_ball, ratingscale, 2000);
220 }
221
222 void havocbot_role_ka_carrier()
223 {SELFPARAM();
224         if (self.deadflag != DEAD_NO)
225                 return;
226
227         if (time > self.bot_strategytime)
228         {
229                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
230
231                 navigation_goalrating_start();
232                 havocbot_goalrating_items(10000, self.origin, 10000);
233                 havocbot_goalrating_enemyplayers(20000, self.origin, 10000);
234                 //havocbot_goalrating_waypoints(1, self.origin, 1000);
235                 navigation_goalrating_end();
236         }
237
238         if (!self.ballcarried)
239         {
240                 self.havocbot_role = havocbot_role_ka_collector;
241                 self.bot_strategytime = 0;
242         }
243 }
244
245 void havocbot_role_ka_collector()
246 {SELFPARAM();
247         if (self.deadflag != DEAD_NO)
248                 return;
249
250         if (time > self.bot_strategytime)
251         {
252                 self.bot_strategytime = time + autocvar_bot_ai_strategyinterval;
253
254                 navigation_goalrating_start();
255                 havocbot_goalrating_items(10000, self.origin, 10000);
256                 havocbot_goalrating_enemyplayers(1000, self.origin, 10000);
257                 havocbot_goalrating_ball(20000, self.origin);
258                 navigation_goalrating_end();
259         }
260
261         if (self.ballcarried)
262         {
263                 self.havocbot_role = havocbot_role_ka_carrier;
264                 self.bot_strategytime = 0;
265         }
266 }
267
268
269 // ==============
270 // Hook Functions
271 // ==============
272
273 MUTATOR_HOOKFUNCTION(ka, PlayerDies)
274 {SELFPARAM();
275         if((frag_attacker != frag_target) && (IS_PLAYER(frag_attacker)))
276         {
277                 if(frag_target.ballcarried) { // add to amount of times killing carrier
278                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
279                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
280                                 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
281                 }
282                 else if(!frag_attacker.ballcarried)
283                         if(autocvar_g_keepaway_noncarrier_warn)
284                                 Send_Notification(NOTIF_ONE_ONLY, frag_attacker, MSG_CENTER, CENTER_KEEPAWAY_WARN);
285
286                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
287                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
288         }
289
290         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
291         return 0;
292 }
293
294 MUTATOR_HOOKFUNCTION(ka, GiveFragsForKill)
295 {
296         frag_score = 0; // no frags counted in keepaway
297         return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count.
298 }
299
300 MUTATOR_HOOKFUNCTION(ka, PlayerPreThink)
301 {SELFPARAM();
302         // clear the item used for the ball in keepaway
303         self.items &= ~IT_KEY1;
304
305         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
306         if(self.ballcarried)
307                 self.items |= IT_KEY1;
308
309         return 0;
310 }
311
312 MUTATOR_HOOKFUNCTION(ka, PlayerUseKey)
313 {SELFPARAM();
314         if(MUTATOR_RETURNVALUE == 0)
315         if(self.ballcarried)
316         {
317                 ka_DropEvent(self);
318                 return 1;
319         }
320         return 0;
321 }
322
323 MUTATOR_HOOKFUNCTION(ka, PlayerDamage_Calculate) // for changing damage and force values that are applied to players in g_damage.qc
324 {
325         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
326         {
327                 if(frag_target == frag_attacker) // damage done to yourself
328                 {
329                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
330                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
331                 }
332                 else // damage done to noncarriers
333                 {
334                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
335                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
336                 }
337         }
338         else if (!frag_target.ballcarried) // if the target is a noncarrier
339         {
340                 if(frag_target == frag_attacker) // damage done to yourself
341                 {
342                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
343                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
344                 }
345                 else // damage done to other noncarriers
346                 {
347                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
348                         frag_force *= autocvar_g_keepaway_noncarrier_force;
349                 }
350         }
351         return 0;
352 }
353
354 MUTATOR_HOOKFUNCTION(ka, ClientDisconnect)
355 {SELFPARAM();
356         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
357         return 0;
358 }
359
360 MUTATOR_HOOKFUNCTION(ka, MakePlayerObserver)
361 {SELFPARAM();
362         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
363         return 0;
364 }
365
366 MUTATOR_HOOKFUNCTION(ka, PlayerPowerups)
367 {SELFPARAM();
368         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
369         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player()
370
371         self.effects &= ~autocvar_g_keepaway_ballcarrier_effects;
372
373         if(self.ballcarried)
374                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
375
376         return 0;
377 }
378
379 .float stat_sv_airspeedlimit_nonqw;
380 .float stat_sv_maxspeed;
381
382 MUTATOR_HOOKFUNCTION(ka, PlayerPhysics)
383 {SELFPARAM();
384         if(self.ballcarried)
385         {
386                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_keepaway_ballcarrier_highspeed;
387                 self.stat_sv_maxspeed *= autocvar_g_keepaway_ballcarrier_highspeed;
388         }
389         return false;
390 }
391
392 MUTATOR_HOOKFUNCTION(ka, BotShouldAttack)
393 {SELFPARAM();
394         // if neither player has ball then don't attack unless the ball is on the ground
395         if(!checkentity.ballcarried && !self.ballcarried && ka_ball.owner)
396                 return true;
397         return false;
398 }
399
400 MUTATOR_HOOKFUNCTION(ka, HavocBot_ChooseRole)
401 {SELFPARAM();
402         if (self.ballcarried)
403                 self.havocbot_role = havocbot_role_ka_carrier;
404         else
405                 self.havocbot_role = havocbot_role_ka_collector;
406         return true;
407 }
408
409
410 // ==============
411 // Initialization
412 // ==============
413
414 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
415 {
416         if(!g_keepaway) { return; }
417
418         entity e;
419         e = spawn();
420         e.model = "models/orbs/orbblue.md3";
421         precache_model(e.model);
422         _setmodel(e, e.model);
423         setsize(e, '-16 -16 -20', '16 16 20'); // 20 20 20 was too big, player is only 16 16 24... gotta cheat with the Z (20) axis so that the particle isn't cut off
424         e.classname = "keepawayball";
425         e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
426         e.takedamage = DAMAGE_YES;
427         e.solid = SOLID_TRIGGER;
428         e.movetype = MOVETYPE_BOUNCE;
429         e.glow_color = autocvar_g_keepawayball_trail_color;
430         e.glow_trail = true;
431         e.flags = FL_ITEM;
432         e.reset = ka_Reset;
433         e.touch = ka_TouchEvent;
434         e.owner = world;
435         ka_ball = e;
436
437         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So.
438 }
439
440 void ka_ScoreRules()
441 {
442         ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, true); // SFL_SORT_PRIO_PRIMARY
443         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS,                     "pickups",              0);
444         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS,        "bckills",              0);
445         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME,                      "bctime",               SFL_SORT_PRIO_SECONDARY);
446         ScoreRules_basics_end();
447 }
448
449 void ka_Initialize() // run at the start of a match, initiates game mode
450 {
451         if(!g_keepaway)
452                 return;
453
454         ka_ScoreRules();
455         ka_SpawnBall();
456 }
457
458
459 REGISTER_MUTATOR(ka, g_keepaway)
460 {
461         MUTATOR_ONADD
462         {
463                 if(time > 1) // game loads at time 1
464                         error("This is a game type and it cannot be added at runtime.");
465                 ka_Initialize();
466         }
467
468         MUTATOR_ONROLLBACK_OR_REMOVE
469         {
470                 // we actually cannot roll back ka_Initialize here
471                 // BUT: we don't need to! If this gets called, adding always
472                 // succeeds.
473         }
474
475         MUTATOR_ONREMOVE
476         {
477                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
478                 return -1;
479         }
480
481         return 0;
482 }