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