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