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