]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/nexball/nexball.qc
Merge branch 'DefaultUser/gametype_votescreen' into 'master'
[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 int autocvar_g_nexball_goalleadlimit;
19 #define autocvar_g_nexball_goallimit cvar("g_nexball_goallimit")
20
21 bool autocvar_g_nexball_basketball_jumppad = true;
22 float autocvar_g_nexball_basketball_bouncefactor;
23 float autocvar_g_nexball_basketball_bouncestop;
24 float autocvar_g_nexball_basketball_carrier_highspeed;
25 bool autocvar_g_nexball_basketball_meter;
26 float autocvar_g_nexball_basketball_meter_maxpower;
27 float autocvar_g_nexball_basketball_meter_minpower;
28 float autocvar_g_nexball_delay_collect;
29 float autocvar_g_nexball_delay_goal;
30 float autocvar_g_nexball_delay_start;
31 bool autocvar_g_nexball_football_jumppad = true;
32 float autocvar_g_nexball_football_bouncefactor;
33 float autocvar_g_nexball_football_bouncestop;
34 bool autocvar_g_nexball_radar_showallplayers;
35 bool autocvar_g_nexball_sound_bounce;
36 int autocvar_g_nexball_trail_color;
37
38 float autocvar_g_nexball_safepass_turnrate;
39 float autocvar_g_nexball_safepass_maxdist;
40 float autocvar_g_nexball_safepass_holdtime;
41 float autocvar_g_nexball_viewmodel_scale;
42 float autocvar_g_nexball_tackling;
43 vector autocvar_g_nexball_viewmodel_offset;
44
45 float autocvar_g_balance_nexball_primary_animtime;
46 float autocvar_g_balance_nexball_primary_refire;
47 float autocvar_g_balance_nexball_primary_speed;
48 float autocvar_g_balance_nexball_secondary_animtime;
49 float autocvar_g_balance_nexball_secondary_force;
50 float autocvar_g_balance_nexball_secondary_lifetime;
51 float autocvar_g_balance_nexball_secondary_refire;
52 float autocvar_g_balance_nexball_secondary_speed;
53
54 void basketball_touch(entity this, entity toucher);
55 void football_touch(entity this, entity toucher);
56 void ResetBall(entity this);
57 const int NBM_NONE = 0;
58 const int NBM_FOOTBALL = 2;
59 const int NBM_BASKETBALL = 4;
60 float nexball_mode;
61
62 float OtherTeam(float t)  //works only if there are two teams on the map!
63 {
64         entity e;
65         e = find(NULL, classname, "nexball_team");
66         if(e.team == t)
67                 e = find(e, classname, "nexball_team");
68         return e.team;
69 }
70
71 const float ST_NEXBALL_GOALS = 1;
72 void nb_ScoreRules(int teams)
73 {
74         ScoreRules_basics(teams, 0, 0, true);
75         ScoreInfo_SetLabel_TeamScore(   ST_NEXBALL_GOALS,  "goals", SFL_SORT_PRIO_PRIMARY);
76         ScoreInfo_SetLabel_PlayerScore( SP_NEXBALL_GOALS,  "goals", SFL_SORT_PRIO_PRIMARY);
77         ScoreInfo_SetLabel_PlayerScore(SP_NEXBALL_FAULTS, "faults", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER);
78         ScoreRules_basics_end();
79 }
80
81 void LogNB(string mode, entity actor)
82 {
83         string s;
84         if(!autocvar_sv_eventlog)
85                 return;
86         s = strcat(":nexball:", mode);
87         if(actor != NULL)
88                 s = strcat(s, ":", ftos(actor.playerid));
89         GameLogEcho(s);
90 }
91
92 void ball_restart(entity this)
93 {
94         if(this.owner)
95                 DropBall(this, this.owner.origin, '0 0 0');
96         ResetBall(this);
97 }
98
99 void nexball_setstatus(entity this)
100 {
101         this.items &= ~IT_KEY1;
102         if(this.ballcarried)
103         {
104                 if(this.ballcarried.teamtime && (this.ballcarried.teamtime < time))
105                 {
106                         bprint("The ", Team_ColoredFullName(this.team), " held the ball for too long.\n");
107                         DropBall(this.ballcarried, this.ballcarried.owner.origin, '0 0 0');
108                         entity e = this.ballcarried;
109                         ResetBall(e);
110                 }
111                 else
112                         this.items |= IT_KEY1;
113         }
114 }
115
116 void relocate_nexball(entity this)
117 {
118         tracebox(this.origin, BALL_MINS, BALL_MAXS, this.origin, true, this);
119         if(trace_startsolid)
120         {
121                 vector o;
122                 o = this.origin;
123                 if(!move_out_of_solid(this))
124                         objerror(this, "could not get out of solid at all!");
125                 LOG_INFO("^1NOTE: this map needs FIXING. ", this.classname, " at ", vtos(o - '0 0 1'));
126                 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(this.origin.x - o.x));
127                 LOG_INFO(" ", ftos(this.origin.y - o.y));
128                 LOG_INFO(" ", ftos(this.origin.z - o.z), "'\n");
129                 this.origin = o;
130         }
131 }
132
133 void DropOwner(entity this)
134 {
135         entity ownr;
136         ownr = this.owner;
137         DropBall(this, ownr.origin, ownr.velocity);
138         makevectors(ownr.v_angle.y * '0 1 0');
139         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
140         UNSET_ONGROUND(ownr);
141 }
142
143 void GiveBall(entity plyr, entity ball)
144 {
145         .entity weaponentity = weaponentities[0]; // TODO: find ballstealer
146         entity ownr = ball.owner;
147         if(ownr)
148         {
149                 ownr.effects &= ~autocvar_g_nexball_basketball_effects_default;
150                 ownr.ballcarried = NULL;
151                 if(ownr.metertime)
152                 {
153                         ownr.metertime = 0;
154                         ownr.(weaponentity).state = WS_READY;
155                 }
156                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
157         }
158         else
159         {
160                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
161         }
162
163         //setattachment(ball, plyr, "");
164         setorigin(ball, plyr.origin + plyr.view_ofs);
165
166         if(ball.team != plyr.team)
167                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
168
169         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
170         ball.team = plyr.team;
171         plyr.ballcarried = ball;
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         plyr.(weaponentity).weapons = plyr.weapons;
193         plyr.(weaponentity).m_switchweapon = PS(plyr).m_weapon;
194         plyr.weapons = WEPSET(NEXBALL);
195         Weapon w = WEP_NEXBALL;
196         w.wr_resetplayer(w, plyr);
197         PS(plyr).m_switchweapon = WEP_NEXBALL;
198         W_SwitchWeapon(plyr, WEP_NEXBALL);
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(ball.owner.metertime)
219         {
220                 ball.owner.metertime = 0;
221                 .entity weaponentity = weaponentities[0]; // TODO: find ballstealer
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         ball.owner.ballcarried = NULL;
230         ball.owner = NULL;
231 }
232
233 void InitBall(entity this)
234 {
235         if(gameover) return;
236         UNSET_ONGROUND(this);
237         set_movetype(this, MOVETYPE_BOUNCE);
238         if(this.classname == "nexball_basketball")
239                 settouch(this, basketball_touch);
240         else if(this.classname == "nexball_football")
241                 settouch(this, football_touch);
242         this.cnt = 0;
243         setthink(this, ResetBall);
244         this.nextthink = time + autocvar_g_nexball_delay_idle + 3;
245         this.teamtime = 0;
246         this.pusher = NULL;
247         this.team = false;
248         _sound(this, CH_TRIGGER, this.noise1, VOL_BASE, ATTEN_NORM);
249         WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
250         LogNB("init", NULL);
251 }
252
253 void ResetBall(entity this)
254 {
255         if(this.cnt < 2)        // step 1
256         {
257                 if(time == this.teamtime)
258                         bprint("The ", Team_ColoredFullName(this.team), " held the ball for too long.\n");
259
260                 settouch(this, func_null);
261                 set_movetype(this, MOVETYPE_NOCLIP);
262                 this.velocity = '0 0 0'; // just in case?
263                 if(!this.cnt)
264                         LogNB("resetidle", NULL);
265                 this.cnt = 2;
266                 this.nextthink = time;
267         }
268         else if(this.cnt < 4)     // step 2 and 3
269         {
270 //              dprint("Step ", ftos(this.cnt), ": Calculated velocity: ", vtos(this.spawnorigin - this.origin), ", time: ", ftos(time), "\n");
271                 this.velocity = (this.spawnorigin - this.origin) * (this.cnt - 1); // 1 or 0.5 second movement
272                 this.nextthink = time + 0.5;
273                 this.cnt += 1;
274         }
275         else     // step 4
276         {
277 //              dprint("Step 4: time: ", ftos(time), "\n");
278                 if(vdist(this.origin - this.spawnorigin, >, 10)) // should not happen anymore
279                         LOG_TRACE("The ball moved too far away from its spawn origin.\nOffset: ",
280                                    vtos(this.origin - this.spawnorigin), " Velocity: ", vtos(this.velocity), "\n");
281                 this.velocity = '0 0 0';
282                 setorigin(this, this.spawnorigin); // make sure it's positioned correctly anyway
283                 set_movetype(this, MOVETYPE_NONE);
284                 setthink(this, InitBall);
285                 this.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
286         }
287 }
288
289 void football_touch(entity this, entity toucher)
290 {
291         if(toucher.solid == SOLID_BSP)
292         {
293                 if(time > this.lastground + 0.1)
294                 {
295                         _sound(this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
296                         this.lastground = time;
297                 }
298                 if(this.velocity && !this.cnt)
299                         this.nextthink = time + autocvar_g_nexball_delay_idle;
300                 return;
301         }
302         if (!IS_PLAYER(toucher))
303                 return;
304         if(toucher.health < 1)
305                 return;
306         if(!this.cnt)
307                 this.nextthink = time + autocvar_g_nexball_delay_idle;
308
309         this.pusher = toucher;
310         this.team = toucher.team;
311
312         if(autocvar_g_nexball_football_physics == -1)   // MrBougo try 1, before decompiling Rev's original
313         {
314                 if(toucher.velocity)
315                         this.velocity = toucher.velocity * 1.5 + '0 0 1' * autocvar_g_nexball_football_boost_up;
316         }
317         else if(autocvar_g_nexball_football_physics == 1)         // MrBougo's modded Rev style: partially independant of the height of the aiming point
318         {
319                 makevectors(toucher.v_angle);
320                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + '0 0 1' * autocvar_g_nexball_football_boost_up;
321         }
322         else if(autocvar_g_nexball_football_physics == 2)         // 2nd mod try: totally independant. Really playable!
323         {
324                 makevectors(toucher.v_angle.y * '0 1 0');
325                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
326         }
327         else     // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
328         {
329                 makevectors(toucher.v_angle);
330                 this.velocity = toucher.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
331         }
332         this.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
333 }
334
335 void basketball_touch(entity this, entity toucher)
336 {
337         if(toucher.ballcarried)
338         {
339                 football_touch(this, toucher);
340                 return;
341         }
342         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))
343         {
344                 if(toucher.health <= 0)
345                         return;
346                 LogNB("caught", toucher);
347                 GiveBall(toucher, this);
348         }
349         else if(toucher.solid == SOLID_BSP)
350         {
351                 _sound(this, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NORM);
352                 if(this.velocity && !this.cnt)
353                         this.nextthink = min(time + autocvar_g_nexball_delay_idle, this.teamtime);
354         }
355 }
356
357 void GoalTouch(entity this, entity toucher)
358 {
359         entity ball;
360         float isclient, pscore, otherteam;
361         string pname;
362
363         if(gameover) return;
364         if((this.spawnflags & GOAL_TOUCHPLAYER) && toucher.ballcarried)
365                 ball = toucher.ballcarried;
366         else
367                 ball = toucher;
368         if(ball.classname != "nexball_basketball")
369                 if(ball.classname != "nexball_football")
370                         return;
371         if((!ball.pusher && this.team != GOAL_OUT) || ball.cnt)
372                 return;
373         EXACTTRIGGER_TOUCH(this, toucher);
374
375
376         if(NumTeams(nb_teams) == 2)
377                 otherteam = OtherTeam(ball.team);
378         else
379                 otherteam = 0;
380
381         if((isclient = IS_CLIENT(ball.pusher)))
382                 pname = ball.pusher.netname;
383         else
384                 pname = "Someone (?)";
385
386         if(ball.team == this.team)               //owngoal (regular goals)
387         {
388                 LogNB("owngoal", ball.pusher);
389                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
390                 pscore = -1;
391         }
392         else if(this.team == GOAL_FAULT)
393         {
394                 LogNB("fault", ball.pusher);
395                 if(NumTeams(nb_teams) == 2)
396                         bprint(Team_ColoredFullName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
397                 else
398                         bprint(Team_ColoredFullName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
399                 pscore = -1;
400         }
401         else if(this.team == GOAL_OUT)
402         {
403                 LogNB("out", ball.pusher);
404                 if((this.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
405                         bprint(pname, "^7 went out of bounds.\n");
406                 else
407                         bprint("The ball was returned.\n");
408                 pscore = 0;
409         }
410         else                                                       //score
411         {
412                 LogNB(strcat("goal:", ftos(this.team)), ball.pusher);
413                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", Team_ColoredFullName(ball.team), ".\n");
414                 pscore = 1;
415         }
416
417         _sound(ball, CH_TRIGGER, this.noise, VOL_BASE, ATTEN_NONE);
418
419         if(ball.team && pscore)
420         {
421                 if(NumTeams(nb_teams) == 2 && pscore < 0)
422                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
423                 else
424                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
425         }
426         if(isclient)
427         {
428                 if(pscore > 0)
429                         PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
430                 else if(pscore < 0)
431                         PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
432         }
433
434         if(ball.owner)  // Happens on spawnflag GOAL_TOUCHPLAYER
435                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
436
437         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
438
439         ball.cnt = 1;
440         setthink(ball, ResetBall);
441         if(ball.classname == "nexball_basketball")
442                 settouch(ball, football_touch); // better than func_null: football control until the ball gets reset
443         ball.nextthink = time + autocvar_g_nexball_delay_goal * (this.team != GOAL_OUT);
444 }
445
446 //=======================//
447 //         team ents       //
448 //=======================//
449 spawnfunc(nexball_team)
450 {
451         if(!g_nexball)
452         {
453                 delete(this);
454                 return;
455         }
456         this.team = this.cnt + 1;
457 }
458
459 void nb_spawnteam(string teamname, float teamcolor)
460 {
461         LOG_TRACE("^2spawned team ", teamname);
462         entity e = new(nexball_team);
463         e.netname = teamname;
464         e.cnt = teamcolor;
465         e.team = e.cnt + 1;
466         //nb_teams += 1;
467 }
468
469 void nb_spawnteams()
470 {
471         bool t_red = false, t_blue = false, t_yellow = false, t_pink = false;
472         entity e;
473         for(e = NULL; (e = find(e, classname, "nexball_goal"));)
474         {
475                 switch(e.team)
476                 {
477                 case NUM_TEAM_1:
478                         if(!t_red)
479                         {
480                                 nb_spawnteam("Red", e.team-1)   ;
481                                 nb_teams |= BIT(0);
482                                 t_red = true;
483                         }
484                         break;
485                 case NUM_TEAM_2:
486                         if(!t_blue)
487                         {
488                                 nb_spawnteam("Blue", e.team-1)  ;
489                                 t_blue = true;
490                                 nb_teams |= BIT(1);
491                         }
492                         break;
493                 case NUM_TEAM_3:
494                         if(!t_yellow)
495                         {
496                                 nb_spawnteam("Yellow", e.team-1);
497                                 t_yellow = true;
498                                 nb_teams |= BIT(2);
499                         }
500                         break;
501                 case NUM_TEAM_4:
502                         if(!t_pink)
503                         {
504                                 nb_spawnteam("Pink", e.team-1)  ;
505                                 t_pink = true;
506                                 nb_teams |= BIT(3);
507                         }
508                         break;
509                 }
510         }
511 }
512
513 void nb_delayedinit(entity this)
514 {
515         if(find(NULL, classname, "nexball_team") == NULL)
516                 nb_spawnteams();
517         nb_ScoreRules(nb_teams);
518 }
519
520
521 //=======================//
522 //        spawnfuncs       //
523 //=======================//
524
525 void SpawnBall(entity this)
526 {
527         if(!g_nexball) { delete(this); return; }
528
529 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
530
531         if(this.model == "")
532         {
533                 this.model = "models/nexball/ball.md3";
534                 this.scale = 1.3;
535         }
536
537         precache_model(this.model);
538         _setmodel(this, this.model);
539         setsize(this, BALL_MINS, BALL_MAXS);
540         ball_scale = this.scale;
541
542         relocate_nexball(this);
543         this.spawnorigin = this.origin;
544
545         this.effects = this.effects | EF_LOWPRECISION;
546
547         if(cvar(strcat("g_", this.classname, "_trail")))  //nexball_basketball :p
548         {
549                 this.glow_color = autocvar_g_nexball_trail_color;
550                 this.glow_trail = true;
551         }
552
553         set_movetype(this, MOVETYPE_FLY);
554
555         if(!autocvar_g_nexball_sound_bounce)
556                 this.noise = "";
557         else if(this.noise == "")
558                 this.noise = strzone(SND(NB_BOUNCE));
559         //bounce sound placeholder (FIXME)
560         if(this.noise1 == "")
561                 this.noise1 = strzone(SND(NB_DROP));
562         //ball drop sound placeholder (FIXME)
563         if(this.noise2 == "")
564                 this.noise2 = strzone(SND(NB_STEAL));
565         //stealing sound placeholder (FIXME)
566         if(this.noise) precache_sound(this.noise);
567         precache_sound(this.noise1);
568         precache_sound(this.noise2);
569
570         WaypointSprite_AttachCarrier(WP_NbBall, this, RADARICON_FLAGCARRIER); // the ball's team is not set yet, no rule update needed
571
572         this.reset = ball_restart;
573         setthink(this, InitBall);
574         this.nextthink = game_starttime + autocvar_g_nexball_delay_start;
575 }
576
577 spawnfunc(nexball_basketball)
578 {
579         nexball_mode |= NBM_BASKETBALL;
580         this.classname = "nexball_basketball";
581         if (!(balls & BALL_BASKET))
582         {
583                 /*
584                 CVTOV(g_nexball_basketball_effects_default);
585                 CVTOV(g_nexball_basketball_delay_hold);
586                 CVTOV(g_nexball_basketball_delay_hold_forteam);
587                 CVTOV(g_nexball_basketball_teamsteal);
588                 */
589                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
590         }
591         if(!this.effects)
592                 this.effects = autocvar_g_nexball_basketball_effects_default;
593         this.solid = SOLID_TRIGGER;
594         this.pushable = autocvar_g_nexball_basketball_jumppad;
595         balls |= BALL_BASKET;
596         this.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
597         this.bouncestop = autocvar_g_nexball_basketball_bouncestop;
598         SpawnBall(this);
599 }
600
601 spawnfunc(nexball_football)
602 {
603         nexball_mode |= NBM_FOOTBALL;
604         this.classname = "nexball_football";
605         this.solid = SOLID_TRIGGER;
606         balls |= BALL_FOOT;
607         this.pushable = autocvar_g_nexball_football_jumppad;
608         this.bouncefactor = autocvar_g_nexball_football_bouncefactor;
609         this.bouncestop = autocvar_g_nexball_football_bouncestop;
610         SpawnBall(this);
611 }
612
613 bool nb_Goal_Customize(entity this, entity client)
614 {
615         entity e = WaypointSprite_getviewentity(client);
616         entity wp_owner = this.owner;
617         if(SAME_TEAM(e, wp_owner)) { return false; }
618
619         return true;
620 }
621
622 void SpawnGoal(entity this)
623 {
624         if(!g_nexball) { delete(this); return; }
625
626         EXACTTRIGGER_INIT;
627
628         if(this.team != GOAL_OUT && Team_TeamToNumber(this.team) != -1)
629         {
630                 entity wp = WaypointSprite_SpawnFixed(WP_NbGoal, (this.absmin + this.absmax) * 0.5, this, sprite, RADARICON_NONE);
631                 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0.5 0');
632                 setcefc(this.sprite, nb_Goal_Customize);
633         }
634
635         this.classname = "nexball_goal";
636         if(this.noise == "")
637                 this.noise = "ctf/respawn.wav";
638         precache_sound(this.noise);
639         settouch(this, GoalTouch);
640 }
641
642 spawnfunc(nexball_redgoal)
643 {
644         this.team = NUM_TEAM_1;
645         SpawnGoal(this);
646 }
647 spawnfunc(nexball_bluegoal)
648 {
649         this.team = NUM_TEAM_2;
650         SpawnGoal(this);
651 }
652 spawnfunc(nexball_yellowgoal)
653 {
654         this.team = NUM_TEAM_3;
655         SpawnGoal(this);
656 }
657 spawnfunc(nexball_pinkgoal)
658 {
659         this.team = NUM_TEAM_4;
660         SpawnGoal(this);
661 }
662
663 spawnfunc(nexball_fault)
664 {
665         this.team = GOAL_FAULT;
666         if(this.noise == "")
667                 this.noise = strzone(SND(TYPEHIT));
668         SpawnGoal(this);
669 }
670
671 spawnfunc(nexball_out)
672 {
673         this.team = GOAL_OUT;
674         if(this.noise == "")
675                 this.noise = strzone(SND(TYPEHIT));
676         SpawnGoal(this);
677 }
678
679 //
680 //Spawnfuncs preserved for compatibility
681 //
682
683 spawnfunc(ball)
684 {
685         spawnfunc_nexball_football(this);
686 }
687 spawnfunc(ball_football)
688 {
689         spawnfunc_nexball_football(this);
690 }
691 spawnfunc(ball_basketball)
692 {
693         spawnfunc_nexball_basketball(this);
694 }
695 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
696 spawnfunc(ball_redgoal)
697 {
698         spawnfunc_nexball_bluegoal(this);       // I blame Revenant
699 }
700 spawnfunc(ball_bluegoal)
701 {
702         spawnfunc_nexball_redgoal(this);        // but he didn't mean to cause trouble :p
703 }
704 spawnfunc(ball_fault)
705 {
706         spawnfunc_nexball_fault(this);
707 }
708 spawnfunc(ball_bound)
709 {
710         spawnfunc_nexball_out(this);
711 }
712
713 //=======================//
714 //        Weapon code     //
715 //=======================//
716
717
718 void W_Nexball_Think(entity this)
719 {
720         //dprint("W_Nexball_Think\n");
721         //vector new_dir = steerlib_arrive(this.enemy.origin, 2500);
722         vector new_dir = normalize(this.enemy.origin + '0 0 50' - this.origin);
723         vector old_dir = normalize(this.velocity);
724         float _speed = vlen(this.velocity);
725         vector new_vel = normalize(old_dir + (new_dir * autocvar_g_nexball_safepass_turnrate)) * _speed;
726         //vector new_vel = (new_dir * autocvar_g_nexball_safepass_turnrate
727
728         this.velocity = new_vel;
729
730         this.nextthink = time;
731 }
732
733 void W_Nexball_Touch(entity this, entity toucher)
734 {
735         entity ball, attacker;
736         attacker = this.owner;
737         //this.think = func_null;
738         //this.enemy = NULL;
739
740         PROJECTILE_TOUCH(this, toucher);
741         if(attacker.team != toucher.team || autocvar_g_nexball_basketball_teamsteal)
742                 if((ball = toucher.ballcarried) && !STAT(FROZEN, toucher) && !IS_DEAD(toucher) && (IS_PLAYER(attacker)))
743                 {
744                         toucher.velocity = toucher.velocity + normalize(this.velocity) * toucher.damageforcescale * autocvar_g_balance_nexball_secondary_force;
745                         UNSET_ONGROUND(toucher);
746                         if(!attacker.ballcarried)
747                         {
748                                 LogNB("stole", attacker);
749                                 _sound(toucher, CH_TRIGGER, ball.noise2, VOL_BASE, ATTEN_NORM);
750
751                                 if(SAME_TEAM(attacker, toucher) && time > attacker.teamkill_complain)
752                                 {
753                                         attacker.teamkill_complain = time + 5;
754                                         attacker.teamkill_soundtime = time + 0.4;
755                                         attacker.teamkill_soundsource = toucher;
756                                 }
757
758                                 GiveBall(attacker, toucher.ballcarried);
759                         }
760                 }
761         delete(this);
762 }
763
764 void W_Nexball_Attack(entity actor, .entity weaponentity, float t)
765 {
766         entity ball;
767         float mul, mi, ma;
768         if(!(ball = actor.ballcarried))
769                 return;
770
771         W_SetupShot(actor, weaponentity, false, 4, SND_NB_SHOOT1, CH_WEAPON_A, 0);
772         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, NULL);
773         if(trace_startsolid)
774         {
775                 if(actor.metertime)
776                         actor.metertime = 0; // Shot failed, hide the power meter
777                 return;
778         }
779
780         //Calculate multiplier
781         if(t < 0)
782                 mul = 1;
783         else
784         {
785                 mi = autocvar_g_nexball_basketball_meter_minpower;
786                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
787                 //One triangle wave period with 1 as max
788                 mul = 2 * (t % g_nexball_meter_period) / g_nexball_meter_period;
789                 if(mul > 1)
790                         mul = 2 - mul;
791                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
792         }
793
794         DropBall(ball, w_shotorg, W_CalculateProjectileVelocity(actor, actor.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, false));
795
796
797         //TODO: use the speed_up cvar too ??
798 }
799
800 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht);
801
802 void W_Nexball_Attack2(entity actor, .entity weaponentity)
803 {
804         if(actor.ballcarried.enemy)
805         {
806                 entity _ball = actor.ballcarried;
807                 W_SetupShot(actor, weaponentity, false, 4, SND_NB_SHOOT1, CH_WEAPON_A, 0);
808                 DropBall(_ball, w_shotorg, trigger_push_calculatevelocity(_ball.origin, _ball.enemy, 32));
809                 setthink(_ball, W_Nexball_Think);
810                 _ball.nextthink = time;
811                 return;
812         }
813
814         if(!autocvar_g_nexball_tackling)
815                 return;
816
817         W_SetupShot(actor, weaponentity, false, 2, SND_NB_SHOOT2, CH_WEAPON_A, 0);
818         entity missile = new(ballstealer);
819
820         missile.owner = actor;
821
822         set_movetype(missile, MOVETYPE_FLY);
823         PROJECTILE_MAKETRIGGER(missile);
824
825         //setmodel(missile, "models/elaser.mdl");  // precision set below
826         setsize(missile, '0 0 0', '0 0 0');
827         setorigin(missile, w_shotorg);
828
829         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
830         missile.angles = vectoangles(missile.velocity);
831         settouch(missile, W_Nexball_Touch);
832         setthink(missile, SUB_Remove);
833         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
834
835         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
836         missile.flags = FL_PROJECTILE;
837         IL_PUSH(g_projectiles, missile);
838         IL_PUSH(g_bot_dodge, missile);
839
840         CSQCProjectile(missile, true, PROJECTILE_ELECTRO, true);
841 }
842
843 bool ball_customize(entity this, entity client)
844 {
845         if(!this.owner)
846         {
847                 this.effects &= ~EF_FLAME;
848                 this.scale = 1;
849                 setcefc(this, func_null);
850                 return true;
851         }
852
853         if(client == this.owner)
854         {
855                 this.scale = autocvar_g_nexball_viewmodel_scale;
856                 if(this.enemy)
857                         this.effects |= EF_FLAME;
858                 else
859                         this.effects &= ~EF_FLAME;
860         }
861         else
862         {
863                 this.effects &= ~EF_FLAME;
864                 this.scale = 1;
865         }
866
867         return true;
868 }
869
870 METHOD(BallStealer, wr_think, void(BallStealer thiswep, entity actor, .entity weaponentity, int fire))
871 {
872     TC(BallStealer, thiswep);
873     if(fire & 1)
874         if(weapon_prepareattack(thiswep, actor, weaponentity, false, autocvar_g_balance_nexball_primary_refire))
875             if(autocvar_g_nexball_basketball_meter)
876             {
877                 if(actor.ballcarried && !actor.metertime)
878                     actor.metertime = time;
879                 else
880                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
881             }
882             else
883             {
884                 W_Nexball_Attack(actor, weaponentity, -1);
885                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
886             }
887     if(fire & 2)
888         if(weapon_prepareattack(thiswep, actor, weaponentity, true, autocvar_g_balance_nexball_secondary_refire))
889         {
890             W_Nexball_Attack2(actor, weaponentity);
891             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
892         }
893
894     if(!(fire & 1) && actor.metertime && actor.ballcarried)
895     {
896         W_Nexball_Attack(actor, weaponentity, time - actor.metertime);
897         // DropBall or stealing will set metertime back to 0
898         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
899     }
900 }
901
902 METHOD(BallStealer, wr_setup, void(BallStealer this, entity actor))
903 {
904     TC(BallStealer, this);
905     //weapon_setup(WEP_PORTO.m_id);
906 }
907
908 METHOD(BallStealer, wr_checkammo1, bool(BallStealer this, entity actor))
909 {
910     TC(BallStealer, this);
911     return true;
912 }
913
914 METHOD(BallStealer, wr_checkammo2, bool(BallStealer this, entity actor))
915 {
916     TC(BallStealer, this);
917     return true;
918 }
919
920 void nb_DropBall(entity player)
921 {
922         if(player.ballcarried && g_nexball)
923                 DropBall(player.ballcarried, player.origin, player.velocity);
924 }
925
926 MUTATOR_HOOKFUNCTION(nb, ClientDisconnect)
927 {
928         entity player = M_ARGV(0, entity);
929
930         nb_DropBall(player);
931 }
932
933 MUTATOR_HOOKFUNCTION(nb, PlayerDies)
934 {
935         entity frag_target = M_ARGV(2, entity);
936
937         nb_DropBall(frag_target);
938 }
939
940 MUTATOR_HOOKFUNCTION(nb, MakePlayerObserver)
941 {
942         entity player = M_ARGV(0, entity);
943
944         nb_DropBall(player);
945         return false;
946 }
947
948 MUTATOR_HOOKFUNCTION(nb, PlayerPreThink)
949 {
950         entity player = M_ARGV(0, entity);
951
952         makevectors(player.v_angle);
953         if(nexball_mode & NBM_BASKETBALL)
954         {
955                 if(player.ballcarried)
956                 {
957                         // 'view ball'
958                         player.ballcarried.velocity = player.velocity;
959                         setcefc(player.ballcarried, ball_customize);
960
961                         vector org = player.origin + player.view_ofs +
962                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
963                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
964                                           v_up * autocvar_g_nexball_viewmodel_offset.z;
965                         setorigin(player.ballcarried, org);
966
967                         // 'safe passing'
968                         if(autocvar_g_nexball_safepass_maxdist)
969                         {
970                                 if(player.ballcarried.wait < time && player.ballcarried.enemy)
971                                 {
972                                         //centerprint(player, sprintf("Lost lock on %s", player.ballcarried.enemy.netname));
973                                         player.ballcarried.enemy = NULL;
974                                 }
975
976
977                                 //tracebox(player.origin + player.view_ofs, '-2 -2 -2', '2 2 2', player.origin + player.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
978                                 crosshair_trace(player);
979                                 if( trace_ent &&
980                                         IS_CLIENT(trace_ent) &&
981                                         !IS_DEAD(trace_ent) &&
982                                         trace_ent.team == player.team &&
983                                         vdist(trace_ent.origin - player.origin, <=, autocvar_g_nexball_safepass_maxdist) )
984                                 {
985
986                                         //if(player.ballcarried.enemy != trace_ent)
987                                         //      centerprint(player, sprintf("Locked to %s", trace_ent.netname));
988                                         player.ballcarried.enemy = trace_ent;
989                                         player.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
990
991
992                                 }
993                         }
994                 }
995                 else
996                 {
997                         .entity weaponentity = weaponentities[0]; // TODO
998                         if(player.(weaponentity).weapons)
999                         {
1000                                 player.weapons = player.(weaponentity).weapons;
1001                                 Weapon w = WEP_NEXBALL;
1002                                 w.wr_resetplayer(w, player);
1003                                 PS(player).m_switchweapon = player.(weaponentity).m_switchweapon;
1004                                 W_SwitchWeapon(player, PS(player).m_switchweapon);
1005
1006                                 player.(weaponentity).weapons = '0 0 0';
1007                         }
1008                 }
1009
1010         }
1011
1012         nexball_setstatus(player);
1013 }
1014
1015 MUTATOR_HOOKFUNCTION(nb, SpectateCopy)
1016 {
1017         entity spectatee = M_ARGV(0, entity);
1018         entity client = M_ARGV(1, entity);
1019
1020         client.metertime = spectatee.metertime;
1021 }
1022
1023 MUTATOR_HOOKFUNCTION(nb, PlayerSpawn)
1024 {
1025         entity player = M_ARGV(0, entity);
1026
1027         player.metertime = 0;
1028         .entity weaponentity = weaponentities[0];
1029         player.(weaponentity).weapons = '0 0 0';
1030
1031         if (nexball_mode & NBM_BASKETBALL)
1032                 player.weapons |= WEPSET(NEXBALL);
1033         else
1034                 player.weapons = '0 0 0';
1035
1036         return false;
1037 }
1038
1039 .float stat_sv_airspeedlimit_nonqw;
1040 .float stat_sv_maxspeed;
1041
1042 MUTATOR_HOOKFUNCTION(nb, PlayerPhysics)
1043 {
1044         entity player = M_ARGV(0, entity);
1045
1046         if(player.ballcarried)
1047         {
1048                 player.stat_sv_airspeedlimit_nonqw *= autocvar_g_nexball_basketball_carrier_highspeed;
1049                 player.stat_sv_maxspeed *= autocvar_g_nexball_basketball_carrier_highspeed;
1050         }
1051 }
1052
1053 MUTATOR_HOOKFUNCTION(nb, ForbidThrowCurrentWeapon)
1054 {
1055         entity player = M_ARGV(0, entity);
1056
1057         return PS(player).m_weapon == WEP_NEXBALL;
1058 }
1059
1060 MUTATOR_HOOKFUNCTION(nb, ForbidDropCurrentWeapon)
1061 {
1062         entity player = M_ARGV(0, entity);
1063
1064         return PS(player).m_weapon == WEP_MORTAR; // TODO: what is this for?
1065 }
1066
1067 MUTATOR_HOOKFUNCTION(nb, FilterItem)
1068 {
1069         entity item = M_ARGV(0, entity);
1070
1071         if(item.classname == "droppedweapon")
1072         if(item.weapon == WEP_NEXBALL.m_id)
1073                 return true;
1074
1075         return false;
1076 }
1077
1078 MUTATOR_HOOKFUNCTION(nb, ItemTouch)
1079 {
1080         entity item = M_ARGV(0, entity);
1081         entity toucher = M_ARGV(1, entity);
1082
1083         if(item.weapon && toucher.ballcarried)
1084                 return MUT_ITEMTOUCH_RETURN; // no new weapons for you, mister!
1085
1086         return MUT_ITEMTOUCH_CONTINUE;
1087 }
1088
1089 MUTATOR_HOOKFUNCTION(nb, GetTeamCount)
1090 {
1091         M_ARGV(1, string) = "nexball_team";
1092         return true;
1093 }
1094
1095 MUTATOR_HOOKFUNCTION(nb, WantWeapon)
1096 {
1097         M_ARGV(1, float) = 0; // weapon is set a few lines later, apparently
1098         return true;
1099 }
1100
1101 MUTATOR_HOOKFUNCTION(nb, DropSpecialItems)
1102 {
1103         entity frag_target = M_ARGV(0, entity);
1104
1105         if(frag_target.ballcarried)
1106                 DropBall(frag_target.ballcarried, frag_target.origin, frag_target.velocity);
1107
1108         return false;
1109 }
1110
1111 MUTATOR_HOOKFUNCTION(nb, SendWaypoint)
1112 {
1113         M_ARGV(2, int) &= ~0x80;
1114 }
1115
1116 REGISTER_MUTATOR(nb, g_nexball)
1117 {
1118         MUTATOR_ONADD
1119         {
1120                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
1121                 if(g_nexball_meter_period <= 0)
1122                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
1123                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
1124
1125                 // General settings
1126                 /*
1127                 CVTOV(g_nexball_football_boost_forward);   //100
1128                 CVTOV(g_nexball_football_boost_up);             //200
1129                 CVTOV(g_nexball_delay_idle);                       //10
1130                 CVTOV(g_nexball_football_physics);               //0
1131                 */
1132                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
1133
1134                 InitializeEntity(NULL, nb_delayedinit, INITPRIO_GAMETYPE);
1135                 WEP_NEXBALL.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
1136
1137                 ActivateTeamplay();
1138                 SetLimits(autocvar_g_nexball_goallimit, autocvar_g_nexball_goalleadlimit, autocvar_timelimit_override, -1);
1139                 have_team_spawns = -1; // request team spawns
1140         }
1141
1142         MUTATOR_ONROLLBACK_OR_REMOVE
1143         {
1144                 WEP_NEXBALL.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
1145                 // we actually cannot roll back nb_delayedinit here
1146                 // BUT: we don't need to! If this gets called, adding always
1147                 // succeeds.
1148         }
1149
1150         MUTATOR_ONREMOVE
1151         {
1152                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
1153                 return -1;
1154         }
1155
1156         return 0;
1157 }
1158
1159 #endif