]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
6ec91706b78f71e77833280f55db8b33a611ecb0
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_keepaway.qc
1 void ka_SpawnBall(void);
2 void ka_TouchEvent(void);
3 void ka_RespawnBall(void);
4 void ka_DropEvent(entity);
5 void ka_TimeScoring(void);
6
7 entity ka_ball;
8
9 float ka_ballcarrier_waypointsprite_visible_for_player(entity);
10
11 void ka_Initialize() // run at the start of a match, initiates game mode
12 {
13         if(!g_keepaway)
14                 return;
15                 
16         precache_sound("keepaway/pickedup.wav");
17         precache_sound("keepaway/dropped.wav");
18         precache_sound("keepaway/respawn.wav");
19         precache_sound("keepaway/touch.wav");
20
21         ScoreRules_keepaway();
22         ka_SpawnBall();
23 }
24
25 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
26 {
27         if(self.owner)
28                 if(self.owner.classname == "player")
29                         ka_DropEvent(self.owner);
30
31         ka_RespawnBall();
32 }
33
34 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
35 {
36         if(!g_keepaway) { return; }
37         
38         entity e;
39         e = spawn();
40         e.model = "models/orbs/orbblue.md3";    
41         precache_model(e.model);
42         setmodel(e, e.model);
43         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
44         e.classname = "keepawayball";
45         e.damageforcescale = autocvar_g_keepawayball_damageforcescale;
46         e.takedamage = DAMAGE_YES;
47         e.solid = SOLID_TRIGGER;
48         e.movetype = MOVETYPE_BOUNCE;
49         e.glow_color = autocvar_g_keepawayball_trail_color;
50         e.glow_trail = TRUE;
51         e.flags = FL_ITEM;
52         e.reset = ka_Reset;
53         e.touch = ka_TouchEvent;
54         e.owner = world;
55         ka_ball = e;
56
57         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So. 
58 }
59
60 void ka_RespawnBall() // runs whenever the ball needs to be relocated
61 {
62         if(gameover) { return; }
63         vector oldballorigin = self.origin;
64         
65         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
66         {
67                 makevectors(self.angles);
68                 self.movetype = MOVETYPE_BOUNCE;
69                 self.velocity = '0 0 200';
70                 self.angles = '0 0 0';
71                 self.effects = autocvar_g_keepawayball_effects;
72                 self.think = ka_RespawnBall;
73                 self.nextthink = time + autocvar_g_keepawayball_respawntime;
74                 
75                 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
76                 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
77
78                 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, FALSE, RADARICON_FLAGCARRIER, '0 1 1');
79                 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);    
80
81                 sound(self, CHAN_AUTO, "keepaway/respawn.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
82         }
83         else
84         {
85                 ka_RespawnBall(); // finding a location failed, retry 
86         }
87 }
88
89 void ka_TouchEvent() // runs any time that the ball comes in contact with something
90 {
91         if(gameover) { return; }
92         if(!self) { return; }
93         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
94         { // The ball fell off the map, respawn it since players can't get to it
95                 ka_RespawnBall();
96                 return;
97         }
98         if(other.deadflag != DEAD_NO) { return; }
99         if(other.classname != "player") 
100         {  // The ball just touched an object, most likely the world
101                 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
102                 sound(self, CHAN_AUTO, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
103                 return; 
104         }
105         else if(self.wait > time) { return; }
106
107         // attach the ball to the player
108         self.owner = other;
109         other.ballcarried = self;
110         setattachment(self, other, "");
111         setorigin(self, '0 0 0');
112         
113         // make the ball invisible/unable to do anything/set up time scoring
114         self.velocity = '0 0 0';
115         self.movetype = MOVETYPE_NONE;
116         self.effects |= EF_NODRAW;
117         self.touch = SUB_Null;
118         self.think = ka_TimeScoring;
119         self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
120         self.takedamage = DAMAGE_NO;
121
122         // apply effects to player
123         other.glow_color = autocvar_g_keepawayball_trail_color;
124         other.glow_trail = TRUE;
125         other.effects |= autocvar_g_keepaway_ballcarrier_effects;
126         
127         // messages and sounds
128         Send_KillNotification(other.netname, "", "", KA_PICKUPBALL, MSG_KA);
129         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
130         WriteString(MSG_BROADCAST, strcat("\n\n", other.netname, "^7 has picked up the ball!\n"));
131         sound(self.owner, CHAN_AUTO, "keepaway/pickedup.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
132         
133         // scoring
134         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
135
136         // waypoints
137         WaypointSprite_AttachCarrier("ka-ballcarrier", other);
138         other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
139         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
140         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
141         WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);   
142         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
143 }
144
145 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
146 {
147         entity ball;
148         ball = plyr.ballcarried;
149
150         if(!ball) { return; }
151         
152         // reset the ball
153         setattachment(ball, world, "");
154         ball.movetype = MOVETYPE_BOUNCE;
155         ball.wait = time + 1; 
156         ball.touch = ka_TouchEvent;
157         ball.think = ka_RespawnBall;
158         ball.nextthink = time + autocvar_g_keepawayball_respawntime;
159         ball.takedamage = DAMAGE_YES;
160         ball.effects &~= EF_NODRAW; 
161         setorigin(ball, plyr.origin + '0 0 10');
162         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
163         ball.owner.ballcarried = world; // I hope nothing checks to see if the world has the ball in the rest of my code :P 
164         ball.owner = world;
165         
166         // reset the player effects
167         plyr.glow_trail = FALSE;
168         plyr.effects &~= autocvar_g_keepaway_ballcarrier_effects;
169
170         // messages and sounds
171         Send_KillNotification(plyr.netname, "", "", KA_DROPBALL, MSG_KA);
172         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
173         WriteString(MSG_BROADCAST, strcat("\n\n", plyr.netname, "^7 has dropped the ball!\n"));
174         sound(other, CHAN_AUTO, "keepaway/dropped.wav", VOL_BASE, ATTN_NONE);   // ATTN_NONE (it's a sound intended to be heard anywhere) 
175         
176         // scoring
177         // PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1); Not anymore, this is 100% the same as pickups and is useless.
178         
179         // waypoints
180         WaypointSprite_Spawn("ka-ball", 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, FALSE, RADARICON_FLAGCARRIER, '0 1 1');
181         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
182         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);    
183         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
184 }
185
186 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame 
187 {
188         if(e.ballcarried)
189                 if(other.classname == "spectator") 
190                         return FALSE; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
191                 
192         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
193
194         return TRUE;
195 }
196
197 void ka_TimeScoring()
198 {
199         if(self.owner.ballcarried)
200         { // add points for holding the ball after a certain amount of time
201                 if(autocvar_g_keepaway_score_timepoints)
202                         PlayerScore_Add(self.owner, SP_SCORE, autocvar_g_keepaway_score_timepoints);
203                         
204                 PlayerScore_Add(self.owner, SP_KEEPAWAY_BCTIME, (autocvar_g_keepaway_score_timeinterval / 1)); // interval is divided by 1 so that time always shows "seconds"
205                 self.nextthink = time + autocvar_g_keepaway_score_timeinterval;
206         }
207 }
208
209 MUTATOR_HOOKFUNCTION(ka_Scoring)
210 {
211         if((frag_attacker != frag_target) && (frag_attacker.classname == "player"))
212         {
213                 if(frag_target.ballcarried) { // add to amount of times killing carrier
214                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
215                         if(autocvar_g_keepaway_score_bckill) // add bckills to the score
216                                 PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_bckill);
217                 }
218                 else if(!frag_attacker.ballcarried)
219                         if(autocvar_g_keepaway_noncarrier_warn)
220                                 centerprint_atprio(frag_attacker, (CENTERPRIO_SPAM + 5), "Killing people while you don't have the ball gives no points!");
221
222                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
223                         PlayerScore_Add(frag_attacker, SP_SCORE, autocvar_g_keepaway_score_killac);
224         }
225
226         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
227         return 0;
228 }
229
230 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
231 {
232         frag_score = 0; // no frags counted in keepaway
233         return 1; // you deceptive little bugger ;3 This needs to be true in order for this function to even count. 
234 }
235
236 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
237 {
238         // clear the item used for the ball in keepaway
239         self.items &~= IT_KEY1;
240         
241         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
242         if(self.ballcarried)
243                 self.items |= IT_KEY1;
244
245         return 0;
246 }
247
248 MUTATOR_HOOKFUNCTION(ka_PlayerUseKey)
249 {
250         if(MUTATOR_RETURNVALUE == 0)
251         if(self.ballcarried)
252         {
253                 ka_DropEvent(self);
254                 return 1;
255         }
256         return 0;
257 }
258
259 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
260 {
261         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
262         {
263                 if(frag_target == frag_attacker) // damage done to yourself
264                 {
265                         frag_damage *= autocvar_g_keepaway_ballcarrier_selfdamage;
266                         frag_force *= autocvar_g_keepaway_ballcarrier_selfforce;
267                 }
268                 else // damage done to noncarriers
269                 {
270                         frag_damage *= autocvar_g_keepaway_ballcarrier_damage;
271                         frag_force *= autocvar_g_keepaway_ballcarrier_force;
272                 }
273         }
274         else if not(frag_target.ballcarried) // if the target is a noncarrier
275         {
276                 if(frag_target == frag_attacker) // damage done to yourself
277                 {
278                         frag_damage *= autocvar_g_keepaway_noncarrier_selfdamage;
279                         frag_force *= autocvar_g_keepaway_noncarrier_selfforce;
280                 }
281                 else // damage done to other noncarriers
282                 {
283                         frag_damage *= autocvar_g_keepaway_noncarrier_damage;
284                         frag_force *= autocvar_g_keepaway_noncarrier_force;
285                 }
286         }
287         return 0;
288 }
289
290 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
291 {
292         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
293         return 0;
294 }
295
296 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
297 {
298         // In the future this hook is supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
299         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player() 
300         
301         self.effects &~= autocvar_g_keepaway_ballcarrier_effects;
302
303         if(self.ballcarried)
304                 self.effects |= autocvar_g_keepaway_ballcarrier_effects;
305         
306         return 0;
307 }
308
309 MUTATOR_DEFINITION(gamemode_keepaway)
310 {
311         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
312         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
313         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
314         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
315         MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_ANY);
316         MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
317         MUTATOR_HOOK(PlayerPowerups, ka_PlayerPowerups, CBC_ORDER_ANY);
318         MUTATOR_HOOK(PlayerUseKey, ka_PlayerUseKey, CBC_ORDER_ANY);
319
320         MUTATOR_ONADD
321         {
322                 if(time > 1) // game loads at time 1
323                         error("This is a game type and it cannot be added at runtime.");
324                 g_keepaway = 1;
325                 ka_Initialize();
326         }
327
328         MUTATOR_ONREMOVE
329         {
330                 g_keepaway = 0;
331                 error("This is a game type and it cannot be removed at runtime.");
332         }
333
334         return 0;
335 }