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