f7784db18a23f9289fc90038cfc1e0171a13fb27
[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 //=======================//
726 //        Weapon code     //
727 //=======================//
728
729
730 void W_Nexball_Think(entity this)
731 {
732         //dprint("W_Nexball_Think\n");
733         //vector new_dir = steerlib_arrive(this.enemy.origin, 2500);
734         vector new_dir = normalize(this.enemy.origin + '0 0 50' - this.origin);
735         vector old_dir = normalize(this.velocity);
736         float _speed = vlen(this.velocity);
737         vector new_vel = normalize(old_dir + (new_dir * autocvar_g_nexball_safepass_turnrate)) * _speed;
738         //vector new_vel = (new_dir * autocvar_g_nexball_safepass_turnrate
739
740         this.velocity = new_vel;
741
742         this.nextthink = time;
743 }
744
745 void W_Nexball_Touch(entity this, entity toucher)
746 {
747         entity ball, attacker;
748         attacker = this.owner;
749         //this.think = func_null;
750         //this.enemy = NULL;
751
752         PROJECTILE_TOUCH(this, toucher);
753         if(attacker.team != toucher.team || autocvar_g_nexball_basketball_teamsteal)
754                 if((ball = toucher.ballcarried) && !STAT(FROZEN, toucher) && !IS_DEAD(toucher) && (IS_PLAYER(attacker)))
755                 {
756                         toucher.velocity = toucher.velocity + normalize(this.velocity) * toucher.damageforcescale * autocvar_g_balance_nexball_secondary_force;
757                         UNSET_ONGROUND(toucher);
758                         if(!attacker.ballcarried)
759                         {
760                                 LogNB("stole", attacker);
761                                 _sound(toucher, CH_TRIGGER, ball.noise2, VOL_BASE, ATTEN_NORM);
762
763                                 if(SAME_TEAM(attacker, toucher) && time > CS(attacker).teamkill_complain)
764                                 {
765                                         CS(attacker).teamkill_complain = time + 5;
766                                         CS(attacker).teamkill_soundtime = time + 0.4;
767                                         CS(attacker).teamkill_soundsource = toucher;
768                                 }
769
770                                 GiveBall(attacker, toucher.ballcarried);
771                         }
772                 }
773         delete(this);
774 }
775
776 void W_Nexball_Attack(entity actor, .entity weaponentity, float t)
777 {
778         entity ball;
779         float mul, mi, ma;
780         if(!(ball = actor.ballcarried))
781                 return;
782
783         W_SetupShot(actor, weaponentity, false, 4, SND_NB_SHOOT1, CH_WEAPON_A, 0);
784         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, NULL);
785         if(trace_startsolid)
786         {
787                 if(actor.metertime)
788                         actor.metertime = 0; // Shot failed, hide the power meter
789                 return;
790         }
791
792         //Calculate multiplier
793         if(t < 0)
794                 mul = 1;
795         else
796         {
797                 mi = autocvar_g_nexball_basketball_meter_minpower;
798                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
799                 //One triangle wave period with 1 as max
800                 mul = 2 * (t % g_nexball_meter_period) / g_nexball_meter_period;
801                 if(mul > 1)
802                         mul = 2 - mul;
803                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
804         }
805
806         DropBall(ball, w_shotorg, W_CalculateProjectileVelocity(actor, actor.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, false));
807
808
809         //TODO: use the speed_up cvar too ??
810 }
811
812 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht);
813
814 void W_Nexball_Attack2(entity actor, .entity weaponentity)
815 {
816         if(actor.ballcarried.enemy)
817         {
818                 entity _ball = actor.ballcarried;
819                 W_SetupShot(actor, weaponentity, false, 4, SND_NB_SHOOT1, CH_WEAPON_A, 0);
820                 DropBall(_ball, w_shotorg, trigger_push_calculatevelocity(_ball.origin, _ball.enemy, 32));
821                 setthink(_ball, W_Nexball_Think);
822                 _ball.nextthink = time;
823                 return;
824         }
825
826         if(!autocvar_g_nexball_tackling)
827                 return;
828
829         W_SetupShot(actor, weaponentity, false, 2, SND_NB_SHOOT2, CH_WEAPON_A, 0);
830         entity missile = new(ballstealer);
831
832         missile.owner = actor;
833
834         set_movetype(missile, MOVETYPE_FLY);
835         PROJECTILE_MAKETRIGGER(missile);
836
837         //setmodel(missile, "models/elaser.mdl");  // precision set below
838         setsize(missile, '0 0 0', '0 0 0');
839         setorigin(missile, w_shotorg);
840
841         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
842         missile.angles = vectoangles(missile.velocity);
843         settouch(missile, W_Nexball_Touch);
844         setthink(missile, SUB_Remove);
845         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
846
847         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
848         missile.flags = FL_PROJECTILE;
849         IL_PUSH(g_projectiles, missile);
850         IL_PUSH(g_bot_dodge, missile);
851
852         CSQCProjectile(missile, true, PROJECTILE_ELECTRO, true);
853 }
854
855 bool ball_customize(entity this, entity client)
856 {
857         if(!this.owner)
858         {
859                 this.effects &= ~EF_FLAME;
860                 this.scale = 1;
861                 setcefc(this, func_null);
862                 return true;
863         }
864
865         if(client == this.owner)
866         {
867                 this.scale = autocvar_g_nexball_viewmodel_scale;
868                 if(this.enemy)
869                         this.effects |= EF_FLAME;
870                 else
871                         this.effects &= ~EF_FLAME;
872         }
873         else
874         {
875                 this.effects &= ~EF_FLAME;
876                 this.scale = 1;
877         }
878
879         return true;
880 }
881
882 METHOD(BallStealer, wr_think, void(BallStealer thiswep, entity actor, .entity weaponentity, int fire))
883 {
884     TC(BallStealer, thiswep);
885     if(fire & 1)
886         if(weapon_prepareattack(thiswep, actor, weaponentity, false, autocvar_g_balance_nexball_primary_refire))
887             if(autocvar_g_nexball_basketball_meter)
888             {
889                 if(actor.ballcarried && !actor.metertime)
890                     actor.metertime = time;
891                 else
892                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
893             }
894             else
895             {
896                 W_Nexball_Attack(actor, weaponentity, -1);
897                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
898             }
899     if(fire & 2)
900         if(weapon_prepareattack(thiswep, actor, weaponentity, true, autocvar_g_balance_nexball_secondary_refire))
901         {
902             W_Nexball_Attack2(actor, weaponentity);
903             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
904         }
905
906     if(!(fire & 1) && actor.metertime && actor.ballcarried)
907     {
908         W_Nexball_Attack(actor, weaponentity, time - actor.metertime);
909         // DropBall or stealing will set metertime back to 0
910         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
911     }
912 }
913
914 METHOD(BallStealer, wr_setup, void(BallStealer this, entity actor, .entity weaponentity))
915 {
916     TC(BallStealer, this);
917     //weapon_setup(WEP_PORTO.m_id);
918 }
919
920 METHOD(BallStealer, wr_reload, void(entity thiswep, entity actor, .entity weaponentity))
921 {
922     TC(BallStealer, this);
923 }
924
925 METHOD(BallStealer, wr_checkammo1, bool(BallStealer this, entity actor, .entity weaponentity))
926 {
927     TC(BallStealer, this);
928     return true;
929 }
930
931 METHOD(BallStealer, wr_checkammo2, bool(BallStealer this, entity actor, .entity weaponentity))
932 {
933     TC(BallStealer, this);
934     return true;
935 }
936
937 void nb_DropBall(entity player)
938 {
939         if(player.ballcarried && g_nexball)
940                 DropBall(player.ballcarried, player.origin, player.velocity);
941 }
942
943 MUTATOR_HOOKFUNCTION(nb, ClientDisconnect)
944 {
945         entity player = M_ARGV(0, entity);
946
947         nb_DropBall(player);
948 }
949
950 MUTATOR_HOOKFUNCTION(nb, PlayerDies)
951 {
952         entity frag_target = M_ARGV(2, entity);
953
954         nb_DropBall(frag_target);
955 }
956
957 MUTATOR_HOOKFUNCTION(nb, MakePlayerObserver)
958 {
959         entity player = M_ARGV(0, entity);
960
961         nb_DropBall(player);
962         return false;
963 }
964
965 MUTATOR_HOOKFUNCTION(nb, PlayerPreThink)
966 {
967         entity player = M_ARGV(0, entity);
968
969         makevectors(player.v_angle);
970         if(nexball_mode & NBM_BASKETBALL)
971         {
972                 if(player.ballcarried)
973                 {
974                         // 'view ball'
975                         player.ballcarried.velocity = player.velocity;
976                         setcefc(player.ballcarried, ball_customize);
977
978                         vector org = player.origin + player.view_ofs +
979                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
980                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
981                                           v_up * autocvar_g_nexball_viewmodel_offset.z;
982                         setorigin(player.ballcarried, org);
983
984                         // 'safe passing'
985                         if(autocvar_g_nexball_safepass_maxdist)
986                         {
987                                 if(player.ballcarried.wait < time && player.ballcarried.enemy)
988                                 {
989                                         //centerprint(player, sprintf("Lost lock on %s", player.ballcarried.enemy.netname));
990                                         player.ballcarried.enemy = NULL;
991                                 }
992
993
994                                 //tracebox(player.origin + player.view_ofs, '-2 -2 -2', '2 2 2', player.origin + player.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
995                                 crosshair_trace(player);
996                                 if( trace_ent &&
997                                         IS_CLIENT(trace_ent) &&
998                                         !IS_DEAD(trace_ent) &&
999                                         trace_ent.team == player.team &&
1000                                         vdist(trace_ent.origin - player.origin, <=, autocvar_g_nexball_safepass_maxdist) )
1001                                 {
1002
1003                                         //if(player.ballcarried.enemy != trace_ent)
1004                                         //      centerprint(player, sprintf("Locked to %s", trace_ent.netname));
1005                                         player.ballcarried.enemy = trace_ent;
1006                                         player.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
1007
1008
1009                                 }
1010                         }
1011                 }
1012                 else
1013                 {
1014                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1015                         {
1016                                 .entity weaponentity = weaponentities[slot];
1017
1018                                 if(player.(weaponentity).weapons)
1019                                 {
1020                                         player.weapons = player.(weaponentity).weapons;
1021                                         Weapon w = WEP_NEXBALL;
1022                                         w.wr_resetplayer(w, player);
1023                                         player.(weaponentity).m_switchweapon = player.m_switchweapon;
1024                                         W_SwitchWeapon(player, player.(weaponentity).m_switchweapon, weaponentity);
1025
1026                                         player.(weaponentity).weapons = '0 0 0';
1027                                 }
1028                         }
1029                 }
1030
1031         }
1032
1033         nexball_setstatus(player);
1034 }
1035
1036 MUTATOR_HOOKFUNCTION(nb, SpectateCopy)
1037 {
1038         entity spectatee = M_ARGV(0, entity);
1039         entity client = M_ARGV(1, entity);
1040
1041         client.metertime = spectatee.metertime;
1042 }
1043
1044 MUTATOR_HOOKFUNCTION(nb, PlayerSpawn)
1045 {
1046         entity player = M_ARGV(0, entity);
1047
1048         player.metertime = 0;
1049         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1050         {
1051                 .entity weaponentity = weaponentities[slot];
1052                 player.(weaponentity).weapons = '0 0 0';
1053         }
1054
1055         if (nexball_mode & NBM_BASKETBALL)
1056                 player.weapons |= WEPSET(NEXBALL);
1057         else
1058                 player.weapons = '0 0 0';
1059
1060         return false;
1061 }
1062
1063 .float stat_sv_airspeedlimit_nonqw;
1064 .float stat_sv_maxspeed;
1065
1066 MUTATOR_HOOKFUNCTION(nb, PlayerPhysics)
1067 {
1068         entity player = M_ARGV(0, entity);
1069
1070         if(player.ballcarried)
1071         {
1072                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_nexball_basketball_carrier_highspeed;
1073                 player.stat_sv_maxspeed *= autocvar_g_nexball_basketball_carrier_highspeed;
1074         }
1075 }
1076
1077 MUTATOR_HOOKFUNCTION(nb, ForbidThrowCurrentWeapon)
1078 {
1079         //entity player = M_ARGV(0, entity);
1080         entity wepent = M_ARGV(1, entity);
1081
1082         return wepent.m_weapon == WEP_NEXBALL;
1083 }
1084
1085 MUTATOR_HOOKFUNCTION(nb, ForbidDropCurrentWeapon)
1086 {
1087         //entity player = M_ARGV(0, entity);
1088         int wep = M_ARGV(1, int);
1089
1090         return wep == WEP_MORTAR.m_id; // TODO: what is this for?
1091 }
1092
1093 MUTATOR_HOOKFUNCTION(nb, FilterItem)
1094 {
1095         entity item = M_ARGV(0, entity);
1096
1097         if(item.classname == "droppedweapon")
1098         if(item.weapon == WEP_NEXBALL.m_id)
1099                 return true;
1100
1101         return false;
1102 }
1103
1104 MUTATOR_HOOKFUNCTION(nb, ItemTouch)
1105 {
1106         entity item = M_ARGV(0, entity);
1107         entity toucher = M_ARGV(1, entity);
1108
1109         if(item.weapon && toucher.ballcarried)
1110                 return MUT_ITEMTOUCH_RETURN; // no new weapons for you, mister!
1111
1112         return MUT_ITEMTOUCH_CONTINUE;
1113 }
1114
1115 MUTATOR_HOOKFUNCTION(nb, CheckAllowedTeams)
1116 {
1117         M_ARGV(1, string) = "nexball_team";
1118         return true;
1119 }
1120
1121 MUTATOR_HOOKFUNCTION(nb, WantWeapon)
1122 {
1123         M_ARGV(1, float) = 0; // weapon is set a few lines later, apparently
1124         return true;
1125 }
1126
1127 MUTATOR_HOOKFUNCTION(nb, DropSpecialItems)
1128 {
1129         entity frag_target = M_ARGV(0, entity);
1130
1131         if(frag_target.ballcarried)
1132                 DropBall(frag_target.ballcarried, frag_target.origin, frag_target.velocity);
1133
1134         return false;
1135 }
1136
1137 MUTATOR_HOOKFUNCTION(nb, SendWaypoint)
1138 {
1139         M_ARGV(2, int) &= ~0x80;
1140 }
1141
1142 REGISTER_MUTATOR(nb, g_nexball)
1143 {
1144     MUTATOR_STATIC();
1145         MUTATOR_ONADD
1146         {
1147                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
1148                 if(g_nexball_meter_period <= 0)
1149                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
1150                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
1151
1152                 // General settings
1153                 /*
1154                 CVTOV(g_nexball_football_boost_forward);   //100
1155                 CVTOV(g_nexball_football_boost_up);             //200
1156                 CVTOV(g_nexball_delay_idle);                       //10
1157                 CVTOV(g_nexball_football_physics);               //0
1158                 */
1159                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
1160
1161                 InitializeEntity(NULL, nb_delayedinit, INITPRIO_GAMETYPE);
1162                 WEP_NEXBALL.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
1163
1164                 GameRules_teams(true);
1165                 GameRules_limit_score(autocvar_g_nexball_goallimit);
1166                 GameRules_limit_lead(autocvar_g_nexball_goalleadlimit);
1167         }
1168
1169         MUTATOR_ONROLLBACK_OR_REMOVE
1170         {
1171                 WEP_NEXBALL.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
1172         }
1173         return 0;
1174 }
1175
1176 #endif