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