Code cleanups + keepaway powerups, this one might be questionable and might require...
[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 void ka_Initialize() // run at the start of a match, initiates game mode
7 {
8         if(!g_keepaway)
9                 return;
10                 
11         precache_sound("keepaway/pickedup.wav");
12         precache_sound("keepaway/dropped.wav");
13         precache_sound("keepaway/respawn.wav");
14         precache_sound("keepaway/touch.wav");
15
16         ScoreRules_keepaway();
17         ka_SpawnBall();
18 }
19
20 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
21 {
22         if(self.owner)
23                 if(self.owner.classname == "player")
24                         ka_DropEvent(self.owner);
25
26         ka_RespawnBall();
27 }
28
29 void ka_SpawnBall() // loads various values for the ball
30 {
31         if(!g_keepaway) { return; }
32         
33         entity e;
34         e = spawn();
35         e.model = "models/orbs/orbblue.md3";    
36         e.scale = 1;
37         precache_model(e.model);
38         setmodel(e, e.model);
39         setsize(e, BALL_MINS, BALL_MAXS);
40         e.classname = "keepawayball";
41         e.damageforcescale = cvar("g_keepawayball_damageforcescale");
42         e.takedamage = DAMAGE_YES;
43         e.glow_color = cvar("g_keepawayball_trail_color");
44         e.glow_trail = TRUE;
45         e.movetype = MOVETYPE_BOUNCE;
46         e.touch = ka_TouchEvent;
47         e.flags = FL_ITEM;
48         e.reset = ka_Reset;
49         e.owner = world;
50
51         InitializeEntity(e, ka_RespawnBall, INITPRIO_SETLOCATION); // is this the right priority? Neh, I have no idea.. Well-- it works! So. 
52 }
53
54 void ka_RespawnBall() // runs whenever the ball needs to be relocated
55 {
56         vector oldballorigin = self.origin;
57
58         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
59         {
60                 makevectors(self.angles);
61                 self.movetype = MOVETYPE_BOUNCE;
62                 self.velocity = '0 0 200';
63                 self.angles = '0 0 0';
64                 self.solid = SOLID_TRIGGER;
65                 self.think = ka_RespawnBall;
66                 self.nextthink = time + cvar("g_keepawayball_respawntime");
67                 
68                 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
69                 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
70
71                 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, FALSE);
72                 WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '0 1 1');
73                 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);    
74
75                 sound(self, CHAN_AUTO, "keepaway/respawn.wav", VOL_BASE, ATTN_NONE);
76         }
77         else
78         {
79                 ka_RespawnBall(); // finding a location failed, retry 
80         }
81 }
82
83 void ka_TouchEvent() // runs any time that the ball comes in contact with something
84 {
85         if(!self) { return; }
86         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
87         { // The ball fell off the map, respawn it since players can't get to it
88                 ka_RespawnBall();
89                 return;
90         }
91         if(other.deadflag != DEAD_NO) { return; }
92         if(other.classname != "player") 
93         {  // The ball just touched an object, most likely the world
94                 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
95                 sound(self, CHAN_AUTO, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
96                 return; 
97         }
98         if(self.wait > time) { return; }
99
100         // attach the ball to the player
101         self.owner = other;
102         other.ballcarried = self;
103         setattachment(self, other, "");
104         setorigin(self, BALL_ATTACHORG);
105         
106         // make the ball invisible/unable to do anything
107         self.velocity = '0 0 0';
108         self.movetype = MOVETYPE_NONE;
109         self.touch = SUB_Null;
110         self.effects |= EF_NODRAW;
111         self.think = SUB_Null;
112         self.nextthink = 0;
113         self.takedamage = DAMAGE_NO;
114
115         // apply effects to player
116         other.glow_color = cvar("g_keepawayball_trail_color");
117         other.glow_trail = TRUE;
118         other.effects |= 8;
119         other.alpha = 0.6;
120
121         // messages and prints
122         bprint(other.netname, "^7 has picked up the ball!\n");
123         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
124         WriteString(MSG_BROADCAST, strcat("\n\n", other.netname, "^7 has picked up the ball!\n"));
125         sound(self.owner, CHAN_AUTO, "keepaway/pickedup.wav", VOL_BASE, ATTN_NONE);
126         
127         // scoring
128         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
129
130         // waypoints
131         WaypointSprite_AttachCarrier("ka-ballcarrier", other);
132         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
133         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
134         WaypointSprite_Ping(other.waypointsprite_attachedforcarrier);   
135         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
136 }
137
138 void ka_DropEvent(entity plyr) // runs any time that a player is supposed to lose the ball
139 {
140         entity ball;
141         ball = plyr.ballcarried;
142
143         if(!ball) { return; }
144         
145         // reset the ball
146         setattachment(ball, world, "");
147         ball.movetype = MOVETYPE_BOUNCE;
148         ball.solid = SOLID_TRIGGER;
149         ball.wait = time + 1; 
150         ball.think = ka_RespawnBall;
151         ball.nextthink = time + cvar("g_keepawayball_respawntime");
152         ball.touch = ka_TouchEvent;
153         ball.takedamage = DAMAGE_YES;
154         ball.effects &~= EF_NODRAW; 
155         setorigin(ball, plyr.origin + '0 0 10');
156         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
157         ball.owner.ballcarried = world;
158         ball.owner = world;
159         
160         // reset the player effects
161         plyr.effects &~= 8;
162         plyr.alpha = 1.0;
163         plyr.glow_trail = FALSE;
164         
165         // messages and prints
166         bprint(plyr.netname, "^7 has dropped the ball!\n");
167         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
168         WriteString(MSG_BROADCAST, strcat("\n\n", plyr.netname, "^7 has dropped the ball!\n"));
169         sound(other, CHAN_AUTO, "keepaway/dropped.wav", VOL_BASE, ATTN_NONE);   
170         
171         // scoring
172         PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1);
173         
174         // waypoints
175         WaypointSprite_Spawn("ka-ball", 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, FALSE);
176         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
177         WaypointSprite_UpdateTeamRadar(ball.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '0 1 1');
178         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);    
179         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
180 }
181
182 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
183 {
184         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has left the match, drop it
185         return 0;
186 }
187
188 MUTATOR_HOOKFUNCTION(ka_Scoring)
189 {
190         if((frag_attacker != frag_target) && (frag_attacker.classname == "player"))
191         {
192                 if(frag_target.ballcarried) { // add to amount of times killing carrier
193                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
194                         if(cvar("g_keepaway_bckillscore")) // add bckills to the score
195                                 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
196                 }
197                 else if(!frag_attacker.ballcarried)
198                         if(cvar("g_keepaway_noncarrier_warn"))
199                                 centerprint_atprio(frag_attacker, (CENTERPRIO_SPAM + 5), "Killing people while you don't have the ball gives no points!");
200
201                 if(frag_attacker.ballcarried) // add to amount of kills while ballcarrier
202                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
203         }
204
205         if(self.ballcarried) { ka_DropEvent(self); } // a player with the ball has died, drop it
206         return 0;
207 }
208
209 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
210 {
211         frag_score = 0; // no frags counted in keepaway
212         return 0;
213 }
214
215 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
216 {
217         self.items &~= IT_KEY1;
218
219         if(self.ballcarried)
220                 self.items |= IT_KEY1;
221         
222         if(self.BUTTON_USE)
223                 if(self.ballcarried) { ka_DropEvent(self); } // drop the ball if the player presses the use button
224
225         return 0;
226 }
227
228 MUTATOR_HOOKFUNCTION(ka_PlayerDamage) // for changing damage and force values that are applied to players in g_damage.qc
229 {
230         if(frag_attacker.items & IT_KEY1) // if the attacker is a ballcarrier
231         {
232                 if(frag_target == frag_attacker) // damage done to yourself
233                 {
234                         frag_damage *= cvar("g_keepaway_ballcarrier_selfdamage");
235                         frag_force *= cvar("g_keepaway_ballcarrier_selfforce");
236                 }
237                 else // damage done to noncarriers
238                 {
239                         frag_damage *= cvar("g_keepaway_ballcarrier_damage");
240                         frag_force *= cvar("g_keepaway_ballcarrier_force");
241                 }
242         }
243         else if not(frag_target.items & IT_KEY1) // if the target is a noncarrier
244         {
245                 if(frag_target == frag_attacker) // damage done to yourself
246                 {
247                         frag_damage *= cvar("g_keepaway_noncarrier_selfdamage");
248                         frag_force *= cvar("g_keepaway_noncarrier_selfforce");
249                 }
250                 else // damage done to other noncarriers
251                 {
252                         frag_damage *= cvar("g_keepaway_noncarrier_damage");
253                         frag_force *= cvar("g_keepaway_noncarrier_force");
254                 }
255         }
256         return 0;
257 }
258
259 MUTATOR_DEFINITION(gamemode_keepaway)
260 {
261         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
262         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
263         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
264         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_ANY);
265         MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_FIRST);
266         MUTATOR_HOOK(PlayerDamage_Calculate, ka_PlayerDamage, CBC_ORDER_ANY);
267
268         MUTATOR_ONADD
269         {
270                 if(time > 1) // game loads at time 1
271                         error("This is a game type and it cannot be added at runtime.");
272                 g_keepaway = 1;
273                 ka_Initialize();
274         }
275
276         MUTATOR_ONREMOVE
277         {
278                 g_keepaway = 0;
279                 error("This is a game type and it cannot be removed at runtime.");
280         }
281
282         return 0;
283 }