Merge branch 'samual/weapons' into Mario/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_nexball.qc
1 float autocvar_g_nexball_safepass_turnrate;
2 float autocvar_g_nexball_safepass_maxdist;
3 float autocvar_g_nexball_safepass_holdtime;
4 float autocvar_g_nexball_viewmodel_scale;
5 float autocvar_g_nexball_tackling;
6 vector autocvar_g_nexball_viewmodel_offset;
7
8 void basketball_touch();
9 void football_touch();
10 void ResetBall();
11 #define NBM_NONE 0
12 #define NBM_FOOTBALL 2
13 #define NBM_BASKETBALL 4
14 float nexball_mode;
15
16 float OtherTeam(float t)  //works only if there are two teams on the map!
17 {
18         entity e;
19         e = find(world, classname, "nexball_team");
20         if(e.team == t)
21                 e = find(e, classname, "nexball_team");
22         return e.team;
23 }
24
25
26 void LogNB(string mode, entity actor)
27 {
28         string s;
29         if(!autocvar_sv_eventlog)
30                 return;
31         s = strcat(":nexball:", mode);
32         if(actor != world)
33                 s = strcat(s, ":", ftos(actor.playerid));
34         GameLogEcho(s);
35 }
36
37 void ball_restart(void)
38 {
39         if(self.owner)
40                 DropBall(self, self.owner.origin, '0 0 0');
41         ResetBall();
42 }
43
44 void nexball_setstatus(void)
45 {
46         entity oldself;
47         self.items &= ~IT_KEY1;
48         if(self.ballcarried)
49         {
50                 if(self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
51                 {
52                         bprint("The ", Team_ColoredFullName(self.team), " held the ball for too long.\n");
53                         oldself = self;
54                         self = self.ballcarried;
55                         DropBall(self, self.owner.origin, '0 0 0');
56                         ResetBall();
57                         self = oldself;
58                 }
59                 else
60                         self.items |= IT_KEY1;
61         }
62 }
63
64 void relocate_nexball(void)
65 {
66         tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, TRUE, self);
67         if(trace_startsolid)
68         {
69                 vector o;
70                 o = self.origin;
71                 if(!move_out_of_solid(self))
72                         objerror("could not get out of solid at all!");
73                 print("^1NOTE: this map needs FIXING. ", self.classname, " at ", vtos(o - '0 0 1'));
74                 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin_x - o_x));
75                 print(" ", ftos(self.origin_y - o_y));
76                 print(" ", ftos(self.origin_z - o_z), "'\n");
77                 self.origin = o;
78         }
79 }
80
81 void DropOwner(void)
82 {
83         entity ownr;
84         ownr = self.owner;
85         DropBall(self, ownr.origin, ownr.velocity);
86         makevectors(ownr.v_angle_y * '0 1 0');
87         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
88         ownr.flags &= ~FL_ONGROUND;
89 }
90
91 void GiveBall(entity plyr, entity ball)
92 {
93         entity ownr;
94
95         if((ownr = ball.owner))
96         {
97                 ownr.effects &= ~autocvar_g_nexball_basketball_effects_default;
98                 ownr.ballcarried = world;
99                 if(ownr.metertime)
100                 {
101                         ownr.metertime = 0;
102                         ownr.weaponentity.state = WS_READY;
103                 }
104                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
105         }
106         else
107         {
108                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
109         }
110
111         //setattachment(ball, plyr, "");
112         setorigin(ball, plyr.origin + plyr.view_ofs);
113
114         if(ball.team != plyr.team)
115                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
116
117         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
118         ball.team = plyr.team;
119         plyr.ballcarried = ball;
120         ball.nb_dropper = plyr;
121
122         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
123         ball.effects &= ~autocvar_g_nexball_basketball_effects_default;
124
125         ball.velocity = '0 0 0';
126         ball.movetype = MOVETYPE_NONE;
127         ball.touch = func_null;
128         ball.effects |= EF_NOSHADOW;
129         ball.scale = 1; // scale down.
130
131         WaypointSprite_AttachCarrier("nb-ball", plyr, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
132         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
133
134         if(autocvar_g_nexball_basketball_delay_hold)
135         {
136                 ball.think = DropOwner;
137                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
138         }
139
140         ownr = self;
141         self = plyr;
142         self.weaponentity.weapons = self.weapons;
143         self.weaponentity.switchweapon = self.weapon;
144         self.weapons = WEPSET_PORTO;
145         WEP_ACTION(WEP_PORTO, WR_RESETPLAYER);
146         self.switchweapon = WEP_PORTO;
147         W_SwitchWeapon(WEP_PORTO);
148         self = ownr;
149 }
150
151 void DropBall(entity ball, vector org, vector vel)
152 {
153         ball.effects |= autocvar_g_nexball_basketball_effects_default;
154         ball.effects &= ~EF_NOSHADOW;
155         ball.owner.effects &= ~autocvar_g_nexball_basketball_effects_default;
156
157         setattachment(ball, world, "");
158         setorigin(ball, org);
159         ball.movetype = MOVETYPE_BOUNCE;
160         ball.flags &= ~FL_ONGROUND;
161         ball.scale = ball_scale;
162         ball.velocity = vel;
163         ball.nb_droptime = time;
164         ball.touch = basketball_touch;
165         ball.think = ResetBall;
166         ball.nextthink = min(time + autocvar_g_nexball_delay_idle, ball.teamtime);
167
168         if(ball.owner.metertime)
169         {
170                 ball.owner.metertime = 0;
171                 ball.owner.weaponentity.state = WS_READY;
172         }
173
174         WaypointSprite_Kill(ball.owner.waypointsprite_attachedforcarrier);
175         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
176         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
177
178         ball.owner.ballcarried = world;
179         ball.owner = world;
180 }
181
182 void InitBall(void)
183 {
184         if(gameover) return;
185         self.flags &= ~FL_ONGROUND;
186         self.movetype = MOVETYPE_BOUNCE;
187         if(self.classname == "nexball_basketball")
188                 self.touch = basketball_touch;
189         else if(self.classname == "nexball_football")
190                 self.touch = football_touch;
191         self.cnt = 0;
192         self.think = ResetBall;
193         self.nextthink = time + autocvar_g_nexball_delay_idle + 3;
194         self.teamtime = 0;
195         self.pusher = world;
196         self.team = FALSE;
197         sound(self, CH_TRIGGER, self.noise1, VOL_BASE, ATTEN_NORM);
198         WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
199         LogNB("init", world);
200 }
201
202 void ResetBall(void)
203 {
204         if(self.cnt < 2)        // step 1
205         {
206                 if(time == self.teamtime)
207                         bprint("The ", Team_ColoredFullName(self.team), " held the ball for too long.\n");
208
209                 self.touch = func_null;
210                 self.movetype = MOVETYPE_NOCLIP;
211                 self.velocity = '0 0 0'; // just in case?
212                 if(!self.cnt)
213                         LogNB("resetidle", world);
214                 self.cnt = 2;
215                 self.nextthink = time;
216         }
217         else if(self.cnt < 4)     // step 2 and 3
218         {
219 //              dprint("Step ", ftos(self.cnt), ": Calculated velocity: ", vtos(self.spawnorigin - self.origin), ", time: ", ftos(time), "\n");
220                 self.velocity = (self.spawnorigin - self.origin) * (self.cnt - 1); // 1 or 0.5 second movement
221                 self.nextthink = time + 0.5;
222                 self.cnt += 1;
223         }
224         else     // step 4
225         {
226 //              dprint("Step 4: time: ", ftos(time), "\n");
227                 if(vlen(self.origin - self.spawnorigin) > 10)  // should not happen anymore
228                         dprint("The ball moved too far away from its spawn origin.\nOffset: ",
229                                    vtos(self.origin - self.spawnorigin), " Velocity: ", vtos(self.velocity), "\n");
230                 self.velocity = '0 0 0';
231                 setorigin(self, self.spawnorigin); // make sure it's positioned correctly anyway
232                 self.movetype = MOVETYPE_NONE;
233                 self.think = InitBall;
234                 self.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
235         }
236 }
237
238 void football_touch(void)
239 {
240         if(other.solid == SOLID_BSP)
241         {
242                 if(time > self.lastground + 0.1)
243                 {
244                         sound(self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
245                         self.lastground = time;
246                 }
247                 if(vlen(self.velocity) && !self.cnt)
248                         self.nextthink = time + autocvar_g_nexball_delay_idle;
249                 return;
250         }
251         if (!IS_PLAYER(other))
252                 return;
253         if(other.health < 1)
254                 return;
255         if(!self.cnt)
256                 self.nextthink = time + autocvar_g_nexball_delay_idle;
257
258         self.pusher = other;
259         self.team = other.team;
260
261         if(autocvar_g_nexball_football_physics == -1)   // MrBougo try 1, before decompiling Rev's original
262         {
263                 if(vlen(other.velocity))
264                         self.velocity = other.velocity * 1.5 + '0 0 1' * autocvar_g_nexball_football_boost_up;
265         }
266         else if(autocvar_g_nexball_football_physics == 1)         // MrBougo's modded Rev style: partially independant of the height of the aiming point
267         {
268                 makevectors(other.v_angle);
269                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + '0 0 1' * autocvar_g_nexball_football_boost_up;
270         }
271         else if(autocvar_g_nexball_football_physics == 2)         // 2nd mod try: totally independant. Really playable!
272         {
273                 makevectors(other.v_angle_y * '0 1 0');
274                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
275         }
276         else     // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
277         {
278                 makevectors(other.v_angle);
279                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
280         }
281         self.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
282 }
283
284 void basketball_touch(void)
285 {
286         if(other.ballcarried)
287         {
288                 football_touch();
289                 return;
290         }
291         if(!self.cnt && IS_PLAYER(other) && (other != self.nb_dropper || time > self.nb_droptime + autocvar_g_nexball_delay_collect))
292         {
293                 if(other.health <= 0)
294                         return;
295                 LogNB("caught", other);
296                 GiveBall(other, self);
297         }
298         else if(other.solid == SOLID_BSP)
299         {
300                 sound(self, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
301                 if(vlen(self.velocity) && !self.cnt)
302                         self.nextthink = min(time + autocvar_g_nexball_delay_idle, self.teamtime);
303         }
304 }
305
306 void GoalTouch(void)
307 {
308         entity ball;
309         float isclient, pscore, otherteam;
310         string pname;
311
312         if(gameover) return;
313         if((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
314                 ball = other.ballcarried;
315         else
316                 ball = other;
317         if(ball.classname != "nexball_basketball")
318                 if(ball.classname != "nexball_football")
319                         return;
320         if((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
321                 return;
322         EXACTTRIGGER_TOUCH;
323
324
325         if(nb_teams == 2)
326                 otherteam = OtherTeam(ball.team);
327         else
328                 otherteam = 0;
329
330         if((isclient = IS_CLIENT(ball.pusher)))
331                 pname = ball.pusher.netname;
332         else
333                 pname = "Someone (?)";
334
335         if(ball.team == self.team)               //owngoal (regular goals)
336         {
337                 LogNB("owngoal", ball.pusher);
338                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
339                 pscore = -1;
340         }
341         else if(self.team == GOAL_FAULT)
342         {
343                 LogNB("fault", ball.pusher);
344                 if(nb_teams == 2)
345                         bprint(Team_ColoredFullName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
346                 else
347                         bprint(Team_ColoredFullName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
348                 pscore = -1;
349         }
350         else if(self.team == GOAL_OUT)
351         {
352                 LogNB("out", ball.pusher);
353                 if((self.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
354                         bprint(pname, "^7 went out of bounds.\n");
355                 else
356                         bprint("The ball was returned.\n");
357                 pscore = 0;
358         }
359         else                                                       //score
360         {
361                 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
362                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", Team_ColoredFullName(ball.team), ".\n");
363                 pscore = 1;
364         }
365
366         sound(ball, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NONE);
367
368         if(ball.team && pscore)
369         {
370                 if(nb_teams == 2 && pscore < 0)
371                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
372                 else
373                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
374         }
375         if(isclient)
376         {
377                 if(pscore > 0)
378                         PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
379                 else if(pscore < 0)
380                         PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
381         }
382
383         if(ball.owner)  // Happens on spawnflag GOAL_TOUCHPLAYER
384                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
385
386         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
387
388         ball.cnt = 1;
389         ball.think = ResetBall;
390         if(ball.classname == "nexball_basketball")
391                 ball.touch = football_touch; // better than func_null: football control until the ball gets reset
392         ball.nextthink = time + autocvar_g_nexball_delay_goal * (self.team != GOAL_OUT);
393 }
394
395 //=======================//
396 //         team ents       //
397 //=======================//
398 void spawnfunc_nexball_team(void)
399 {
400         if(!g_nexball)
401         {
402                 remove(self);
403                 return;
404         }
405         self.team = self.cnt + 1;
406 }
407
408 void nb_spawnteam(string teamname, float teamcolor)
409 {
410         dprint("^2spawned team ", teamname, "\n");
411         entity e;
412         e = spawn();
413         e.classname = "nexball_team";
414         e.netname = teamname;
415         e.cnt = teamcolor;
416         e.team = e.cnt + 1;
417         nb_teams += 1;
418 }
419
420 void nb_spawnteams(void)
421 {
422         float t_r = 0, t_b = 0, t_y = 0, t_p = 0;
423         entity e;
424         for(e = world; (e = find(e, classname, "nexball_goal"));)
425         {
426                 switch(e.team)
427                 {
428                 case NUM_TEAM_1:
429                         if(!t_r)
430                         {
431                                 nb_spawnteam("Red", e.team-1)   ;
432                                 t_r = 1;
433                         }
434                         break;
435                 case NUM_TEAM_2:
436                         if(!t_b)
437                         {
438                                 nb_spawnteam("Blue", e.team-1)  ;
439                                 t_b = 1;
440                         }
441                         break;
442                 case NUM_TEAM_3:
443                         if(!t_y)
444                         {
445                                 nb_spawnteam("Yellow", e.team-1);
446                                 t_y = 1;
447                         }
448                         break;
449                 case NUM_TEAM_4:
450                         if(!t_p)
451                         {
452                                 nb_spawnteam("Pink", e.team-1)  ;
453                                 t_p = 1;
454                         }
455                         break;
456                 }
457         }
458 }
459
460 void nb_delayedinit(void)
461 {
462         if(find(world, classname, "nexball_team") == world)
463                 nb_spawnteams();
464         ScoreRules_nexball(nb_teams);
465 }
466
467
468 //=======================//
469 //        spawnfuncs       //
470 //=======================//
471
472 void SpawnBall(void)
473 {
474         if(!g_nexball)
475         {
476                 remove(self);
477                 return;
478         }
479
480 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
481
482         if(self.model == "")
483         {
484                 self.model = "models/nexball/ball.md3";
485                 self.scale = 1.3;
486         }
487
488         precache_model(self.model);
489         setmodel(self, self.model);
490         setsize(self, BALL_MINS, BALL_MAXS);
491         ball_scale = self.scale;
492
493         relocate_nexball();
494         self.spawnorigin = self.origin;
495
496         self.effects = self.effects | EF_LOWPRECISION;
497
498         if(cvar(strcat("g_", self.classname, "_trail")))  //nexball_basketball :p
499         {
500                 self.glow_color = autocvar_g_nexball_trail_color;
501                 self.glow_trail = TRUE;
502         }
503
504         self.movetype = MOVETYPE_FLY;
505
506         if(!autocvar_g_nexball_sound_bounce)
507                 self.noise = "";
508         else if(self.noise == "")
509                 self.noise = "sound/nexball/bounce.wav";
510         //bounce sound placeholder (FIXME)
511         if(self.noise1 == "")
512                 self.noise1 = "sound/nexball/drop.wav";
513         //ball drop sound placeholder (FIXME)
514         if(self.noise2 == "")
515                 self.noise2 = "sound/nexball/steal.wav";
516         //stealing sound placeholder (FIXME)
517         if(self.noise) precache_sound(self.noise);
518         precache_sound(self.noise1);
519         precache_sound(self.noise2);
520
521         WaypointSprite_AttachCarrier("nb-ball", self, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR); // the ball's team is not set yet, no rule update needed
522
523         self.reset = ball_restart;
524         self.think = InitBall;
525         self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
526 }
527
528 void spawnfunc_nexball_basketball(void)
529 {
530         nexball_mode |= NBM_BASKETBALL;
531         self.classname = "nexball_basketball";
532         if (!(balls & BALL_BASKET))
533         {
534                 /*
535                 CVTOV(g_nexball_basketball_effects_default);
536                 CVTOV(g_nexball_basketball_delay_hold);
537                 CVTOV(g_nexball_basketball_delay_hold_forteam);
538                 CVTOV(g_nexball_basketball_teamsteal);
539                 */
540                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
541         }
542         if(!self.effects)
543                 self.effects = autocvar_g_nexball_basketball_effects_default;
544         self.solid = SOLID_TRIGGER;
545         balls |= BALL_BASKET;
546         self.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
547         self.bouncestop = autocvar_g_nexball_basketball_bouncestop;
548         SpawnBall();
549 }
550
551 void spawnfunc_nexball_football(void)
552 {
553         nexball_mode |= NBM_FOOTBALL;
554         self.classname = "nexball_football";
555         self.solid = SOLID_TRIGGER;
556         balls |= BALL_FOOT;
557         self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
558         self.bouncestop = autocvar_g_nexball_football_bouncestop;
559         SpawnBall();
560 }
561
562 void SpawnGoal(void)
563 {
564         if(!g_nexball)
565         {
566                 remove(self);
567                 return;
568         }
569         EXACTTRIGGER_INIT;
570         self.classname = "nexball_goal";
571         if(self.noise == "")
572                 self.noise = "ctf/respawn.wav";
573         precache_sound(self.noise);
574         self.touch = GoalTouch;
575 }
576
577 void spawnfunc_nexball_redgoal(void)
578 {
579         self.team = NUM_TEAM_1;
580         SpawnGoal();
581 }
582 void spawnfunc_nexball_bluegoal(void)
583 {
584         self.team = NUM_TEAM_2;
585         SpawnGoal();
586 }
587 void spawnfunc_nexball_yellowgoal(void)
588 {
589         self.team = NUM_TEAM_3;
590         SpawnGoal();
591 }
592 void spawnfunc_nexball_pinkgoal(void)
593 {
594         self.team = NUM_TEAM_4;
595         SpawnGoal();
596 }
597
598 void spawnfunc_nexball_fault(void)
599 {
600         self.team = GOAL_FAULT;
601         if(self.noise == "")
602                 self.noise = "misc/typehit.wav";
603         SpawnGoal();
604 }
605
606 void spawnfunc_nexball_out(void)
607 {
608         self.team = GOAL_OUT;
609         if(self.noise == "")
610                 self.noise = "misc/typehit.wav";
611         SpawnGoal();
612 }
613
614 //
615 //Spawnfuncs preserved for compatibility
616 //
617
618 void spawnfunc_ball(void)
619 {
620         spawnfunc_nexball_football();
621 }
622 void spawnfunc_ball_football(void)
623 {
624         spawnfunc_nexball_football();
625 }
626 void spawnfunc_ball_basketball(void)
627 {
628         spawnfunc_nexball_basketball();
629 }
630 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
631 void spawnfunc_ball_redgoal(void)
632 {
633         spawnfunc_nexball_bluegoal();   // I blame Revenant
634 }
635 void spawnfunc_ball_bluegoal(void)
636 {
637         spawnfunc_nexball_redgoal();    // but he didn't mean to cause trouble :p
638 }
639 void spawnfunc_ball_fault(void)
640 {
641         spawnfunc_nexball_fault();
642 }
643 void spawnfunc_ball_bound(void)
644 {
645         spawnfunc_nexball_out();
646 }
647
648 //=======================//
649 //        Weapon code     //
650 //=======================//
651
652
653 void W_Nexball_Think()
654 {
655         //dprint("W_Nexball_Think\n");
656         //vector new_dir = steerlib_arrive(self.enemy.origin, 2500);
657         vector new_dir = normalize(self.enemy.origin + '0 0 50' - self.origin);
658         vector old_dir = normalize(self.velocity);
659         float _speed = vlen(self.velocity);
660         vector new_vel = normalize(old_dir + (new_dir * autocvar_g_nexball_safepass_turnrate)) * _speed;
661         //vector new_vel = (new_dir * autocvar_g_nexball_safepass_turnrate
662
663         self.velocity = new_vel;
664
665         self.nextthink = time;
666 }
667
668 void W_Nexball_Touch(void)
669 {
670         entity ball, attacker;
671         attacker = self.owner;
672         //self.think = func_null;
673         //self.enemy = world;
674
675         PROJECTILE_TOUCH;
676         if(attacker.team != other.team || autocvar_g_nexball_basketball_teamsteal)
677                 if((ball = other.ballcarried) && (IS_PLAYER(attacker)))
678                 {
679                         other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
680                         other.flags &= ~FL_ONGROUND;
681                         if(!attacker.ballcarried)
682                         {
683                                 LogNB("stole", attacker);
684                                 sound(other, CH_TRIGGER, ball.noise2, VOL_BASE, ATTEN_NORM);
685
686                                 if(attacker.team == other.team && time > attacker.teamkill_complain)
687                                 {
688                                         attacker.teamkill_complain = time + 5;
689                                         attacker.teamkill_soundtime = time + 0.4;
690                                         attacker.teamkill_soundsource = other;
691                                 }
692
693                                 GiveBall(attacker, other.ballcarried);
694                         }
695                 }
696         remove(self);
697 }
698
699 void W_Nexball_Attack(float t)
700 {
701         entity ball;
702         float mul, mi, ma;
703         if(!(ball = self.ballcarried))
704                 return;
705
706         W_SetupShot(self, FALSE, 4, "nexball/shoot1.wav", CH_WEAPON_A, 0);
707         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, world);
708         if(trace_startsolid)
709         {
710                 if(self.metertime)
711                         self.metertime = 0; // Shot failed, hide the power meter
712                 return;
713         }
714
715         //Calculate multiplier
716         if(t < 0)
717                 mul = 1;
718         else
719         {
720                 mi = autocvar_g_nexball_basketball_meter_minpower;
721                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
722                 //One triangle wave period with 1 as max
723                 mul = 2 * mod(t, g_nexball_meter_period) / g_nexball_meter_period;
724                 if(mul > 1)
725                         mul = 2 - mul;
726                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
727         }
728
729         DropBall(ball, w_shotorg, W_CalculateProjectileVelocity(self.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, FALSE));
730
731
732         //TODO: use the speed_up cvar too ??
733 }
734
735 void W_Nexball_Attack2(void)
736 {
737         if(self.ballcarried.enemy)
738         {
739                 entity _ball = self.ballcarried;
740                 W_SetupShot(self, FALSE, 4, "nexball/shoot1.wav", CH_WEAPON_A, 0);
741                 DropBall(_ball, w_shotorg, trigger_push_calculatevelocity(_ball.origin, _ball.enemy, 32));
742                 _ball.think = W_Nexball_Think;
743                 _ball.nextthink = time;
744                 return;
745         }
746
747         if(!autocvar_g_nexball_tackling)
748                 return;
749
750         entity missile;
751         if(!(balls & BALL_BASKET))
752                 return;
753         W_SetupShot(self, FALSE, 2, "nexball/shoot2.wav", CH_WEAPON_A, 0);
754 //      pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
755         missile = spawn();
756
757         missile.owner = self;
758         missile.classname = "ballstealer";
759
760         missile.movetype = MOVETYPE_FLY;
761         PROJECTILE_MAKETRIGGER(missile);
762
763         setmodel(missile, "models/elaser.mdl");  // precision set below
764         setsize(missile, '0 0 0', '0 0 0');
765         setorigin(missile, w_shotorg);
766
767         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
768         missile.angles = vectoangles(missile.velocity);
769         missile.touch = W_Nexball_Touch;
770         missile.think = SUB_Remove;
771         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
772
773         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
774         missile.flags = FL_PROJECTILE;
775 }
776
777 float ball_customize()
778 {
779         if(!self.owner)
780         {
781                 self.effects &= ~EF_FLAME;
782                 self.scale = 1;
783                 self.customizeentityforclient = func_null;
784                 return TRUE;
785         }
786
787         if(other == self.owner)
788         {
789                 self.scale = autocvar_g_nexball_viewmodel_scale;
790                 if(self.enemy)
791                         self.effects |= EF_FLAME;
792                 else
793                         self.effects &= ~EF_FLAME;
794         }
795         else
796         {
797                 self.effects &= ~EF_FLAME;
798                 self.scale = 1;
799         }
800
801         return TRUE;
802 }
803
804 float w_nexball_weapon(float req)
805 {
806         if(req == WR_THINK)
807         {
808                 if(self.BUTTON_ATCK)
809                         if(weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
810                                 if(autocvar_g_nexball_basketball_meter)
811                                 {
812                                         if(self.ballcarried && !self.metertime)
813                                                 self.metertime = time;
814                                         else
815                                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
816                                 }
817                                 else
818                                 {
819                                         W_Nexball_Attack(-1);
820                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
821                                 }
822                 if(self.BUTTON_ATCK2)
823                         if(weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
824                         {
825                                 W_Nexball_Attack2();
826                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
827                         }
828
829                 if(!self.BUTTON_ATCK && self.metertime && self.ballcarried)
830                 {
831                         W_Nexball_Attack(time - self.metertime);
832                         // DropBall or stealing will set metertime back to 0
833                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
834                 }
835         }
836         else if(req == WR_INIT)
837         {
838                 precache_model("models/weapons/g_porto.md3");
839                 precache_model("models/weapons/v_porto.md3");
840                 precache_model("models/weapons/h_porto.iqm");
841                 precache_model("models/elaser.mdl");
842                 precache_sound("nexball/shoot1.wav");
843                 precache_sound("nexball/shoot2.wav");
844                 precache_sound("misc/typehit.wav");
845         }
846         else if(req == WR_SETUP)
847         {
848                 //weapon_setup(WEP_PORTO);
849         }
850         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE
851         return TRUE;
852 }
853
854 MUTATOR_HOOKFUNCTION(nexball_BallDrop)
855 {
856         if(self.ballcarried && g_nexball)
857                 DropBall(self.ballcarried, self.origin, self.velocity);
858
859         return 0;
860 }
861
862 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsString)
863 {
864         ret_string = strcat(ret_string, ":NB");
865         return 0;
866 }
867
868 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsPrettyString)
869 {
870         ret_string = strcat(ret_string, ", NexBall");
871         return 0;
872 }
873
874 MUTATOR_HOOKFUNCTION(nexball_PlayerPreThink)
875 {
876         makevectors(self.v_angle);
877         if(nexball_mode & NBM_BASKETBALL)
878         {
879                 if(self.ballcarried)
880                 {
881                         // 'view ball'
882                         self.ballcarried.velocity = self.velocity;
883                         self.ballcarried.customizeentityforclient = ball_customize;
884
885                         setorigin(self.ballcarried, self.origin + self.view_ofs +
886                                           v_forward * autocvar_g_nexball_viewmodel_offset_x +
887                                           v_right * autocvar_g_nexball_viewmodel_offset_y +
888                                           v_up * autocvar_g_nexball_viewmodel_offset_z);
889
890                         // 'safe passing'
891                         if(autocvar_g_nexball_safepass_maxdist)
892                         {
893                                 if(self.ballcarried.wait < time && self.ballcarried.enemy)
894                                 {
895                                         //centerprint(self, sprintf("Lost lock on %s", self.ballcarried.enemy.netname));
896                                         self.ballcarried.enemy = world;
897                                 }
898
899
900                                 //tracebox(self.origin + self.view_ofs, '-2 -2 -2', '2 2 2', self.origin + self.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
901                                 crosshair_trace(self);
902                                 if( trace_ent &&
903                                         IS_CLIENT(trace_ent) &&
904                                         trace_ent.deadflag == DEAD_NO &&
905                                         trace_ent.team == self.team &&
906                                         vlen(trace_ent.origin - self.origin) <= autocvar_g_nexball_safepass_maxdist )
907                                 {
908
909                                         //if(self.ballcarried.enemy != trace_ent)
910                                         //      centerprint(self, sprintf("Locked to %s", trace_ent.netname));
911                                         self.ballcarried.enemy = trace_ent;
912                                         self.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
913
914
915                                 }
916                         }
917                 }
918                 else
919                 {
920                         if(self.weaponentity.weapons)
921                         {
922                                 self.weapons = self.weaponentity.weapons;
923                                 WEP_ACTION(WEP_PORTO, WR_RESETPLAYER);
924                                 self.switchweapon = self.weaponentity.switchweapon;
925                                 W_SwitchWeapon(self.switchweapon);
926
927                 self.weaponentity.weapons = '0 0 0';
928                         }
929                 }
930
931         }
932
933         nexball_setstatus();
934
935         return FALSE;
936 }
937
938 MUTATOR_HOOKFUNCTION(nexball_PlayerSpawn)
939 {
940         self.weaponentity.weapons = '0 0 0';
941
942         if(nexball_mode & NBM_BASKETBALL)
943                 self.weapons |= WEPSET_PORTO;
944         else
945                 self.weapons = '0 0 0';
946
947         return FALSE;
948 }
949
950 MUTATOR_HOOKFUNCTION(nexball_SetStartItems)
951 {
952         start_items |= IT_UNLIMITED_SUPERWEAPONS; // FIXME BAD BAD BAD BAD HACK, NEXBALL SHOULDN'T ABUSE PORTO'S WEAPON SLOT
953
954         return FALSE;
955 }
956
957 MUTATOR_DEFINITION(gamemode_nexball)
958 {
959         MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
960         MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
961         MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
962         MUTATOR_HOOK(BuildMutatorsPrettyString, nexball_BuildMutatorsPrettyString, CBC_ORDER_ANY);
963         MUTATOR_HOOK(BuildMutatorsString, nexball_BuildMutatorsString, CBC_ORDER_ANY);
964         MUTATOR_HOOK(PlayerSpawn, nexball_PlayerSpawn, CBC_ORDER_ANY);
965         MUTATOR_HOOK(PlayerPreThink, nexball_PlayerPreThink, CBC_ORDER_ANY);
966         MUTATOR_HOOK(SetStartItems, nexball_SetStartItems, CBC_ORDER_ANY);
967
968         MUTATOR_ONADD
969         {
970                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
971                 if(g_nexball_meter_period <= 0)
972                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
973                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
974                 addstat(STAT_NB_METERSTART, AS_FLOAT, metertime);
975
976                 // General settings
977                 /*
978                 CVTOV(g_nexball_football_boost_forward);   //100
979                 CVTOV(g_nexball_football_boost_up);             //200
980                 CVTOV(g_nexball_delay_idle);                       //10
981                 CVTOV(g_nexball_football_physics);               //0
982                 */
983                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
984
985                 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
986         }
987
988         MUTATOR_ONROLLBACK_OR_REMOVE
989         {
990                 // we actually cannot roll back nb_delayedinit here
991                 // BUT: we don't need to! If this gets called, adding always
992                 // succeeds.
993         }
994
995         MUTATOR_ONREMOVE
996         {
997                 print("This is a game type and it cannot be removed at runtime.");
998                 return -1;
999         }
1000
1001         return 0;
1002 }