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