]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_nexball.qc
Turn nexball into a gamemode mutator
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_nexball.qc
1
2 MUTATOR_HOOKFUNCTION(nexball_BallDrop)
3 {
4     if(self.ballcarried && g_nexball)
5         DropBall(self.ballcarried, self.origin, self.velocity);
6
7         return 0;
8 }
9 void nb_delayedinit();
10
11 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsString)
12 {
13         ret_string = strcat(ret_string, ":NB");
14         return 0;
15 }
16
17 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsPrettyString)
18 {
19         ret_string = strcat(ret_string, ", NexBall");
20         return 0;
21 }
22 MUTATOR_DEFINITION(gamemode_nexball)
23 {
24         //MUTATOR_HOOK(MakePlayerObserver, kh_Key_DropAll, CBC_ORDER_ANY);
25     MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
26     MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
27     MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
28     MUTATOR_HOOK(BuildMutatorsPrettyString, nexball_BuildMutatorsPrettyString, CBC_ORDER_ANY);
29     MUTATOR_HOOK(BuildMutatorsString, nexball_BuildMutatorsString, CBC_ORDER_ANY);
30
31         MUTATOR_ONADD
32         {
33
34
35         
36         g_nexball_meter_period = autocvar_g_nexball_meter_period;
37         if (g_nexball_meter_period <= 0)
38             g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
39         g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
40         addstat(STAT_NB_METERSTART, AS_FLOAT, metertime);
41
42         // General settings
43         /*
44         CVTOV(g_nexball_football_boost_forward);   //100
45         CVTOV(g_nexball_football_boost_up);        //200
46         CVTOV(g_nexball_delay_idle);               //10
47         CVTOV(g_nexball_football_physics);         //0
48         */
49         radar_showennemies = autocvar_g_nexball_radar_showallplayers;
50
51         InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
52         }
53         
54         return 0;
55 }
56
57 float OtherTeam(float t)  //works only if there are two teams on the map!
58 {
59         entity e;
60         e = find(world, classname, "nexball_team");
61         if (e.team == t)
62                 e = find(e, classname, "nexball_team");
63         return e.team;
64 }
65
66 void ResetBall();
67
68 void LogNB(string mode, entity actor)
69 {
70         string s;
71         if(!autocvar_sv_eventlog)
72                 return;
73         s = strcat(":nexball:", mode);
74         if(actor != world)
75                 s = strcat(s, ":", ftos(actor.playerid));
76         GameLogEcho(s);
77 }
78
79 void ball_restart (void)
80 {
81         if(self.owner)
82                 DropBall(self, self.owner.origin, '0 0 0');
83         ResetBall();
84 }
85
86 void nexball_setstatus (void)
87 {
88         entity oldself;
89         self.items &~= IT_KEY1;
90         if (self.ballcarried)
91         {
92                 if (self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
93                 {
94                         bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
95                         oldself = self;
96                         self = self.ballcarried;
97                         DropBall(self, self.owner.origin, '0 0 0');
98                         ResetBall();
99                         self = oldself;
100                 } else
101                         self.items |= IT_KEY1;
102         }
103 }
104
105 void relocate_nexball (void)
106 {
107         tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, TRUE, self);
108         if (trace_startsolid)
109         {
110                 vector o;
111                 o = self.origin;
112                 if(!move_out_of_solid(self))
113                         objerror("could not get out of solid at all!");
114                 print("^1NOTE: this map needs FIXING. ", self.classname, " at ", vtos(o - '0 0 1'));
115                 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin_x - o_x));
116                 print(" ", ftos(self.origin_y - o_y));
117                 print(" ", ftos(self.origin_z - o_z), "'\n");
118                 self.origin = o;
119         }
120 }
121
122 void basketball_touch();
123 void football_touch();
124
125 void DropOwner (void)
126 {
127         entity ownr;
128         ownr = self.owner;
129         DropBall(self, ownr.origin, ownr.velocity);
130         makevectors(ownr.v_angle_y * '0 1 0');
131         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
132         ownr.flags &~= FL_ONGROUND;
133 }
134
135 void GiveBall (entity plyr, entity ball)
136 {
137         entity ownr;
138
139         if ((ownr = ball.owner))
140         {
141                 ownr.effects &~= autocvar_g_nexball_basketball_effects_default;
142                 ownr.ballcarried = world;
143                 if (ownr.metertime)
144                 {
145                         ownr.metertime = 0;
146                         ownr.weaponentity.state = WS_READY;
147                 }
148                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
149         }
150         else
151         {
152                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
153         }
154
155         setattachment(ball, plyr, "");
156         setorigin(ball, BALL_ATTACHORG);
157
158         if (ball.team != plyr.team)
159                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
160
161         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
162         ball.team = plyr.team;
163         plyr.ballcarried = ball;
164         ball.dropperid = plyr.playerid;
165
166         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
167         ball.effects &~= autocvar_g_nexball_basketball_effects_default;
168
169         ball.velocity = '0 0 0';
170         ball.movetype = MOVETYPE_NONE;
171         ball.touch = SUB_Null;
172         ball.effects |= EF_NOSHADOW;
173         ball.scale = 1; // scale down.
174
175         WaypointSprite_AttachCarrier("nb-ball", plyr, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
176         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
177
178         if (autocvar_g_nexball_basketball_delay_hold)
179         {
180                 ball.think = DropOwner;
181                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
182         }
183 }
184
185 void DropBall (entity ball, vector org, vector vel)
186 {
187         ball.effects |= autocvar_g_nexball_basketball_effects_default;
188         ball.effects &~= EF_NOSHADOW;
189         ball.owner.effects &~= autocvar_g_nexball_basketball_effects_default;
190
191         setattachment(ball, world, "");
192         setorigin (ball, org);
193         ball.movetype = MOVETYPE_BOUNCE;
194         ball.flags &~= FL_ONGROUND;
195         ball.scale = ball_scale;
196         ball.velocity = vel;
197         ball.ctf_droptime = time;
198         ball.touch = basketball_touch;
199         ball.think = ResetBall;
200         ball.nextthink = min(time + autocvar_g_nexball_delay_idle, ball.teamtime);
201
202         if (ball.owner.metertime)
203         {
204                 ball.owner.metertime = 0;
205                 ball.owner.weaponentity.state = WS_READY;
206         }
207
208         WaypointSprite_Kill(ball.owner.waypointsprite_attachedforcarrier);
209         WaypointSprite_Spawn("nb-ball", 0, 0, ball, '0 0 64', world, ball.team, ball, waypointsprite_attachedforcarrier, FALSE, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR); // no health bar please
210         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
211
212         ball.owner.ballcarried = world;
213         ball.owner = world;
214 }
215
216 void InitBall (void)
217 {
218         if (gameover) return;
219         self.flags &~= FL_ONGROUND;
220         self.movetype = MOVETYPE_BOUNCE;
221         if (self.classname == "nexball_basketball")
222                 self.touch = basketball_touch;
223         else if (self.classname == "nexball_football")
224                 self.touch = football_touch;
225         self.cnt = 0;
226         self.think = ResetBall;
227         self.nextthink = time + autocvar_g_nexball_delay_idle + 3;
228         self.teamtime = 0;
229         self.pusher = world;
230         self.team = FALSE;
231         sound (self, CH_TRIGGER, self.noise1, VOL_BASE, ATTN_NORM);
232         WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
233         LogNB("init", world);
234 }
235
236 void ResetBall (void)
237 {
238         if (self.cnt < 2) { // step 1
239                 if (time == self.teamtime)
240                         bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
241                 self.touch = SUB_Null;
242                 self.movetype = MOVETYPE_NOCLIP;
243                 self.velocity = '0 0 0'; // just in case?
244                 if(!self.cnt)
245                         LogNB("resetidle", world);
246                 self.cnt = 2;
247                 self.nextthink = time;
248         } else if (self.cnt < 4) { // step 2 and 3
249 //              dprint("Step ", ftos(self.cnt), ": Calculated velocity: ", vtos(self.spawnorigin - self.origin), ", time: ", ftos(time), "\n");
250                 self.velocity = (self.spawnorigin - self.origin) * (self.cnt - 1); // 1 or 0.5 second movement
251                 self.nextthink = time + 0.5;
252                 self.cnt += 1;
253         } else { // step 4
254 //              dprint("Step 4: time: ", ftos(time), "\n");
255                 if (vlen(self.origin - self.spawnorigin) > 10) // should not happen anymore
256                         dprint("The ball moved too far away from its spawn origin.\nOffset: ",
257                                vtos(self.origin - self.spawnorigin), " Velocity: ", vtos(self.velocity), "\n");
258                 self.velocity = '0 0 0';
259                 setorigin(self, self.spawnorigin); // make sure it's positioned correctly anyway
260                 self.movetype = MOVETYPE_NONE;
261                 self.think = InitBall;
262                 self.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
263         }
264 }
265
266 void football_touch (void)
267 {
268         if (other.solid == SOLID_BSP) {
269                 if (time > self.lastground + 0.1)
270                 {
271                         sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NORM);
272                         self.lastground = time;
273                 }
274                 if (vlen(self.velocity) && !self.cnt)
275                         self.nextthink = time + autocvar_g_nexball_delay_idle;
276                 return;
277         }
278         if (other.classname != "player")
279                 return;
280         if (other.health < 1)
281                 return;
282         if (!self.cnt)
283                 self.nextthink = time + autocvar_g_nexball_delay_idle;
284
285         self.pusher = other;
286         self.team = other.team;
287
288         if (autocvar_g_nexball_football_physics == -1) { // MrBougo try 1, before decompiling Rev's original
289                 if (vlen(other.velocity))
290                         self.velocity = other.velocity * 1.5 + '0 0 1' * autocvar_g_nexball_football_boost_up;
291         } else if (autocvar_g_nexball_football_physics == 1) { // MrBougo's modded Rev style: partially independant of the height of the aiming point
292                 makevectors(other.v_angle);
293                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + '0 0 1' * autocvar_g_nexball_football_boost_up;
294         } else if (autocvar_g_nexball_football_physics == 2) { // 2nd mod try: totally independant. Really playable!
295                 makevectors(other.v_angle_y * '0 1 0');
296                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
297         } else { // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
298                 makevectors(other.v_angle);
299                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
300         }
301         self.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
302 }
303
304 void basketball_touch (void)
305 {
306         if (other.ballcarried)
307         {
308                 football_touch();
309                 return;
310         }
311         if (!self.cnt && other.classname == "player" && (other.playerid != self.dropperid || time > self.ctf_droptime + autocvar_g_nexball_delay_collect)) {
312                 if (other.health <= 0)
313                         return;
314                 LogNB("caught", other);
315                 GiveBall(other, self);
316         } else if (other.solid == SOLID_BSP) {
317                 sound (self, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NORM);
318                 if (vlen(self.velocity) && !self.cnt)
319                         self.nextthink = min(time + autocvar_g_nexball_delay_idle, self.teamtime);
320         }
321 }
322
323 void GoalTouch (void)
324 {
325         entity ball;
326         float isclient, pscore, otherteam;
327         string pname;
328
329         if (gameover) return;
330         if ((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
331                 ball = other.ballcarried;
332         else
333                 ball = other;
334         if (ball.classname != "nexball_basketball")
335         if (ball.classname != "nexball_football")
336                 return;
337         if ((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
338                 return;
339         EXACTTRIGGER_TOUCH;
340
341
342         if(nb_teams == 2)
343                 otherteam = OtherTeam(ball.team);
344
345         if((isclient = ball.pusher.flags & FL_CLIENT))
346                 pname = ball.pusher.netname;
347         else
348                 pname = "Someone (?)";
349
350         if        (ball.team == self.team) //owngoal (regular goals)
351         {
352                 LogNB("owngoal", ball.pusher);
353                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
354                 pscore = -1;
355         } else if (self.team == GOAL_FAULT) {
356                 LogNB("fault", ball.pusher);
357                 if (nb_teams == 2)
358                         bprint(ColoredTeamName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
359                 else
360                         bprint(ColoredTeamName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
361                 pscore = -1;
362         } else if (self.team == GOAL_OUT) {
363                 LogNB("out", ball.pusher);
364                 if ((self.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
365                         bprint(pname, "^7 went out of bounds.\n");
366                 else
367                         bprint("The ball was returned.\n");
368                 pscore = 0;
369         } else {                           //score
370                 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
371                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", ColoredTeamName(ball.team), ".\n");
372                 pscore = 1;
373         }
374
375         sound (ball, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NONE);
376
377         if(ball.team && pscore)
378         {
379                 if (nb_teams == 2 && pscore < 0)
380                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
381                 else
382                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
383         }
384         if (isclient)
385         {
386                 if (pscore > 0)
387                         PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
388                 else if (pscore < 0)
389                         PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
390         }
391
392         if (ball.owner) // Happens on spawnflag GOAL_TOUCHPLAYER
393                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
394
395         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
396
397         ball.cnt = 1;
398         ball.think = ResetBall;
399         if (ball.classname == "nexball_basketball")
400                 ball.touch = football_touch; // better than SUB_Null: football control until the ball gets reset
401         ball.nextthink = time + autocvar_g_nexball_delay_goal * (self.team != GOAL_OUT);
402 }
403
404 //=======================//
405 //       team ents       //
406 //=======================//
407 void spawnfunc_nexball_team (void)
408 {
409         if(!g_nexball) { remove(self); return; }
410         self.team = self.cnt + 1;
411 }
412
413 void nb_spawnteam (string teamname, float teamcolor)
414 {
415         dprint("^2spawned team ", teamname, "\n");
416         entity e;
417         e = spawn();
418         e.classname = "nexball_team";
419         e.netname = teamname;
420         e.cnt = teamcolor;
421         e.team = e.cnt + 1;
422         nb_teams += 1;
423 }
424
425 void nb_spawnteams (void)
426 {
427         float t_r, t_b, t_y, t_p;
428         entity e;
429         for(e = world; (e = find(e, classname, "nexball_goal")); )
430         {
431                 switch(e.team)
432                 {
433                         case COLOR_TEAM1: if(!t_r) { nb_spawnteam ("Red", e.team-1)   ; t_r = 1; } break;
434                         case COLOR_TEAM2: if(!t_b) { nb_spawnteam ("Blue", e.team-1)  ; t_b = 1; } break;
435                         case COLOR_TEAM3: if(!t_y) { nb_spawnteam ("Yellow", e.team-1); t_y = 1; } break;
436                         case COLOR_TEAM4: if(!t_p) { nb_spawnteam ("Pink", e.team-1)  ; t_p = 1; } break;
437                 }
438         }
439 }
440
441 void nb_delayedinit (void)
442 {
443         if (find(world, classname, "nexball_team") == world)
444                 nb_spawnteams();
445         ScoreRules_nexball(nb_teams);
446 }
447
448
449 //=======================//
450 //      spawnfuncs       //
451 //=======================//
452
453 void SpawnBall (void)
454 {
455         if(!g_nexball) { remove(self); return; }
456
457 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
458
459         if (!self.model) {
460                 self.model = "models/nexball/ball.md3";
461                 self.scale = 1.3;
462         }
463
464         precache_model (self.model);
465         setmodel (self, self.model);
466         setsize (self, BALL_MINS, BALL_MAXS);
467         ball_scale = self.scale;
468
469         relocate_nexball();
470         self.spawnorigin = self.origin;
471
472         self.effects = self.effects | EF_LOWPRECISION;
473
474         if (cvar(strcat("g_", self.classname, "_trail"))) //nexball_basketball :p
475         {
476                 self.glow_color = autocvar_g_nexball_trail_color;
477                 self.glow_trail = TRUE;
478         }
479
480         self.movetype = MOVETYPE_FLY;
481
482         if (!autocvar_g_nexball_sound_bounce)
483                 self.noise = "";
484         else if (!self.noise)
485                 self.noise = "sound/nexball/bounce.wav";
486                 //bounce sound placeholder (FIXME)
487         if (!self.noise1)
488                 self.noise1 = "sound/nexball/drop.wav";
489                 //ball drop sound placeholder (FIXME)
490         if (!self.noise2)
491                 self.noise2 = "sound/nexball/steal.wav";
492                 //stealing sound placeholder (FIXME)
493         if (self.noise) precache_sound (self.noise);
494         precache_sound (self.noise1);
495         precache_sound (self.noise2);
496
497         WaypointSprite_AttachCarrier("nb-ball", self, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR); // the ball's team is not set yet, no rule update needed
498
499         self.reset = ball_restart;
500         self.think = InitBall;
501         self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
502 }
503
504 void spawnfunc_nexball_basketball (void)
505 {
506         self.classname = "nexball_basketball";
507         if not(balls & BALL_BASKET)
508         {
509                 /*
510                 CVTOV(g_nexball_basketball_effects_default);
511                 CVTOV(g_nexball_basketball_delay_hold);
512                 CVTOV(g_nexball_basketball_delay_hold_forteam);
513                 CVTOV(g_nexball_basketball_teamsteal);
514                 */
515                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
516         }
517         if (!self.effects)
518                 self.effects = autocvar_g_nexball_basketball_effects_default;
519         self.solid = SOLID_TRIGGER;
520         balls |= BALL_BASKET;
521         self.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
522         self.bouncestop = autocvar_g_nexball_basketball_bouncestop;
523         SpawnBall();
524 }
525
526 void spawnfunc_nexball_football (void)
527 {
528         self.classname = "nexball_football";
529         self.solid = SOLID_TRIGGER;
530         balls |= BALL_FOOT;
531         self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
532         self.bouncestop = autocvar_g_nexball_football_bouncestop;
533         SpawnBall();
534 }
535
536 void SpawnGoal (void)
537 {
538         if(!g_nexball) { remove(self); return; }
539         EXACTTRIGGER_INIT;
540         self.classname = "nexball_goal";
541         if (!self.noise)
542                 self.noise = "ctf/respawn.wav";
543         precache_sound(self.noise);
544         self.touch = GoalTouch;
545 }
546
547 void spawnfunc_nexball_redgoal (void)
548 {
549         self.team = COLOR_TEAM1;
550         SpawnGoal();
551 }
552 void spawnfunc_nexball_bluegoal (void)
553 {
554         self.team = COLOR_TEAM2;
555         SpawnGoal();
556 }
557 void spawnfunc_nexball_yellowgoal (void)
558 {
559         self.team = COLOR_TEAM3;
560         SpawnGoal();
561 }
562 void spawnfunc_nexball_pinkgoal (void)
563 {
564         self.team = COLOR_TEAM4;
565         SpawnGoal();
566 }
567
568 void spawnfunc_nexball_fault (void)
569 {
570         self.team = GOAL_FAULT;
571         if (!self.noise)
572                 self.noise = "misc/typehit.wav";
573         SpawnGoal();
574 }
575
576 void spawnfunc_nexball_out (void)
577 {
578         self.team = GOAL_OUT;
579         if (!self.noise)
580                 self.noise = "misc/typehit.wav";
581         SpawnGoal();
582 }
583
584 //
585 //Spawnfuncs preserved for compatibility
586 //
587
588 void spawnfunc_ball            (void) { spawnfunc_nexball_football(); }
589 void spawnfunc_ball_football   (void) { spawnfunc_nexball_football(); }
590 void spawnfunc_ball_basketball (void) { spawnfunc_nexball_basketball(); }
591 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
592 void spawnfunc_ball_redgoal    (void) { spawnfunc_nexball_bluegoal(); } // I blame Revenant
593 void spawnfunc_ball_bluegoal   (void) { spawnfunc_nexball_redgoal(); }  // but he didn't mean to cause trouble :p
594 void spawnfunc_ball_fault      (void) { spawnfunc_nexball_fault(); }
595 void spawnfunc_ball_bound      (void) { spawnfunc_nexball_out(); }
596
597 //=======================//
598 //      Weapon code      //
599 //=======================//
600
601 void W_Nexball_Touch (void)
602 {
603         entity ball, attacker;
604         attacker = self.owner;
605
606         PROJECTILE_TOUCH;
607         if(attacker.team != other.team || autocvar_g_nexball_basketball_teamsteal)
608         if((ball = other.ballcarried) && (attacker.classname == "player"))
609         {
610                 other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
611                 other.flags &~= FL_ONGROUND;
612                 if(!attacker.ballcarried)
613                 {
614                         LogNB("stole", attacker);
615                         sound (other, CH_TRIGGER, ball.noise2, VOL_BASE, ATTN_NORM);
616
617                         if(attacker.team == other.team && time > attacker.teamkill_complain)
618                         {
619                                 attacker.teamkill_complain = time + 5;
620                                 attacker.teamkill_soundtime = time + 0.4;
621                                 attacker.teamkill_soundsource = other;
622                         }
623
624                         GiveBall(attacker, other.ballcarried);
625                 }
626         }
627         remove(self);
628 }
629
630 void W_Nexball_Attack (float t)
631 {
632         entity ball;
633         float mul, mi, ma;
634         if (!(ball = self.ballcarried))
635                 return;
636
637         W_SetupShot (self, FALSE, 4, "nexball/shoot1.wav", CH_WEAPON_A, 0);
638         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, world);
639         if(trace_startsolid)
640         {
641                 if(self.metertime)
642                         self.metertime = 0; // Shot failed, hide the power meter
643                 return;
644         }
645
646         //Calculate multiplier
647         if (t < 0)
648                 mul = 1;
649         else
650         {
651                 mi = autocvar_g_nexball_basketball_meter_minpower;
652                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
653                 //One triangle wave period with 1 as max
654                 mul = 2 * mod(t, g_nexball_meter_period) / g_nexball_meter_period;
655                 if (mul > 1)
656                         mul = 2 - mul;
657                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
658         }
659         DropBall (ball, w_shotorg, W_CalculateProjectileVelocity(self.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, FALSE));
660         //TODO: use the speed_up cvar too ??
661 }
662
663 void W_Nexball_Attack2 (void)
664 {
665         entity missile;
666         if (!(balls & BALL_BASKET))
667                 return;
668         W_SetupShot (self, FALSE, 2, "nexball/shoot2.wav", CH_WEAPON_A, 0);
669 //      pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
670         missile = spawn ();
671
672         missile.owner = self;
673         missile.classname = "ballstealer";
674
675         missile.movetype = MOVETYPE_FLY;
676         PROJECTILE_MAKETRIGGER(missile);
677
678         setmodel (missile, "models/elaser.mdl"); // precision set below
679         setsize (missile, '0 0 0', '0 0 0');
680         setorigin (missile, w_shotorg);
681
682         W_SetupProjectileVelocity(missile, autocvar_g_balance_nexball_secondary_speed, 0);
683         missile.angles = vectoangles (missile.velocity);
684         missile.touch = W_Nexball_Touch;
685         missile.think = SUB_Remove;
686         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
687
688         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
689         missile.flags = FL_PROJECTILE;
690 }
691
692 float w_nexball_weapon(float req)
693 {
694         if (req == WR_THINK)
695         {
696                 if (self.BUTTON_ATCK)
697                 if (weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
698                 if (autocvar_g_nexball_basketball_meter)
699                 {
700                         if (self.ballcarried && !self.metertime)
701                                 self.metertime = time;
702                         else
703                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
704                 }
705                 else
706                 {
707                         W_Nexball_Attack(-1);
708                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
709                 }
710                 if (self.BUTTON_ATCK2)
711                 if (weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
712                 {
713                         W_Nexball_Attack2();
714                         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
715                 }
716
717                 if (!self.BUTTON_ATCK && self.metertime && self.ballcarried)
718                 {
719                         W_Nexball_Attack(time - self.metertime);
720                         // DropBall or stealing will set metertime back to 0
721                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
722                 }
723         }
724         else if (req == WR_PRECACHE)
725         {
726                 precache_model ("models/weapons/g_porto.md3");
727                 precache_model ("models/weapons/v_porto.md3");
728                 precache_model ("models/weapons/h_porto.iqm");
729                 precache_model ("models/elaser.mdl");
730                 precache_sound ("nexball/shoot1.wav");
731                 precache_sound ("nexball/shoot2.wav");
732                 precache_sound ("misc/typehit.wav");
733         }
734         else if (req == WR_SETUP)
735                 weapon_setup(WEP_PORTO);
736         else if (req == WR_SUICIDEMESSAGE)
737         {
738                 w_deathtypestring = "is a weirdo";
739         }
740         else if (req == WR_KILLMESSAGE)
741         {
742                 w_deathtypestring = "got killed by #'s black magic";
743         }
744         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE
745         return TRUE;
746 }