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