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