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