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