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