38b7a7f53021fa74886a29bded80cdebbb435725
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / gamemodes / gamemode / nexball / sv_nexball.qc
1 #include "sv_nexball.qh"
2
3 .entity ballcarried;
4
5 int autocvar_g_nexball_goalleadlimit;
6 #define autocvar_g_nexball_goallimit cvar("g_nexball_goallimit")
7
8 bool autocvar_g_nexball_basketball_jumppad = true;
9 float autocvar_g_nexball_basketball_bouncefactor;
10 float autocvar_g_nexball_basketball_bouncestop;
11 float autocvar_g_nexball_basketball_carrier_highspeed;
12 bool autocvar_g_nexball_basketball_meter;
13 float autocvar_g_nexball_basketball_meter_maxpower;
14 float autocvar_g_nexball_basketball_meter_minpower;
15 float autocvar_g_nexball_delay_collect;
16 float autocvar_g_nexball_delay_goal;
17 float autocvar_g_nexball_delay_start;
18 bool autocvar_g_nexball_football_jumppad = true;
19 float autocvar_g_nexball_football_bouncefactor;
20 float autocvar_g_nexball_football_bouncestop;
21 bool autocvar_g_nexball_radar_showallplayers;
22 bool autocvar_g_nexball_sound_bounce;
23 int autocvar_g_nexball_trail_color;
24 bool autocvar_g_nexball_playerclip_collisions = true;
25
26 float autocvar_g_nexball_safepass_turnrate;
27 float autocvar_g_nexball_safepass_maxdist;
28 float autocvar_g_nexball_safepass_holdtime;
29 float autocvar_g_nexball_viewmodel_scale;
30 float autocvar_g_nexball_tackling;
31 vector autocvar_g_nexball_viewmodel_offset;
32
33 float autocvar_g_balance_nexball_primary_animtime;
34 float autocvar_g_balance_nexball_primary_refire;
35 float autocvar_g_balance_nexball_primary_speed;
36 float autocvar_g_balance_nexball_secondary_animtime;
37 float autocvar_g_balance_nexball_secondary_force;
38 float autocvar_g_balance_nexball_secondary_lifetime;
39 float autocvar_g_balance_nexball_secondary_refire;
40 float autocvar_g_balance_nexball_secondary_speed;
41
42 void basketball_touch(entity this, entity toucher);
43 void football_touch(entity this, entity toucher);
44 void ResetBall(entity this);
45 const int NBM_NONE = 0;
46 const int NBM_FOOTBALL = 2;
47 const int NBM_BASKETBALL = 4;
48 float nexball_mode;
49
50 float OtherTeam(float t)  //works only if there are two teams on the map!
51 {
52         entity e;
53         e = find(NULL, classname, "nexball_team");
54         if(e.team == t)
55                 e = find(e, classname, "nexball_team");
56         return e.team;
57 }
58
59 const int ST_NEXBALL_GOALS = 1;
60 void nb_ScoreRules(int teams)
61 {
62     GameRules_scoring(teams, 0, 0, {
63         field_team(ST_NEXBALL_GOALS, "goals", SFL_SORT_PRIO_PRIMARY);
64         field(SP_NEXBALL_GOALS, "goals", SFL_SORT_PRIO_PRIMARY);
65         field(SP_NEXBALL_FAULTS, "faults", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER);
66     });
67 }
68
69 void LogNB(string mode, entity actor)
70 {
71         string s;
72         if(!autocvar_sv_eventlog)
73                 return;
74         s = strcat(":nexball:", mode);
75         if(actor != NULL)
76                 s = strcat(s, ":", ftos(actor.playerid));
77         GameLogEcho(s);
78 }
79
80 void ball_restart(entity this)
81 {
82         if(this.owner)
83                 DropBall(this, this.owner.origin, '0 0 0');
84         ResetBall(this);
85 }
86
87 void nexball_setstatus(entity this)
88 {
89         this.items &= ~IT_KEY1;
90         if(this.ballcarried)
91         {
92                 if(this.ballcarried.teamtime && (this.ballcarried.teamtime < time))
93                 {
94                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(this.team, INFO_NEXBALL_RETURN_HELD));
95                         entity e = this.ballcarried;
96                         DropBall(this.ballcarried, this.ballcarried.owner.origin, '0 0 0');
97                         ResetBall(e);
98                 }
99                 else
100                         this.items |= IT_KEY1;
101         }
102 }
103
104 void relocate_nexball(entity this)
105 {
106         tracebox(this.origin, BALL_MINS, BALL_MAXS, this.origin, true, this);
107         if(trace_startsolid)
108         {
109                 vector o = this.origin;
110                 if (!move_out_of_solid(this)) {
111                         objerror(this, "could not get out of solid at all!");
112         }
113         LOG_INFOF(
114             "^1NOTE: this map needs FIXING. %s at %s needs to be moved out of solid, e.g. by %s",
115             this.classname,
116             vtos(o - '0 0 1'),
117             vtos(this.origin - o)
118         );
119                 this.origin = o;
120         }
121 }
122
123 void DropOwner(entity this)
124 {
125         entity ownr;
126         ownr = this.owner;
127         DropBall(this, ownr.origin, ownr.velocity);
128         makevectors(ownr.v_angle.y * '0 1 0');
129         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
130         UNSET_ONGROUND(ownr);
131 }
132
133 void GiveBall(entity plyr, entity ball)
134 {
135         .entity weaponentity = weaponentities[0]; // TODO: find ballstealer
136         entity ownr = ball.owner;
137         if(ownr)
138         {
139                 ownr.effects &= ~autocvar_g_nexball_basketball_effects_default;
140                 ownr.ballcarried = NULL;
141                 GameRules_scoring_vip(ownr, false);
142                 if(STAT(NB_METERSTART, ownr))
143                 {
144                         STAT(NB_METERSTART, ownr) = 0;
145                         ownr.(weaponentity).state = WS_READY;
146                 }
147                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
148         }
149         else
150         {
151                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
152         }
153
154         //setattachment(ball, plyr, "");
155         setorigin(ball, plyr.origin + plyr.view_ofs);
156
157         if(ball.team != plyr.team)
158                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
159
160         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
161         ball.weaponentity_fld = weaponentity;
162         ball.team = plyr.team;
163         plyr.ballcarried = ball;
164         GameRules_scoring_vip(plyr, true);
165         ball.nb_dropper = plyr;
166
167         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
168         ball.effects &= ~autocvar_g_nexball_basketball_effects_default;
169
170         ball.velocity = '0 0 0';
171         set_movetype(ball, MOVETYPE_NONE);
172         settouch(ball, func_null);
173         ball.effects |= EF_NOSHADOW;
174         ball.scale = 1; // scale down.
175
176         WaypointSprite_AttachCarrier(WP_NbBall, plyr, RADARICON_FLAGCARRIER);
177         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
178
179         if(autocvar_g_nexball_basketball_delay_hold)
180         {
181                 setthink(ball, DropOwner);
182                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
183         }
184
185         STAT(WEAPONS, plyr.(weaponentity)) = STAT(WEAPONS, plyr);
186         plyr.m_switchweapon = plyr.(weaponentity).m_weapon;
187         STAT(WEAPONS, plyr) = WEPSET(NEXBALL);
188         Weapon w = WEP_NEXBALL;
189         w.wr_resetplayer(w, plyr);
190         plyr.(weaponentity).m_switchweapon = WEP_NEXBALL;
191         W_SwitchWeapon(plyr, WEP_NEXBALL, weaponentity);
192 }
193
194 void DropBall(entity ball, vector org, vector vel)
195 {
196         ball.effects |= autocvar_g_nexball_basketball_effects_default;
197         ball.effects &= ~EF_NOSHADOW;
198         ball.owner.effects &= ~autocvar_g_nexball_basketball_effects_default;
199
200         setattachment(ball, NULL, "");
201         setorigin(ball, org);
202         set_movetype(ball, MOVETYPE_BOUNCE);
203         UNSET_ONGROUND(ball);
204         ball.scale = ball_scale;
205         ball.velocity = vel;
206         ball.nb_droptime = time;
207         settouch(ball, basketball_touch);
208         setthink(ball, ResetBall);
209         ball.nextthink = min(time + autocvar_g_nexball_delay_idle, ball.teamtime);
210
211         if(STAT(NB_METERSTART, ball.owner))
212         {
213                 STAT(NB_METERSTART, ball.owner) = 0;
214                 .entity weaponentity = ball.weaponentity_fld;
215                 ball.owner.(weaponentity).state = WS_READY;
216         }
217
218         WaypointSprite_Kill(ball.owner.waypointsprite_attachedforcarrier);
219         WaypointSprite_Spawn(WP_NbBall, 0, 0, ball, '0 0 64', NULL, ball.team, ball, waypointsprite_attachedforcarrier, false, RADARICON_FLAGCARRIER); // no health bar please
220         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
221
222         entity e = ball.owner; ball.owner = NULL;
223         e.ballcarried = NULL;
224         GameRules_scoring_vip(e, false);
225 }
226
227 void InitBall(entity this)
228 {
229         if(game_stopped) return;
230         UNSET_ONGROUND(this);
231         set_movetype(this, MOVETYPE_BOUNCE);
232         if(this.classname == "nexball_basketball")
233                 settouch(this, basketball_touch);
234         else if(this.classname == "nexball_football")
235                 settouch(this, football_touch);
236         this.cnt = 0;
237         setthink(this, ResetBall);
238         this.nextthink = time + autocvar_g_nexball_delay_idle + 3;
239         this.teamtime = 0;
240         this.pusher = NULL;
241         this.team = false;
242         _sound(this, CH_TRIGGER, this.noise1, VOL_BASE, ATTEN_NORM);
243         WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
244         LogNB("init", NULL);
245 }
246
247 void ResetBall(entity this)
248 {
249         if(this.cnt < 2)        // step 1
250         {
251                 if(time == this.teamtime)
252                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, APP_TEAM_NUM(this.team, INFO_NEXBALL_RETURN_HELD));
253
254                 settouch(this, func_null);
255                 set_movetype(this, MOVETYPE_NOCLIP);
256                 this.velocity = '0 0 0'; // just in case?
257                 if(!this.cnt)
258                         LogNB("resetidle", NULL);
259                 this.cnt = 2;
260                 this.nextthink = time;
261         }
262         else if(this.cnt < 4)     // step 2 and 3
263         {
264 //              dprint("Step ", ftos(this.cnt), ": Calculated velocity: ", vtos(this.spawnorigin - this.origin), ", time: ", ftos(time), "\n");
265                 this.velocity = (this.spawnorigin - this.origin) * (this.cnt - 1); // 1 or 0.5 second movement
266                 this.nextthink = time + 0.5;
267                 this.cnt += 1;
268         }
269         else     // step 4
270         {
271 //              dprint("Step 4: time: ", ftos(time), "\n");
272                 if(vdist(this.origin - this.spawnorigin, >, 10)) // should not happen anymore
273                         LOG_TRACE("The ball moved too far away from its spawn origin.\nOffset: ",
274                                    vtos(this.origin - this.spawnorigin), " Velocity: ", vtos(this.velocity), "\n");
275                 this.velocity = '0 0 0';
276                 setorigin(this, this.spawnorigin); // make sure it's positioned correctly anyway
277                 set_movetype(this, MOVETYPE_NONE);
278                 setthink(this, InitBall);
279                 this.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
280         }
281 }
282
283 void football_touch(entity this, entity toucher)
284 {
285         if(toucher.solid == SOLID_BSP)
286         {
287                 if(time > this.lastground + 0.1)
288                 {
289                         _sound(this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
290                         this.lastground = time;
291                 }
292                 if(this.velocity && !this.cnt)
293                         this.nextthink = time + autocvar_g_nexball_delay_idle;
294                 return;
295         }
296         if (!IS_PLAYER(toucher) && !IS_VEHICLE(toucher))
297                 return;
298         if(GetResource(toucher, RES_HEALTH) < 1)
299                 return;
300         if(!this.cnt)
301                 this.nextthink = time + autocvar_g_nexball_delay_idle;
302
303         this.pusher = toucher;
304         this.team = toucher.team;
305
306         if(autocvar_g_nexball_football_physics == -1)   // MrBougo try 1, before decompiling Rev's original
307         {
308                 if(toucher.velocity)
309                         this.velocity = toucher.velocity * 1.5 + '0 0 1' * autocvar_g_nexball_football_boost_up;
310         }
311         else if(autocvar_g_nexball_football_physics == 1)         // MrBougo's modded Rev style: partially independant of the height of the aiming point
312         {
313                 makevectors(toucher.v_angle);
314                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + '0 0 1' * autocvar_g_nexball_football_boost_up;
315         }
316         else if(autocvar_g_nexball_football_physics == 2)         // 2nd mod try: totally independant. Really playable!
317         {
318                 makevectors(toucher.v_angle.y * '0 1 0');
319                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
320         }
321         else     // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
322         {
323                 makevectors(toucher.v_angle);
324                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
325         }
326         this.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
327 }
328
329 void basketball_touch(entity this, entity toucher)
330 {
331         if(toucher.ballcarried)
332         {
333                 football_touch(this, toucher);
334                 return;
335         }
336         if(!this.cnt && IS_PLAYER(toucher) && !STAT(FROZEN, toucher) && !IS_DEAD(toucher) && (toucher != this.nb_dropper || time > this.nb_droptime + autocvar_g_nexball_delay_collect))
337         {
338                 if(GetResource(toucher, RES_HEALTH) < 1)
339                         return;
340                 LogNB("caught", toucher);
341                 GiveBall(toucher, this);
342         }
343         else if(toucher.solid == SOLID_BSP)
344         {
345                 _sound(this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
346                 if(this.velocity && !this.cnt)
347                         this.nextthink = min(time + autocvar_g_nexball_delay_idle, this.teamtime);
348         }
349 }
350
351 void GoalTouch(entity this, entity toucher)
352 {
353         entity ball;
354         float isclient, pscore, otherteam;
355         string pname;
356
357         if(game_stopped) return;
358         if((this.spawnflags & GOAL_TOUCHPLAYER) && toucher.ballcarried)
359                 ball = toucher.ballcarried;
360         else
361                 ball = toucher;
362         if(ball.classname != "nexball_basketball")
363                 if(ball.classname != "nexball_football")
364                         return;
365         if((!ball.pusher && this.team != GOAL_OUT) || ball.cnt)
366                 return;
367         EXACTTRIGGER_TOUCH(this, toucher);
368
369
370         if(NumTeams(nb_teams) == 2)
371                 otherteam = OtherTeam(ball.team);
372         else
373                 otherteam = 0;
374
375         if((isclient = IS_CLIENT(ball.pusher)))
376                 pname = ball.pusher.netname;
377         else
378                 pname = "Someone (?)";
379
380         if(ball.team == this.team)               //owngoal (regular goals)
381         {
382                 LogNB("owngoal", ball.pusher);
383                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
384                 pscore = -1;
385         }
386         else if(this.team == GOAL_FAULT)
387         {
388                 LogNB("fault", ball.pusher);
389                 if(NumTeams(nb_teams) == 2)
390                         bprint(Team_ColoredFullName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
391                 else
392                         bprint(Team_ColoredFullName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
393                 pscore = -1;
394         }
395         else if(this.team == GOAL_OUT)
396         {
397                 LogNB("out", ball.pusher);
398                 if((this.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
399                         bprint(pname, "^7 went out of bounds.\n");
400                 else
401                         bprint("The ball was returned.\n");
402                 pscore = 0;
403         }
404         else                                                       //score
405         {
406                 LogNB(strcat("goal:", ftos(this.team)), ball.pusher);
407                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", Team_ColoredFullName(ball.team), ".\n");
408                 pscore = 1;
409         }
410
411         _sound(ball, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NONE);
412
413         if(ball.team && pscore)
414         {
415                 if(NumTeams(nb_teams) == 2 && pscore < 0)
416                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
417                 else
418                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
419         }
420         if(isclient)
421         {
422                 if(pscore > 0)
423                         GameRules_scoring_add(ball.pusher, NEXBALL_GOALS, pscore);
424                 else if(pscore < 0)
425                         GameRules_scoring_add(ball.pusher, NEXBALL_FAULTS, -pscore);
426         }
427
428         if(ball.owner)  // Happens on spawnflag GOAL_TOUCHPLAYER
429                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
430
431         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
432
433         ball.cnt = 1;
434         setthink(ball, ResetBall);
435         if(ball.classname == "nexball_basketball")
436                 settouch(ball, football_touch); // better than func_null: football control until the ball gets reset
437         ball.nextthink = time + autocvar_g_nexball_delay_goal * (this.team != GOAL_OUT);
438 }
439
440 //=======================//
441 //         team ents       //
442 //=======================//
443 spawnfunc(nexball_team)
444 {
445         if(!g_nexball)
446         {
447                 delete(this);
448                 return;
449         }
450         this.team = this.cnt + 1;
451 }
452
453 void nb_spawnteam(string teamname, float teamcolor)
454 {
455         LOG_TRACE("^2spawned team ", teamname);
456         entity e = new(nexball_team);
457         e.netname = teamname;
458         e.cnt = teamcolor;
459         e.team = e.cnt + 1;
460         //nb_teams += 1;
461 }
462
463 void nb_spawnteams()
464 {
465         bool t_red = false, t_blue = false, t_yellow = false, t_pink = false;
466         entity e;
467         for(e = NULL; (e = find(e, classname, "nexball_goal"));)
468         {
469                 switch(e.team)
470                 {
471                 case NUM_TEAM_1:
472                         if(!t_red)
473                         {
474                                 nb_spawnteam("Red", e.team-1)   ;
475                                 nb_teams |= BIT(0);
476                                 t_red = true;
477                         }
478                         break;
479                 case NUM_TEAM_2:
480                         if(!t_blue)
481                         {
482                                 nb_spawnteam("Blue", e.team-1)  ;
483                                 t_blue = true;
484                                 nb_teams |= BIT(1);
485                         }
486                         break;
487                 case NUM_TEAM_3:
488                         if(!t_yellow)
489                         {
490                                 nb_spawnteam("Yellow", e.team-1);
491                                 t_yellow = true;
492                                 nb_teams |= BIT(2);
493                         }
494                         break;
495                 case NUM_TEAM_4:
496                         if(!t_pink)
497                         {
498                                 nb_spawnteam("Pink", e.team-1)  ;
499                                 t_pink = true;
500                                 nb_teams |= BIT(3);
501                         }
502                         break;
503                 }
504         }
505 }
506
507 void nb_delayedinit(entity this)
508 {
509         if(find(NULL, classname, "nexball_team") == NULL)
510                 nb_spawnteams();
511         nb_ScoreRules(nb_teams);
512 }
513
514
515 //=======================//
516 //        spawnfuncs       //
517 //=======================//
518
519 void SpawnBall(entity this)
520 {
521         if(!g_nexball) { delete(this); return; }
522
523 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
524
525         if(this.model == "")
526         {
527                 this.model = "models/nexball/ball.md3";
528                 this.scale = 1.3;
529         }
530
531         precache_model(this.model);
532         _setmodel(this, this.model);
533         setsize(this, BALL_MINS, BALL_MAXS);
534         ball_scale = this.scale;
535
536         relocate_nexball(this);
537         this.spawnorigin = this.origin;
538
539         this.effects = this.effects | EF_LOWPRECISION;
540
541         if(cvar(strcat("g_", this.classname, "_trail")))  //nexball_basketball :p
542         {
543                 this.glow_color = autocvar_g_nexball_trail_color;
544                 this.glow_trail = true;
545         }
546
547         set_movetype(this, MOVETYPE_FLY);
548
549         if(autocvar_g_nexball_playerclip_collisions)
550                 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID | DPCONTENTS_PLAYERCLIP;
551
552         if(!autocvar_g_nexball_sound_bounce)
553                 this.noise = "";
554         else if(this.noise == "")
555                 this.noise = strzone(SND(NB_BOUNCE));
556         //bounce sound placeholder (FIXME)
557         if(this.noise1 == "")
558                 this.noise1 = strzone(SND(NB_DROP));
559         //ball drop sound placeholder (FIXME)
560         if(this.noise2 == "")
561                 this.noise2 = strzone(SND(NB_STEAL));
562         //stealing sound placeholder (FIXME)
563         if(this.noise) precache_sound(this.noise);
564         precache_sound(this.noise1);
565         precache_sound(this.noise2);
566
567         WaypointSprite_AttachCarrier(WP_NbBall, this, RADARICON_FLAGCARRIER); // the ball's team is not set yet, no rule update needed
568
569         this.reset = ball_restart;
570         setthink(this, InitBall);
571         this.nextthink = game_starttime + autocvar_g_nexball_delay_start;
572 }
573
574 spawnfunc(nexball_basketball)
575 {
576         nexball_mode |= NBM_BASKETBALL;
577         this.classname = "nexball_basketball";
578         if (!(balls & BALL_BASKET))
579         {
580                 /*
581                 CVTOV(g_nexball_basketball_effects_default);
582                 CVTOV(g_nexball_basketball_delay_hold);
583                 CVTOV(g_nexball_basketball_delay_hold_forteam);
584                 CVTOV(g_nexball_basketball_teamsteal);
585                 */
586                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
587         }
588         if(!this.effects)
589                 this.effects = autocvar_g_nexball_basketball_effects_default;
590         this.solid = SOLID_TRIGGER;
591         this.pushable = autocvar_g_nexball_basketball_jumppad;
592         balls |= BALL_BASKET;
593         this.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
594         this.bouncestop = autocvar_g_nexball_basketball_bouncestop;
595         SpawnBall(this);
596 }
597
598 spawnfunc(nexball_football)
599 {
600         nexball_mode |= NBM_FOOTBALL;
601         this.classname = "nexball_football";
602         this.solid = SOLID_TRIGGER;
603         balls |= BALL_FOOT;
604         this.pushable = autocvar_g_nexball_football_jumppad;
605         this.bouncefactor = autocvar_g_nexball_football_bouncefactor;
606         this.bouncestop = autocvar_g_nexball_football_bouncestop;
607         SpawnBall(this);
608 }
609
610 bool nb_Goal_Customize(entity this, entity client)
611 {
612         entity e = WaypointSprite_getviewentity(client);
613         entity wp_owner = this.owner;
614         if(SAME_TEAM(e, wp_owner)) { return false; }
615
616         return true;
617 }
618
619 void SpawnGoal(entity this)
620 {
621         if(!g_nexball) { delete(this); return; }
622
623         EXACTTRIGGER_INIT;
624
625         if(this.team != GOAL_OUT && Team_IsValidTeam(this.team))
626         {
627                 entity wp = WaypointSprite_SpawnFixed(WP_NbGoal, (this.absmin + this.absmax) * 0.5, this, sprite, RADARICON_NONE);
628                 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0.5 0');
629                 setcefc(this.sprite, nb_Goal_Customize);
630         }
631
632         this.classname = "nexball_goal";
633         if(this.noise == "")
634                 this.noise = "ctf/respawn.wav";
635         precache_sound(this.noise);
636         settouch(this, GoalTouch);
637 }
638
639 spawnfunc(nexball_redgoal)
640 {
641         this.team = NUM_TEAM_1;
642         SpawnGoal(this);
643 }
644 spawnfunc(nexball_bluegoal)
645 {
646         this.team = NUM_TEAM_2;
647         SpawnGoal(this);
648 }
649 spawnfunc(nexball_yellowgoal)
650 {
651         this.team = NUM_TEAM_3;
652         SpawnGoal(this);
653 }
654 spawnfunc(nexball_pinkgoal)
655 {
656         this.team = NUM_TEAM_4;
657         SpawnGoal(this);
658 }
659
660 spawnfunc(nexball_fault)
661 {
662         this.team = GOAL_FAULT;
663         if(this.noise == "")
664                 this.noise = strzone(SND(TYPEHIT));
665         SpawnGoal(this);
666 }
667
668 spawnfunc(nexball_out)
669 {
670         this.team = GOAL_OUT;
671         if(this.noise == "")
672                 this.noise = strzone(SND(TYPEHIT));
673         SpawnGoal(this);
674 }
675
676 //
677 //Spawnfuncs preserved for compatibility
678 //
679
680 spawnfunc(ball)
681 {
682         spawnfunc_nexball_football(this);
683 }
684 spawnfunc(ball_football)
685 {
686         spawnfunc_nexball_football(this);
687 }
688 spawnfunc(ball_basketball)
689 {
690         spawnfunc_nexball_basketball(this);
691 }
692 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
693 spawnfunc(ball_redgoal)
694 {
695         spawnfunc_nexball_bluegoal(this);       // I blame Revenant
696 }
697 spawnfunc(ball_bluegoal)
698 {
699         spawnfunc_nexball_redgoal(this);        // but he didn't mean to cause trouble :p
700 }
701 spawnfunc(ball_fault)
702 {
703         spawnfunc_nexball_fault(this);
704 }
705 spawnfunc(ball_bound)
706 {
707         spawnfunc_nexball_out(this);
708 }
709
710 bool ball_customize(entity this, entity client)
711 {
712         if(!this.owner)
713         {
714                 this.effects &= ~EF_FLAME;
715                 this.scale = 1;
716                 setcefc(this, func_null);
717                 return true;
718         }
719
720         if(client == this.owner)
721         {
722                 this.scale = autocvar_g_nexball_viewmodel_scale;
723                 if(this.enemy)
724                         this.effects |= EF_FLAME;
725                 else
726                         this.effects &= ~EF_FLAME;
727         }
728         else
729         {
730                 this.effects &= ~EF_FLAME;
731                 this.scale = 1;
732         }
733
734         return true;
735 }
736
737 void nb_DropBall(entity player)
738 {
739         if(player.ballcarried && g_nexball)
740                 DropBall(player.ballcarried, player.origin, player.velocity);
741 }
742
743 MUTATOR_HOOKFUNCTION(nb, ClientDisconnect)
744 {
745         entity player = M_ARGV(0, entity);
746
747         nb_DropBall(player);
748 }
749
750 MUTATOR_HOOKFUNCTION(nb, PlayerDies)
751 {
752         entity frag_target = M_ARGV(2, entity);
753
754         nb_DropBall(frag_target);
755 }
756
757 MUTATOR_HOOKFUNCTION(nb, MakePlayerObserver)
758 {
759         entity player = M_ARGV(0, entity);
760
761         nb_DropBall(player);
762         return false;
763 }
764
765 MUTATOR_HOOKFUNCTION(nb, PlayerPreThink)
766 {
767         entity player = M_ARGV(0, entity);
768
769         makevectors(player.v_angle);
770         if(nexball_mode & NBM_BASKETBALL)
771         {
772                 if(player.ballcarried)
773                 {
774                         // 'view ball'
775                         player.ballcarried.velocity = player.velocity;
776                         setcefc(player.ballcarried, ball_customize);
777
778                         vector org = player.origin + player.view_ofs +
779                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
780                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
781                                           v_up * autocvar_g_nexball_viewmodel_offset.z;
782                         setorigin(player.ballcarried, org);
783
784                         // 'safe passing'
785                         if(autocvar_g_nexball_safepass_maxdist)
786                         {
787                                 if(player.ballcarried.wait < time && player.ballcarried.enemy)
788                                 {
789                                         //centerprint(player, sprintf("Lost lock on %s", player.ballcarried.enemy.netname));
790                                         player.ballcarried.enemy = NULL;
791                                 }
792
793
794                                 //tracebox(player.origin + player.view_ofs, '-2 -2 -2', '2 2 2', player.origin + player.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
795                                 crosshair_trace(player);
796                                 if( trace_ent &&
797                                         IS_CLIENT(trace_ent) &&
798                                         !IS_DEAD(trace_ent) &&
799                                         trace_ent.team == player.team &&
800                                         vdist(trace_ent.origin - player.origin, <=, autocvar_g_nexball_safepass_maxdist) )
801                                 {
802
803                                         //if(player.ballcarried.enemy != trace_ent)
804                                         //      centerprint(player, sprintf("Locked to %s", trace_ent.netname));
805                                         player.ballcarried.enemy = trace_ent;
806                                         player.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
807
808
809                                 }
810                         }
811                 }
812                 else
813                 {
814                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
815                         {
816                                 .entity weaponentity = weaponentities[slot];
817
818                                 if(STAT(WEAPONS, player.(weaponentity)))
819                                 {
820                                         STAT(WEAPONS, player) = STAT(WEAPONS, player.(weaponentity));
821                                         Weapon w = WEP_NEXBALL;
822                                         w.wr_resetplayer(w, player);
823                                         player.(weaponentity).m_switchweapon = player.m_switchweapon;
824                                         W_SwitchWeapon(player, player.(weaponentity).m_switchweapon, weaponentity);
825
826                                         STAT(WEAPONS, player.(weaponentity)) = '0 0 0';
827                                 }
828                         }
829                 }
830
831         }
832
833         nexball_setstatus(player);
834 }
835
836 MUTATOR_HOOKFUNCTION(nb, SpectateCopy)
837 {
838         entity spectatee = M_ARGV(0, entity);
839         entity client = M_ARGV(1, entity);
840
841         STAT(NB_METERSTART, client) = STAT(NB_METERSTART, spectatee);
842 }
843
844 MUTATOR_HOOKFUNCTION(nb, PlayerSpawn)
845 {
846         entity player = M_ARGV(0, entity);
847
848         STAT(NB_METERSTART, player) = 0;
849         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
850         {
851                 .entity weaponentity = weaponentities[slot];
852                 STAT(WEAPONS, player.(weaponentity)) = '0 0 0';
853         }
854
855         if (nexball_mode & NBM_BASKETBALL)
856                 STAT(WEAPONS, player) |= WEPSET(NEXBALL);
857         else
858                 STAT(WEAPONS, player) = '0 0 0';
859
860         return false;
861 }
862
863 MUTATOR_HOOKFUNCTION(nb, PlayerPhysics_UpdateStats)
864 {
865         entity player = M_ARGV(0, entity);
866         // these automatically reset, no need to worry
867
868         if(player.ballcarried)
869                 STAT(MOVEVARS_HIGHSPEED, player) *= autocvar_g_nexball_basketball_carrier_highspeed;
870 }
871
872 MUTATOR_HOOKFUNCTION(nb, ForbidThrowCurrentWeapon)
873 {
874         //entity player = M_ARGV(0, entity);
875         entity wepent = M_ARGV(1, entity);
876
877         return wepent.m_weapon == WEP_NEXBALL;
878 }
879
880 MUTATOR_HOOKFUNCTION(nb, ForbidDropCurrentWeapon)
881 {
882         //entity player = M_ARGV(0, entity);
883         int wep = M_ARGV(1, int);
884
885         return wep == WEP_MORTAR.m_id; // TODO: what is this for?
886 }
887
888 MUTATOR_HOOKFUNCTION(nb, FilterItem)
889 {
890         entity item = M_ARGV(0, entity);
891
892         if(Item_IsLoot(item))
893         if(item.weapon == WEP_NEXBALL.m_id)
894                 return true;
895
896         return false;
897 }
898
899 MUTATOR_HOOKFUNCTION(nb, ItemTouch)
900 {
901         entity item = M_ARGV(0, entity);
902         entity toucher = M_ARGV(1, entity);
903
904         if(item.weapon && toucher.ballcarried)
905                 return MUT_ITEMTOUCH_RETURN; // no new weapons for you, mister!
906
907         return MUT_ITEMTOUCH_CONTINUE;
908 }
909
910 MUTATOR_HOOKFUNCTION(nb, TeamBalance_CheckAllowedTeams)
911 {
912         M_ARGV(1, string) = "nexball_team";
913         return true;
914 }
915
916 MUTATOR_HOOKFUNCTION(nb, WantWeapon)
917 {
918         M_ARGV(1, float) = 0; // weapon is set a few lines later, apparently
919         return true;
920 }
921
922 MUTATOR_HOOKFUNCTION(nb, DropSpecialItems)
923 {
924         entity frag_target = M_ARGV(0, entity);
925
926         if(frag_target.ballcarried)
927                 DropBall(frag_target.ballcarried, frag_target.origin, frag_target.velocity);
928
929         return false;
930 }
931
932 MUTATOR_HOOKFUNCTION(nb, SendWaypoint)
933 {
934         M_ARGV(2, int) &= ~0x80;
935 }
936
937 REGISTER_MUTATOR(nb, false)
938 {
939     MUTATOR_STATIC();
940         MUTATOR_ONADD
941         {
942                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
943                 if(g_nexball_meter_period <= 0)
944                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
945                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
946
947                 // General settings
948                 /*
949                 CVTOV(g_nexball_football_boost_forward);   //100
950                 CVTOV(g_nexball_football_boost_up);             //200
951                 CVTOV(g_nexball_delay_idle);                       //10
952                 CVTOV(g_nexball_football_physics);               //0
953                 */
954                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
955
956                 InitializeEntity(NULL, nb_delayedinit, INITPRIO_GAMETYPE);
957                 WEP_NEXBALL.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
958
959                 GameRules_teams(true);
960                 GameRules_limit_score(autocvar_g_nexball_goallimit);
961                 GameRules_limit_lead(autocvar_g_nexball_goalleadlimit);
962         }
963
964         MUTATOR_ONROLLBACK_OR_REMOVE
965         {
966                 WEP_NEXBALL.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
967         }
968         return 0;
969 }