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