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