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