Almost fully functioning now! Just a couple more things to do and then testing.
[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()
7 {
8         if(!g_keepaway)
9                 return;
10                 
11         precache_sound("keepaway/pickedup.wav");
12         precache_sound("keepaway/dropped.wav");
13
14         ScoreRules_keepaway();
15         
16         entity e;
17         e = spawn();
18         e.think = ka_SpawnBall;
19         e.nextthink = time;
20 }
21
22 void ka_SpawnBall() // self = the ball
23 {
24         if(!g_keepaway) { 
25                 remove(self); 
26                 return; 
27         }
28         if (!self.model) {
29                 self.model = "models/orbs/orbblue.md3"; 
30                 self.scale = 1;
31         }
32
33         precache_model(self.model);
34         setmodel(self, self.model);
35         setsize(self, BALL_MINS, BALL_MAXS);
36         ball_scale = self.scale;
37         self.classname = "keepawayball";
38         self.damageforcescale = cvar("g_keepawayball_damageforcescale");
39         self.effects = self.effects | EF_FULLBRIGHT;
40         self.movetype = MOVETYPE_BOUNCE;
41         self.touch = ka_TouchEvent;
42         self.think = ka_RespawnBall;
43         self.nextthink = time;
44         self.flags = FL_ITEM;
45         //self.reset = ka_Reset;
46         self.owner = world;
47         
48         WaypointSprite_AttachCarrier("ka-ball", self);
49         //WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
50         // wtf radar doesn't work when not in a team environment?
51 }
52
53 void ka_RespawnBall()
54 {
55         if(MoveToRandomMapLocation(self, DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, Q3SURFACEFLAG_SKY, 10, 1024, 256))
56         {
57                 makevectors(self.angles);
58                 self.movetype = MOVETYPE_BOUNCE;
59                 self.velocity = '0 0 200';
60                 self.angles = '0 0 0';
61                 self.solid = SOLID_TRIGGER;
62                 //self.touch = ka_TouchEvent;
63                 self.think = ka_RespawnBall;
64                 self.nextthink = time + cvar("g_keepawayball_respawntime");
65         }
66         else
67         {
68                 // sorry, can't spawn, better luck next frame
69                 self.think = ka_RespawnBall;
70                 self.nextthink = time;
71         }
72 }
73
74 void ka_TouchEvent()
75 {
76         if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT)
77         {
78                 self.think = ka_SpawnBall;
79                 self.nextthink = time;
80                 return;
81         }
82         if(!self) 
83                 return;
84         if(other.classname != "player" || other.health < 1)
85                 return;
86         if(self.wait > time)
87                 return;
88         //if(time > self.ctf_droptime + cvar("g_keepawayball_respawntime"))
89         //      return;
90
91         self.owner = other;
92         other.kaballcarried = self;
93         setattachment(self, other, "");
94         setorigin(self, BALL_ATTACHORG);
95         
96         self.velocity = '0 0 0';
97         self.movetype = MOVETYPE_NONE;
98         self.touch = SUB_Null;
99         self.alpha = 0.01;
100         
101         self.think = SUB_Null;
102         self.nextthink = 0;
103
104         self.glow_color = cvar("g_keepawayball_trail_color");
105         self.glow_trail = TRUE;
106         other.effects |= 8;
107         other.alpha = 0.6;
108
109         bprint(other.netname, "^7 has picked up the ball!\n");
110         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
111         WriteString(MSG_BROADCAST, strcat("\n\n", other.netname, "^7 has picked up the ball!\n"));
112         sound(self.owner, CHAN_AUTO, "keepaway/pickedup.wav", VOL_BASE, ATTN_NORM);
113         
114         PlayerScore_Add(other, SP_KEEPAWAY_PICKUPS, 1);
115
116         WaypointSprite_Kill(self.waypointsprite_attachedforcarrier);
117         WaypointSprite_AttachCarrier("ka-ballcarrier", other);
118         //WaypointSprite_UpdateRule(other.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
119         //WaypointSprite_UpdateTeamRadar(other.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, '1 0 0');
120 }
121
122 void ka_DropEvent(entity plyr) 
123 {
124         entity ball;
125         ball = plyr.kaballcarried;
126
127         if(!ball) { return; }
128         
129         setattachment(ball, world, "");
130         ball.movetype = MOVETYPE_BOUNCE;
131         ball.solid = SOLID_TRIGGER;
132         ball.wait = time + 1;
133         ball.ctf_droptime = time;
134         ball.think = ka_SpawnBall;
135         ball.nextthink = time + cvar("g_keepawayball_respawntime");
136         ball.touch = ka_TouchEvent;
137         plyr.effects = EF_LOWPRECISION;
138         plyr.alpha = 1.0;
139         ball.alpha = 1.0;
140         setorigin(ball, plyr.origin + '0 0 10');
141         ball.velocity = '0 0 200' + '0 100 0'*crandom() + '100 0 0'*crandom();
142
143         bprint(plyr.netname, "^7 has dropped the ball!\n");
144         WriteByte(MSG_BROADCAST, SVC_CENTERPRINT);
145         WriteString(MSG_BROADCAST, strcat("\n\n", plyr.netname, "^7 has dropped the ball!\n"));
146         sound(other, CHAN_AUTO, "keepaway/dropped.wav", VOL_BASE, ATTN_NORM);   
147         
148         PlayerScore_Add(plyr, SP_KEEPAWAY_DROPS, 1);
149         
150         // todo
151         WaypointSprite_Kill(plyr.waypointsprite_attachedforcarrier);
152         WaypointSprite_AttachCarrier("ka-ball", ball);
153         //WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
154         //WaypointSprite_UpdateTeamRadar(ball.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
155         //WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);  
156
157         ball.owner.kaballcarried = world;
158         ball.owner = world;
159 }
160
161 MUTATOR_HOOKFUNCTION(ka_RemovePlayer)
162 {
163         if(self.kaballcarried) { ka_DropEvent(self); }
164         return 1;
165 }
166
167 MUTATOR_HOOKFUNCTION(ka_Scoring)
168 {
169         if not(frag_attacker == frag_target)
170         {
171                 if(frag_target.kaballcarried) { // get amount of times killing carrier
172                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_CARRIERKILLS, 1);
173                 }
174                 else if not(frag_attacker.kaballcarried)
175                         if(cvar("g_keepaway_noncarrier_warn"))
176                                 centerprint_atprio(frag_attacker, (CENTERPRIO_SPAM + 5), "Killing people while you don't have the ball gives no points!");
177
178                 if(frag_attacker.kaballcarried) // get kills as carrier
179                         PlayerScore_Add(frag_attacker, SP_KEEPAWAY_SCORE, 1);
180         }
181
182         if(self.kaballcarried) { ka_DropEvent(self); }
183         return 1;
184 }
185
186 MUTATOR_HOOKFUNCTION(ka_GiveFragsForKill)
187 {
188         frag_score = 0; // no frags counted in keepaway
189         return 1;
190 }
191
192 MUTATOR_DEFINITION(gamemode_keepaway)
193 {
194         MUTATOR_HOOK(MakePlayerObserver, ka_RemovePlayer, CBC_ORDER_ANY);
195         MUTATOR_HOOK(ClientDisconnect, ka_RemovePlayer, CBC_ORDER_ANY);
196         MUTATOR_HOOK(PlayerDies, ka_Scoring, CBC_ORDER_ANY);
197         MUTATOR_HOOK(GiveFragsForKill, ka_GiveFragsForKill, CBC_ORDER_FIRST);
198
199         MUTATOR_ONADD
200         {
201                 if(time > 1) // game loads at time 1
202                         error("This is a game type and it cannot be added at runtime.");
203                 g_keepaway = 1;
204                 ka_Initialize();
205         }
206
207         MUTATOR_ONREMOVE
208         {
209                 g_keepaway = 0;
210                 error("This is a game type and it cannot be removed at runtime.");
211         }
212
213         return 0;
214 }
215