410326305a67096a149985b6fdf715fb91a5abec
[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 //.float dropperid;
6
7 void ka_Initialize() // run at the start of a match, initiates game mode
8 {
9         if(!g_keepaway)
10                 return;
11                 
12         precache_sound("keepaway/pickedup.wav");
13         precache_sound("keepaway/dropped.wav");
14         precache_sound("keepaway/respawn.wav");
15         precache_sound("keepaway/touch.wav");
16
17         ScoreRules_keepaway();
18         
19         //entity e; // Is this needed here? I think not
20         //e = spawn();
21         //e.think = ka_SpawnBall;
22         //e.nextthink = time;
23
24         //InitializeEntity(e, ka_SpawnBall, INITPRIO_SETLOCATION); // Is initializeentity needed here? I think not
25         ka_SpawnBall();
26 }
27
28 void ka_Reset() // used to clear the ballcarrier whenever the match switches from warmup to normal
29 {
30         if(self.owner)
31                 if(self.owner.classname == "player")
32                         ka_DropEvent(self.owner);
33
34         ka_RespawnBall();
35 }
36
37 void ka_SpawnBall() // loads various values for the ball
38 {
39         if(!g_keepaway) { return; }
40         
41         entity e;
42         e = spawn();
43         if (!e.model) { // is this needed? OF COURSE the model doesn't exist, the ball isn't on the map yet!
44                 e.model = "models/orbs/orbblue.md3";    
45                 e.scale = 1; }
46         precache_model(e.model);
47         setmodel(e, e.model);
48         setsize(e, BALL_MINS, BALL_MAXS);
49         ball_scale = e.scale;
50         e.classname = "keepawayball";
51         e.damageforcescale = cvar("g_keepawayball_damageforcescale");
52         e.takedamage = DAMAGE_YES;
53         //self.effects |= "sparks";
54         e.glow_color = cvar("g_keepawayball_trail_color");
55         e.glow_trail = TRUE;
56         e.movetype = MOVETYPE_BOUNCE;
57         e.touch = ka_TouchEvent;
58         e.think = ka_RespawnBall;
59         e.nextthink = time;
60         e.flags = FL_ITEM;
61         e.reset = ka_Reset;
62         e.owner = world;
63 }
64
65 void ka_RespawnBall() // runs whenever the ball needs to be relocated
66 {
67         vector oldballorigin = self.origin;
68
69         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
70         {
71                 makevectors(self.angles);
72                 self.movetype = MOVETYPE_BOUNCE;
73                 self.velocity = '0 0 200';
74                 self.angles = '0 0 0';
75                 self.solid = SOLID_TRIGGER;
76                 //self.touch = ka_TouchEvent;
77                 self.think = ka_RespawnBall;
78                 self.nextthink = time + cvar("g_keepawayball_respawntime");
79                 pointparticles(particleeffectnum("electro_combo"), oldballorigin, '0 0 0', 1);
80                 pointparticles(particleeffectnum("electro_combo"), self.origin, '0 0 0', 1);
81
82                 WaypointSprite_Spawn("ka-ball", 0, 0, self, '0 0 64', world, self.team, self, waypointsprite_attachedforcarrier, FALSE);
83                 WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '0 1 1');
84
85                 sound(self, CHAN_AUTO, "keepaway/respawn.wav", VOL_BASE, ATTN_NONE);
86         }
87         else
88         {
89                 // sorry, can't spawn, better luck next frame
90                 //self.think = ka_RespawnBall;
91                 //self.nextthink = time;
92                 ka_RespawnBall();
93         }
94 }
95
96 void ka_TouchEvent() // runs any time that the ball comes in contact with something
97 {
98         if(!self) { return; }
99         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
100         {
101                 self.think = ka_RespawnBall;
102                 self.nextthink = time;
103                 return;
104         }
105         if(other.deadflag != DEAD_NO) { return; }
106         if(other.classname != "player") 
107         {  // The ball just touched an object, most likely the world
108                 pointparticles(particleeffectnum("kaball_sparks"), self.origin, '0 0 0', 1);
109                 sound(self, CHAN_AUTO, "keepaway/touch.wav", VOL_BASE, ATTN_NORM);
110                 return; 
111         }
112         if(self.wait > time) { return; }
113
114         self.owner = other;
115         other.ballcarried = self;
116         setattachment(self, other, "");
117         setorigin(self, BALL_ATTACHORG);
118         
119         self.velocity = '0 0 0';
120         self.movetype = MOVETYPE_NONE;
121         self.touch = SUB_Null;
122         self.effects |= EF_NODRAW;
123         self.think = SUB_Null;
124         self.nextthink = 0;
125         self.takedamage = DAMAGE_NO;
126
127         other.glow_color = cvar("g_keepawayball_trail_color");
128         other.glow_trail = TRUE;
129         other.effects |= 8;
130         other.alpha = 0.6;
131
132         bprint(other.netname, "^7 has picked up the ball!\n");
133         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
134         WriteString(MSG_BROADCAST, strcat("\n\n", other.netname, "^7 has picked up the ball!\n"));
135         sound(self.owner, CHAN_AUTO, "keepaway/pickedup.wav", VOL_BASE, ATTN_NONE);
136         
137         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
138
139         WaypointSprite_AttachCarrier("ka-ballcarrier", other);
140         WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
141         WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
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         setattachment(ball, world, "");
154         ball.movetype = MOVETYPE_BOUNCE;
155         ball.solid = SOLID_TRIGGER;
156         ball.wait = time + 1; 
157         ball.think = ka_RespawnBall;
158         ball.nextthink = time + cvar("g_keepawayball_respawntime");
159         ball.touch = ka_TouchEvent;
160         ball.takedamage = DAMAGE_YES;
161         ball.effects &~= EF_NODRAW; //ball.alpha = 1.0;
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;
165         ball.owner = world;
166
167         plyr.effects &~= 8;
168         plyr.alpha = 1.0;
169         plyr.glow_trail = FALSE;
170         
171         bprint(plyr.netname, "^7 has dropped the ball!\n");
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);   
175         
176         PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1);
177         
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 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
186 {
187         if(self.ballcarried) { ka_DropEvent(self); }
188         return 1;
189 }
190
191 MUTATOR_HOOKFUNCTION(ka_Scoring)
192 {
193         if((frag_attacker != frag_target) && (frag_attacker.classname == "player"))
194         {
195                 if(frag_target.ballcarried) { // get amount of times killing carrier
196                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
197                         if(cvar("g_keepaway_bckillscore"))
198                                 PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
199                 }
200                 else if(!frag_attacker.ballcarried)
201                         if(cvar("g_keepaway_noncarrier_warn"))
202                                 centerprint_atprio(frag_attacker, (CENTERPRIO_SPAM + 5), "Killing people while you don't have the ball gives no points!");
203
204                 if(frag_attacker.ballcarried) // get kills as carrier
205                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
206         }
207
208         if(self.ballcarried) { ka_DropEvent(self); }
209         return 1;
210 }
211
212 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
213 {
214         frag_score = 0; // no frags counted in keepaway
215         return 1;
216 }
217
218 MUTATOR_HOOKFUNCTION(ka_PlayerPreThink)
219 {
220         self.items &~= IT_KEY1;
221
222         if(self.ballcarried)
223                 self.items |= IT_KEY1; 
224         
225         if(self.BUTTON_USE)
226                 if(self.ballcarried) { ka_DropEvent(self); }
227
228         return 1;
229 }
230
231 MUTATOR_DEFINITION(gamemode_keepaway)
232 {
233         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
234         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
235         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
236         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_FIRST);
237         MUTATOR_HOOK(PlayerPreThink, ka_PlayerPreThink, CBC_ORDER_FIRST);
238
239         MUTATOR_ONADD
240         {
241                 if(time > 1) // game loads at time 1
242                         error("This is a game type and it cannot be added at runtime.");
243                 g_keepaway = 1;
244                 ka_Initialize();
245         }
246
247         MUTATOR_ONREMOVE
248         {
249                 g_keepaway = 0;
250                 error("This is a game type and it cannot be removed at runtime.");
251         }
252
253         return 0;
254 }