]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_keepaway.qc
Update the powerups function a little so that it actually functions in a slightly...
[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
6 float ka_ballcarrier_waypointsprite_visible_for_player(entity);
7
8 void ka_Initialize() // run at the start of a match, initiates game mode
9 {
10         if(!g_keepaway)
11                 return;
12                 
13         precache_sound("keepaway/pickedup.wav");
14         precache_sound("keepaway/dropped.wav");
15         precache_sound("keepaway/respawn.wav");
16         precache_sound("keepaway/touch.wav");
17
18         ScoreRules_keepaway();
19         ka_SpawnBall();
20 }
21
22 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
23 {
24         if(self.owner)
25                 if(self.owner.classname == "player")
26                         ka_DropEvent(self.owner);
27
28         ka_RespawnBall();
29 }
30
31 void ka_SpawnBall() // loads various values for the ball, runs only once at start of match
32 {
33         if(!g_keepaway) { return; }
34         
35         entity e;
36         e = spawn();
37         e.model = "models/orbs/orbblue.md3";    
38         e.scale = 1;
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 = cvar("g_keepawayball_damageforcescale");
44         e.takedamage = DAMAGE_YES;
45         e.glow_color = cvar("g_keepawayball_trail_color");
46         e.glow_trail = TRUE;
47         e.movetype = MOVETYPE_BOUNCE;
48         e.touch = ka_TouchEvent;
49         e.flags = FL_ITEM;
50         e.reset = ka_Reset;
51         e.owner = world;
52
53         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So. 
54 }
55
56 void ka_RespawnBall() // runs whenever the ball needs to be relocated
57 {
58         vector oldballorigin = self.origin;
59
60         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
61         {
62                 makevectors(self.angles);
63                 self.movetype = MOVETYPE_BOUNCE;
64                 self.velocity = '0 0 200';
65                 self.angles = '0 0 0';
66                 self.solid = SOLID_TRIGGER;
67                 self.think = ka_RespawnBall;
68                 self.nextthink = time + cvar("g_keepawayball_respawntime");
69                 
70                 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
71                 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
72
73                 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, FALSE);
74                 WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '0 1 1');
75                 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);    
76
77                 sound(self, CHAN_AUTO, "keepaway/respawn.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
78         }
79         else
80         {
81                 ka_RespawnBall(); // finding a location failed, retry 
82         }
83 }
84
85 void ka_TouchEvent() // runs any time that the ball comes in contact with something
86 {
87         if(!self) { return; }
88         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
89         { // The ball fell off the map, respawn it since players can't get to it
90                 ka_RespawnBall();
91                 return;
92         }
93         if(other.deadflag != DEAD_NO) { return; }
94         if(other.classname != "player") 
95         {  // The ball just touched an object, most likely the world
96                 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
97                 sound(self, CHAN_AUTO, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
98                 return; 
99         }
100         else if(self.wait > time) { return; }
101
102         // attach the ball to the player
103         self.owner = other;
104         other.ballcarried = self;
105         setattachment(self, other, "");
106         setorigin(self, '3 0 20'); // wtf why is this not '0 0 0' ? 
107         
108         // make the ball invisible/unable to do anything
109         self.velocity = '0 0 0';
110         self.movetype = MOVETYPE_NONE;
111         self.touch = SUB_Null;
112         self.effects |= EF_NODRAW;
113         self.think = SUB_Null;
114         self.nextthink = 0;
115         self.takedamage = DAMAGE_NO;
116
117         // apply effects to player
118         other.glow_color = cvar("g_keepawayball_trail_color");
119         other.glow_trail = TRUE;
120         other.effects |= EF_DIMLIGHT;
121         other.alpha = cvar("g_keepaway_ballcarrier_alpha");
122         other.exteriorweaponentity.alpha = cvar("g_keepaway_ballcarrier_alpha");
123
124         // messages and sounds
125         Send_KillNotification(other.netname, "", "", KA_PICKUPBALL, MSG_KA);
126         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
127         WriteString(MSG_BROADCAST, strcat("\n\n", other.netname, "^7 has picked up the ball!\n"));
128         sound(self.owner, CHAN_AUTO, "keepaway/pickedup.wav", VOL_BASE, ATTN_NONE); // ATTN_NONE (it's a sound intended to be heard anywhere) 
129         
130         // scoring
131         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
132
133         // waypoints
134         WaypointSprite_AttachCarrier("ka-ballcarrier", other);
135         other.waypointsprite_attachedforcarrier.waypointsprite_visible_for_player = ka_ballcarrier_waypointsprite_visible_for_player;
136         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
137         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
138         WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);   
139         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
140 }
141
142 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
143 {
144         entity ball;
145         ball = plyr.ballcarried;
146
147         if(!ball) { return; }
148         
149         // reset the ball
150         setattachment(ball, world, "");
151         ball.movetype = MOVETYPE_BOUNCE;
152         ball.solid = SOLID_TRIGGER; // is this needed? 
153         ball.wait = time + 1; 
154         ball.think = ka_RespawnBall;
155         ball.nextthink = time + cvar("g_keepawayball_respawntime");
156         ball.touch = ka_TouchEvent;
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 &~= EF_DIMLIGHT;
167         plyr.alpha = default_player_alpha;
168         plyr.exteriorweaponentity.alpha = default_weapon_alpha; 
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);
178         
179         // waypoints
180         WaypointSprite_Spawn("ka-ball", 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, FALSE);
181         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
182         WaypointSprite_UpdateTeamRadar(ball.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '0 1 1');
183         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);    
184         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
185 }
186
187 float ka_ballcarrier_waypointsprite_visible_for_player(entity e) // runs on waypoints which are attached to ballcarriers, updates once per frame 
188 {
189         if(e.ballcarried)
190                 if(other.classname == "spectator") 
191                         return FALSE; // we don't want spectators of the ballcarrier to see the attached waypoint on the top of their screen
192                 
193         // TODO: Make the ballcarrier lack a waypointsprite whenever they have the invisibility powerup
194
195         return TRUE;
196 }
197
198 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
199 {
200         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
201         return 0;
202 }
203
204 MUTATOR_HOOKFUNCTION(ka_Scoring)
205 {
206         if((frag_attacker != frag_target) && (frag_attacker.classname == "player"))
207         {
208                 if(frag_target.ballcarried) { // add to amount of times killing carrier
209                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
210                         if(cvar("g_keepaway_bckillscore")) // add bckills to the score
211                                 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
212                 }
213                 else if(!frag_attacker.ballcarried)
214                         if(cvar("g_keepaway_noncarrier_warn"))
215                                 centerprint_atprio(frag_attacker, (CENTERPRIO_SPAM + 5), "Killing people while you don't have the ball gives no points!");
216
217                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
218                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
219         }
220
221         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
222         return 0;
223 }
224
225 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
226 {
227         frag_score = 0; // no frags counted in keepaway
228         return 0;
229 }
230
231 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
232 {
233         // clear the item used for the ball in keepaway
234         self.items &~= IT_KEY1;
235         
236         // if the player has the ball, make sure they have the item for it (Used for HUD primarily)
237         if(self.ballcarried)
238                 self.items |= IT_KEY1;
239
240         // drop the ball if the player presses the use button
241         if(self.BUTTON_USE)
242                 if(self.ballcarried) { ka_DropEvent(self); } 
243
244         return 0;
245 }
246
247 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
248 {
249         if(frag_attacker.ballcarried) // if the attacker is a ballcarrier
250         {
251                 if(frag_target == frag_attacker) // damage done to yourself
252                 {
253                         frag_damage *= cvar("g_keepaway_ballcarrier_selfdamage");
254                         frag_force *= cvar("g_keepaway_ballcarrier_selfforce");
255                 }
256                 else // damage done to noncarriers
257                 {
258                         frag_damage *= cvar("g_keepaway_ballcarrier_damage");
259                         frag_force *= cvar("g_keepaway_ballcarrier_force");
260                 }
261         }
262         else if not(frag_target.ballcarried) // if the target is a noncarrier
263         {
264                 if(frag_target == frag_attacker) // damage done to yourself
265                 {
266                         frag_damage *= cvar("g_keepaway_noncarrier_selfdamage");
267                         frag_force *= cvar("g_keepaway_noncarrier_selfforce");
268                 }
269                 else // damage done to other noncarriers
270                 {
271                         frag_damage *= cvar("g_keepaway_noncarrier_damage");
272                         frag_force *= cvar("g_keepaway_noncarrier_force");
273                 }
274         }
275         return 0;
276 }
277
278 MUTATOR_HOOKFUNCTION(ka_PlayerPowerups)
279 {
280         // right now this hook doesn't make much sense (It's actually useless this way except for minstagib invisibility alpha) 
281         // but in the future it's supposed to allow me to do some extra stuff with waypointsprites and invisibility powerup
282         // So bare with me until I can fix a certain bug with ka_ballcarrier_waypointsprite_visible_for_player() 
283         
284         // also note that this structure makes no sense (Rather there is a better way to do it) the way it's currently applied
285         // again just bare with me as this is for a future feature. 
286
287         if(self.ballcarried)
288         { 
289                 // force the default ballcarrier alpha on the player if they have the ball
290                 self.alpha = cvar("g_keepaway_ballcarrier_alpha");
291                 self.exteriorweaponentity.alpha = cvar("g_keepaway_ballcarrier_alpha");
292         
293                 if(g_minstagib)
294                 {
295                         if(olditems & IT_STRENGTH) 
296                         { // if the player has the ball and they also have the invisibility powerup, apply alpha accordingly
297                                 self.alpha = g_minstagib_invis_alpha;
298                                 self.exteriorweaponentity.alpha = g_minstagib_invis_alpha;
299                         }
300                 }
301         }
302         else if(g_minstagib)
303         {
304                 // if we're in minstagib and a noncarrier has invisibility, assure that we apply the invisibility effects normally
305                 if(olditems & IT_STRENGTH) 
306                 {
307                         self.alpha = g_minstagib_invis_alpha;
308                         self.exteriorweaponentity.alpha = g_minstagib_invis_alpha;
309                 }
310         }
311         else
312         {
313                 // if we're a normal player with no powerups that edit alpha make sure the alpha is default. 
314                 // (normal powerups just use EF_ADDITIVE)
315                 self.alpha = default_player_alpha;
316                 self.exteriorweaponentity.alpha = default_weapon_alpha;
317         }
318         
319         return 0;
320 }
321
322 MUTATOR_DEFINITION(gamemode_keepaway)
323 {
324         // I don't quite understand these orders, perhaps someone could enlighten me?
325         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
326         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
327         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
328         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
329         MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_FIRST);
330         MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
331         MUTATOR_HOOK(PlayerPowerups, ka_PlayerPowerups, CBC_ORDER_ANY);
332
333         MUTATOR_ONADD
334         {
335                 if(time > 1) // game loads at time 1
336                         error("This is a game type and it cannot be added at runtime.");
337                 g_keepaway = 1;
338                 ka_Initialize();
339         }
340
341         MUTATOR_ONREMOVE
342         {
343                 g_keepaway = 0;
344                 error("This is a game type and it cannot be removed at runtime.");
345         }
346
347         return 0;
348 }