]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
Improvements to Keepaway (move scorerules to ka file, split declarations)
[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(other.classname == "spectator") 
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                 makevectors(self.angles);
31                 self.movetype = MOVETYPE_BOUNCE;
32                 self.velocity = '0 0 200';
33                 self.angles = '0 0 0';
34                 self.effects = autocvar_g_keepawayball_effects;
35                 self.think = ka_RespawnBall;
36                 self.nextthink = time + autocvar_g_keepawayball_respawntime;
37                 
38                 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
39                 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
40
41                 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, FALSE, RADARICON_FLAGCARRIER, '0 1 1');
42                 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);    
43
44                 sound(self, CH_TRIGGER, "keepaway/respawn.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
45         }
46         else
47         {
48                 ka_RespawnBall(); // finding a location failed, retry 
49         }
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.classname != "player") 
75         {  // The ball just touched an object, most likely the world
76                 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
77                 sound(self, CH_TRIGGER, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
78                 return; 
79         }
80         else if(self.wait > time) { return; }
81
82         // attach the ball to the player
83         self.owner = other;
84         other.ballcarried = self;
85         setattachment(self, other, "");
86         setorigin(self, '0 0 0');
87         
88         // make the ball invisible/unable to do anything/set up time scoring
89         self.velocity = '0 0 0';
90         self.movetype = MOVETYPE_NONE;
91         self.effects |= EF_NODRAW;
92         self.touch = SUB_Null;
93         self.think = ka_TimeScoring;
94         self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
95         self.takedamage = DAMAGE_NO;
96
97         // apply effects to player
98         other.glow_color = autocvar_g_keepawayball_trail_color;
99         other.glow_trail = TRUE;
100         other.effects |= autocvar_g_keepaway_ballcarrier_effects;
101         
102         // messages and sounds
103         ka_EventLog("pickup", other);
104         Send_KillNotification(other.netname, "", "", KA_PICKUPBALL, MSG_KA);
105         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
106         WriteString(MSG_BROADCAST, strcat(other.netname, "^7 has picked up the ball!"));
107         sound(self.owner, CH_TRIGGER, "keepaway/pickedup.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
108         
109         // scoring
110         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
111
112         // waypoints
113         WaypointSprite_AttachCarrier("ka-ballcarrier", other, RADARICON_FLAGCARRIER, '1 0 0');
114         other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
115         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
116         WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);   
117         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
118 }
119
120 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
121 {
122         entity ball;
123         ball = plyr.ballcarried;
124
125         if(!ball) { return; }
126         
127         // reset the ball
128         setattachment(ball, world, "");
129         ball.movetype = MOVETYPE_BOUNCE;
130         ball.wait = time + 1; 
131         ball.touch = ka_TouchEvent;
132         ball.think = ka_RespawnBall;
133         ball.nextthink = time + autocvar_g_keepawayball_respawntime;
134         ball.takedamage = DAMAGE_YES;
135         ball.effects &~= EF_NODRAW; 
136         setorigin(ball, plyr.origin + '0 0 10');
137         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
138         ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P 
139         ball.owner = world;
140         
141         // reset the player effects
142         plyr.glow_trail = FALSE;
143         plyr.effects &~= autocvar_g_keepaway_ballcarrier_effects;
144
145         // messages and sounds
146         ka_EventLog("dropped", plyr);
147         Send_KillNotification(plyr.netname, "", "", KA_DROPBALL, MSG_KA);
148         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
149         WriteString(MSG_BROADCAST, strcat(plyr.netname, "^7 has dropped the ball!"));
150         sound(other, CH_TRIGGER, "keepaway/dropped.wav", VOL_BASE, ATTN_NONE);  // ATTN_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) && (self.owner.classname == "player"))
165                 ka_DropEvent(self.owner);
166
167         ka_RespawnBall();
168 }
169
170
171 // ==============
172 // Hook Functions
173 // ==============
174
175 MUTATOR_HOOKFUNCTION(ka_Scoring)
176 {
177         if((frag_attacker != frag_target) && (frag_attacker.classname == "player"))
178         {
179                 if(frag_target.ballcarried) { // add to amount of times killing carrier
180                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
181                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
182                                 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
183                 }
184                 else if(!frag_attacker.ballcarried)
185                         if(autocvar_g_keepaway_noncarrier_warn)
186                                 centerprint(frag_attacker, "Killing people while you don't have the ball gives no points!");
187
188                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
189                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
190         }
191
192         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
193         return 0;
194 }
195
196 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
197 {
198         frag_score = 0; // no frags counted in keepaway
199         return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count. 
200 }
201
202 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
203 {
204         // clear the item used for the ball in keepaway
205         self.items &~= IT_KEY1;
206         
207         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
208         if(self.ballcarried)
209                 self.items |= IT_KEY1;
210
211         return 0;
212 }
213
214 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
215 {
216         if(MUTATOR_RETURNVALUE == 0)
217         if(self.ballcarried)
218         {
219                 ka_DropEvent(self);
220                 return 1;
221         }
222         return 0;
223 }
224
225 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
226 {
227         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
228         {
229                 if(frag_target == frag_attacker) // damage done to yourself
230                 {
231                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
232                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
233                 }
234                 else // damage done to noncarriers
235                 {
236                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
237                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
238                 }
239         }
240         else if not(frag_target.ballcarried) // if the target is a noncarrier
241         {
242                 if(frag_target == frag_attacker) // damage done to yourself
243                 {
244                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
245                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
246                 }
247                 else // damage done to other noncarriers
248                 {
249                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
250                         frag_force *= autocvar_g_keepaway_noncarrier_force;
251                 }
252         }
253         return 0;
254 }
255
256 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
257 {
258         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
259         return 0;
260 }
261
262 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
263 {
264         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
265         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player() 
266         
267         self.effects &~= autocvar_g_keepaway_ballcarrier_effects;
268
269         if(self.ballcarried)
270                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
271         
272         return 0;
273 }
274
275
276 // ==============
277 // Initialization
278 // ==============
279
280 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
281 {
282         if(!g_keepaway) { return; }
283         
284         entity e;
285         e = spawn();
286         e.model = "models/orbs/orbblue.md3";    
287         precache_model(e.model);
288         setmodel(e, e.model);
289         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
290         e.classname = "keepawayball";
291         e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
292         e.takedamage = DAMAGE_YES;
293         e.solid = SOLID_TRIGGER;
294         e.movetype = MOVETYPE_BOUNCE;
295         e.glow_color = autocvar_g_keepawayball_trail_color;
296         e.glow_trail = TRUE;
297         e.flags = FL_ITEM;
298         e.reset = ka_Reset;
299         e.touch = ka_TouchEvent;
300         e.owner = world;
301         ka_ball = e;
302
303         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So. 
304 }
305
306 void ka_ScoreRules()
307 {
308         ScoreRules_basics(0, SFL_SORT_PRIO_PRIMARY, 0, TRUE); // SFL_SORT_PRIO_PRIMARY
309         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_PICKUPS,                     "pickups",              0);
310         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_CARRIERKILLS,        "bckills",              0);
311         ScoreInfo_SetLabel_PlayerScore(SP_KEEPAWAY_BCTIME,                      "bctime",               SFL_SORT_PRIO_SECONDARY);
312         ScoreRules_basics_end();
313 }
314
315 void ka_Initialize() // run at the start of a match, initiates game mode
316 {
317         if(!g_keepaway)
318                 return;
319                 
320         precache_sound("keepaway/pickedup.wav");
321         precache_sound("keepaway/dropped.wav");
322         precache_sound("keepaway/respawn.wav");
323         precache_sound("keepaway/touch.wav");
324
325         ka_ScoreRules();
326         ka_SpawnBall();
327 }
328
329
330 MUTATOR_DEFINITION(gamemode_keepaway)
331 {
332         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
333         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
334         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
335         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
336         MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_ANY);
337         MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
338         MUTATOR_HOOK(PlayerPowerups, ka_PlayerPowerups, CBC_ORDER_ANY);
339         MUTATOR_HOOK(PlayerUseKey, ka_PlayerUseKey, CBC_ORDER_ANY);
340
341         MUTATOR_ONADD
342         {
343                 if(time > 1) // game loads at time 1
344                         error("This is a game type and it cannot be added at runtime.");
345                 ka_Initialize();
346         }
347
348         MUTATOR_ONREMOVE
349         {
350                 error("This is a game type and it cannot be removed at runtime.");
351         }
352
353         return 0;
354 }