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