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