]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/gamemodes/gamemode/nexball/nexball.qc
Remove weapon SELFPARAM
[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(entity this);
44 void football_touch(entity this);
45 void ResetBall(entity this);
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(this);
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                         entity e = this.ballcarried;
100                         WITHSELF(e, ResetBall(e));
101                 }
102                 else
103                         this.items |= IT_KEY1;
104         }
105 }
106
107 void relocate_nexball(entity this)
108 {
109         tracebox(this.origin, BALL_MINS, BALL_MAXS, this.origin, true, this);
110         if(trace_startsolid)
111         {
112                 vector o;
113                 o = this.origin;
114                 if(!move_out_of_solid(this))
115                         objerror("could not get out of solid at all!");
116                 LOG_INFO("^1NOTE: this map needs FIXING. ", this.classname, " at ", vtos(o - '0 0 1'));
117                 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(this.origin.x - o.x));
118                 LOG_INFO(" ", ftos(this.origin.y - o.y));
119                 LOG_INFO(" ", ftos(this.origin.z - o.z), "'\n");
120                 this.origin = o;
121         }
122 }
123
124 void DropOwner(entity this)
125 {
126         entity ownr;
127         ownr = this.owner;
128         DropBall(this, ownr.origin, ownr.velocity);
129         makevectors(ownr.v_angle.y * '0 1 0');
130         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
131         UNSET_ONGROUND(ownr);
132 }
133
134 void GiveBall(entity plyr, entity ball)
135 {
136         .entity weaponentity = weaponentities[0]; // TODO: find ballstealer
137         entity ownr = ball.owner;
138         if(ownr)
139         {
140                 ownr.effects &= ~autocvar_g_nexball_basketball_effects_default;
141                 ownr.ballcarried = world;
142                 if(ownr.metertime)
143                 {
144                         ownr.metertime = 0;
145                         ownr.(weaponentity).state = WS_READY;
146                 }
147                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
148         }
149         else
150         {
151                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
152         }
153
154         //setattachment(ball, plyr, "");
155         setorigin(ball, plyr.origin + plyr.view_ofs);
156
157         if(ball.team != plyr.team)
158                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
159
160         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
161         ball.team = plyr.team;
162         plyr.ballcarried = ball;
163         ball.nb_dropper = plyr;
164
165         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
166         ball.effects &= ~autocvar_g_nexball_basketball_effects_default;
167
168         ball.velocity = '0 0 0';
169         ball.movetype = MOVETYPE_NONE;
170         settouch(ball, func_null);
171         ball.effects |= EF_NOSHADOW;
172         ball.scale = 1; // scale down.
173
174         WaypointSprite_AttachCarrier(WP_NbBall, plyr, RADARICON_FLAGCARRIER);
175         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
176
177         if(autocvar_g_nexball_basketball_delay_hold)
178         {
179                 setthink(ball, DropOwner);
180                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
181         }
182
183         plyr.(weaponentity).weapons = plyr.weapons;
184         plyr.(weaponentity).m_switchweapon = PS(plyr).m_weapon;
185         plyr.weapons = WEPSET(NEXBALL);
186         Weapon w = WEP_NEXBALL;
187         WITHSELF(plyr, w.wr_resetplayer(w, plyr));
188         PS(plyr).m_switchweapon = WEP_NEXBALL;
189         WITHSELF(plyr, W_SwitchWeapon(plyr, WEP_NEXBALL));
190 }
191
192 void DropBall(entity ball, vector org, vector vel)
193 {
194         ball.effects |= autocvar_g_nexball_basketball_effects_default;
195         ball.effects &= ~EF_NOSHADOW;
196         ball.owner.effects &= ~autocvar_g_nexball_basketball_effects_default;
197
198         setattachment(ball, world, "");
199         setorigin(ball, org);
200         ball.movetype = MOVETYPE_BOUNCE;
201         UNSET_ONGROUND(ball);
202         ball.scale = ball_scale;
203         ball.velocity = vel;
204         ball.nb_droptime = time;
205         settouch(ball, basketball_touch);
206         setthink(ball, ResetBall);
207         ball.nextthink = min(time + autocvar_g_nexball_delay_idle, ball.teamtime);
208
209         if(ball.owner.metertime)
210         {
211                 ball.owner.metertime = 0;
212                 .entity weaponentity = weaponentities[0]; // TODO: find ballstealer
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(entity this)
225 {
226         if(gameover) return;
227         UNSET_ONGROUND(this);
228         this.movetype = MOVETYPE_BOUNCE;
229         if(this.classname == "nexball_basketball")
230                 settouch(this, basketball_touch);
231         else if(this.classname == "nexball_football")
232                 settouch(this, football_touch);
233         this.cnt = 0;
234         setthink(this, ResetBall);
235         this.nextthink = time + autocvar_g_nexball_delay_idle + 3;
236         this.teamtime = 0;
237         this.pusher = world;
238         this.team = false;
239         _sound(this, CH_TRIGGER, this.noise1, VOL_BASE, ATTEN_NORM);
240         WaypointSprite_Ping(this.waypointsprite_attachedforcarrier);
241         LogNB("init", world);
242 }
243
244 void ResetBall(entity this)
245 {
246         if(this.cnt < 2)        // step 1
247         {
248                 if(time == this.teamtime)
249                         bprint("The ", Team_ColoredFullName(this.team), " held the ball for too long.\n");
250
251                 settouch(this, func_null);
252                 this.movetype = MOVETYPE_NOCLIP;
253                 this.velocity = '0 0 0'; // just in case?
254                 if(!this.cnt)
255                         LogNB("resetidle", world);
256                 this.cnt = 2;
257                 this.nextthink = time;
258         }
259         else if(this.cnt < 4)     // step 2 and 3
260         {
261 //              dprint("Step ", ftos(this.cnt), ": Calculated velocity: ", vtos(this.spawnorigin - this.origin), ", time: ", ftos(time), "\n");
262                 this.velocity = (this.spawnorigin - this.origin) * (this.cnt - 1); // 1 or 0.5 second movement
263                 this.nextthink = time + 0.5;
264                 this.cnt += 1;
265         }
266         else     // step 4
267         {
268 //              dprint("Step 4: time: ", ftos(time), "\n");
269                 if(vlen(this.origin - this.spawnorigin) > 10)  // should not happen anymore
270                         LOG_TRACE("The ball moved too far away from its spawn origin.\nOffset: ",
271                                    vtos(this.origin - this.spawnorigin), " Velocity: ", vtos(this.velocity), "\n");
272                 this.velocity = '0 0 0';
273                 setorigin(this, this.spawnorigin); // make sure it's positioned correctly anyway
274                 this.movetype = MOVETYPE_NONE;
275                 setthink(this, InitBall);
276                 this.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
277         }
278 }
279
280 void football_touch(entity this)
281 {
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(entity this)
327 {
328         if(other.ballcarried)
329         {
330                 football_touch(this);
331                 return;
332         }
333         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))
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(entity this)
349 {
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         setthink(ball, ResetBall);
432         if(ball.classname == "nexball_basketball")
433                 settouch(ball, 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(this);
445                 return;
446         }
447         this.team = this.cnt + 1;
448 }
449
450 void nb_spawnteam(string teamname, float teamcolor)
451 {
452         LOG_TRACE("^2spawned team ", teamname, "\n");
453         entity e = new(nexball_team);
454         e.netname = teamname;
455         e.cnt = teamcolor;
456         e.team = e.cnt + 1;
457         nb_teams += 1;
458 }
459
460 void nb_spawnteams()
461 {
462         bool t_red = false, t_blue = false, t_yellow = false, t_pink = false;
463         entity e;
464         for(e = world; (e = find(e, classname, "nexball_goal"));)
465         {
466                 switch(e.team)
467                 {
468                 case NUM_TEAM_1:
469                         if(!t_red)
470                         {
471                                 nb_spawnteam("Red", e.team-1)   ;
472                                 t_red = true;
473                         }
474                         break;
475                 case NUM_TEAM_2:
476                         if(!t_blue)
477                         {
478                                 nb_spawnteam("Blue", e.team-1)  ;
479                                 t_blue = true;
480                         }
481                         break;
482                 case NUM_TEAM_3:
483                         if(!t_yellow)
484                         {
485                                 nb_spawnteam("Yellow", e.team-1);
486                                 t_yellow = true;
487                         }
488                         break;
489                 case NUM_TEAM_4:
490                         if(!t_pink)
491                         {
492                                 nb_spawnteam("Pink", e.team-1)  ;
493                                 t_pink = true;
494                         }
495                         break;
496                 }
497         }
498 }
499
500 void nb_delayedinit(entity this)
501 {
502         if(find(world, classname, "nexball_team") == world)
503                 nb_spawnteams();
504         nb_ScoreRules(nb_teams);
505 }
506
507
508 //=======================//
509 //        spawnfuncs       //
510 //=======================//
511
512 void SpawnBall(entity this)
513 {
514         if(!g_nexball) { remove(this); return; }
515
516 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
517
518         if(this.model == "")
519         {
520                 this.model = "models/nexball/ball.md3";
521                 this.scale = 1.3;
522         }
523
524         precache_model(this.model);
525         _setmodel(this, this.model);
526         setsize(this, BALL_MINS, BALL_MAXS);
527         ball_scale = this.scale;
528
529         relocate_nexball(this);
530         this.spawnorigin = this.origin;
531
532         this.effects = this.effects | EF_LOWPRECISION;
533
534         if(cvar(strcat("g_", this.classname, "_trail")))  //nexball_basketball :p
535         {
536                 this.glow_color = autocvar_g_nexball_trail_color;
537                 this.glow_trail = true;
538         }
539
540         this.movetype = MOVETYPE_FLY;
541
542         if(!autocvar_g_nexball_sound_bounce)
543                 this.noise = "";
544         else if(this.noise == "")
545                 this.noise = strzone(SND(NB_BOUNCE));
546         //bounce sound placeholder (FIXME)
547         if(this.noise1 == "")
548                 this.noise1 = strzone(SND(NB_DROP));
549         //ball drop sound placeholder (FIXME)
550         if(this.noise2 == "")
551                 this.noise2 = strzone(SND(NB_STEAL));
552         //stealing sound placeholder (FIXME)
553         if(this.noise) precache_sound(this.noise);
554         precache_sound(this.noise1);
555         precache_sound(this.noise2);
556
557         WaypointSprite_AttachCarrier(WP_NbBall, this, RADARICON_FLAGCARRIER); // the ball's team is not set yet, no rule update needed
558
559         this.reset = ball_restart;
560         setthink(this, InitBall);
561         this.nextthink = game_starttime + autocvar_g_nexball_delay_start;
562 }
563
564 spawnfunc(nexball_basketball)
565 {
566         nexball_mode |= NBM_BASKETBALL;
567         this.classname = "nexball_basketball";
568         if (!(balls & BALL_BASKET))
569         {
570                 /*
571                 CVTOV(g_nexball_basketball_effects_default);
572                 CVTOV(g_nexball_basketball_delay_hold);
573                 CVTOV(g_nexball_basketball_delay_hold_forteam);
574                 CVTOV(g_nexball_basketball_teamsteal);
575                 */
576                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
577         }
578         if(!this.effects)
579                 this.effects = autocvar_g_nexball_basketball_effects_default;
580         this.solid = SOLID_TRIGGER;
581         this.pushable = autocvar_g_nexball_basketball_jumppad;
582         balls |= BALL_BASKET;
583         this.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
584         this.bouncestop = autocvar_g_nexball_basketball_bouncestop;
585         SpawnBall(this);
586 }
587
588 spawnfunc(nexball_football)
589 {
590         nexball_mode |= NBM_FOOTBALL;
591         this.classname = "nexball_football";
592         this.solid = SOLID_TRIGGER;
593         balls |= BALL_FOOT;
594         this.pushable = autocvar_g_nexball_football_jumppad;
595         this.bouncefactor = autocvar_g_nexball_football_bouncefactor;
596         this.bouncestop = autocvar_g_nexball_football_bouncestop;
597         SpawnBall(this);
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(entity this)
611 {
612         if(!g_nexball) { remove(this); return; }
613
614         EXACTTRIGGER_INIT;
615
616         if(this.team != GOAL_OUT && Team_TeamToNumber(this.team) != -1)
617         {
618                 entity wp = WaypointSprite_SpawnFixed(WP_NbGoal, (this.absmin + this.absmax) * 0.5, this, sprite, RADARICON_NONE);
619                 wp.colormod = ((this.team) ? Team_ColorRGB(this.team) : '1 0.5 0');
620                 this.sprite.customizeentityforclient = nb_Goal_Customize;
621         }
622
623         this.classname = "nexball_goal";
624         if(this.noise == "")
625                 this.noise = "ctf/respawn.wav";
626         precache_sound(this.noise);
627         settouch(this, GoalTouch);
628 }
629
630 spawnfunc(nexball_redgoal)
631 {
632         this.team = NUM_TEAM_1;
633         SpawnGoal(this);
634 }
635 spawnfunc(nexball_bluegoal)
636 {
637         this.team = NUM_TEAM_2;
638         SpawnGoal(this);
639 }
640 spawnfunc(nexball_yellowgoal)
641 {
642         this.team = NUM_TEAM_3;
643         SpawnGoal(this);
644 }
645 spawnfunc(nexball_pinkgoal)
646 {
647         this.team = NUM_TEAM_4;
648         SpawnGoal(this);
649 }
650
651 spawnfunc(nexball_fault)
652 {
653         this.team = GOAL_FAULT;
654         if(this.noise == "")
655                 this.noise = strzone(SND(TYPEHIT));
656         SpawnGoal(this);
657 }
658
659 spawnfunc(nexball_out)
660 {
661         this.team = GOAL_OUT;
662         if(this.noise == "")
663                 this.noise = strzone(SND(TYPEHIT));
664         SpawnGoal(this);
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(entity this)
707 {
708         //dprint("W_Nexball_Think\n");
709         //vector new_dir = steerlib_arrive(this.enemy.origin, 2500);
710         vector new_dir = normalize(this.enemy.origin + '0 0 50' - this.origin);
711         vector old_dir = normalize(this.velocity);
712         float _speed = vlen(this.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         this.velocity = new_vel;
717
718         this.nextthink = time;
719 }
720
721 void W_Nexball_Touch(entity this)
722 {
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) && !STAT(FROZEN, other) && !IS_DEAD(other) && (IS_PLAYER(attacker)))
731                 {
732                         other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
733                         UNSET_ONGROUND(other);
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()
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                 setthink(_ball, 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 = new(ballstealer);
807
808         missile.owner = self;
809
810         missile.movetype = MOVETYPE_FLY;
811         PROJECTILE_MAKETRIGGER(missile);
812
813         //setmodel(missile, "models/elaser.mdl");  // precision set below
814         setsize(missile, '0 0 0', '0 0 0');
815         setorigin(missile, w_shotorg);
816
817         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
818         missile.angles = vectoangles(missile.velocity);
819         settouch(missile, W_Nexball_Touch);
820         setthink(missile, SUB_Remove);
821         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
822
823         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
824         missile.flags = FL_PROJECTILE;
825
826         CSQCProjectile(missile, true, PROJECTILE_ELECTRO, true);
827 }
828
829 float ball_customize()
830 {SELFPARAM();
831         if(!self.owner)
832         {
833                 self.effects &= ~EF_FLAME;
834                 self.scale = 1;
835                 self.customizeentityforclient = func_null;
836                 return true;
837         }
838
839         if(other == self.owner)
840         {
841                 self.scale = autocvar_g_nexball_viewmodel_scale;
842                 if(self.enemy)
843                         self.effects |= EF_FLAME;
844                 else
845                         self.effects &= ~EF_FLAME;
846         }
847         else
848         {
849                 self.effects &= ~EF_FLAME;
850                 self.scale = 1;
851         }
852
853         return true;
854 }
855
856 METHOD(BallStealer, wr_think, void(BallStealer thiswep, entity actor, .entity weaponentity, int fire))
857 {
858     SELFPARAM();
859     TC(BallStealer, thiswep);
860     if(fire & 1)
861         if(weapon_prepareattack(thiswep, actor, weaponentity, false, autocvar_g_balance_nexball_primary_refire))
862             if(autocvar_g_nexball_basketball_meter)
863             {
864                 if(self.ballcarried && !self.metertime)
865                     self.metertime = time;
866                 else
867                     weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
868             }
869             else
870             {
871                 W_Nexball_Attack(-1);
872                 weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
873             }
874     if(fire & 2)
875         if(weapon_prepareattack(thiswep, actor, weaponentity, true, autocvar_g_balance_nexball_secondary_refire))
876         {
877             W_Nexball_Attack2();
878             weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
879         }
880
881     if(!(fire & 1) && self.metertime && self.ballcarried)
882     {
883         W_Nexball_Attack(time - self.metertime);
884         // DropBall or stealing will set metertime back to 0
885         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
886     }
887 }
888
889 METHOD(BallStealer, wr_setup, void(BallStealer this, entity actor))
890 {
891     TC(BallStealer, this);
892     //weapon_setup(WEP_PORTO.m_id);
893 }
894
895 METHOD(BallStealer, wr_checkammo1, bool(BallStealer this, entity actor))
896 {
897     TC(BallStealer, this);
898     return true;
899 }
900
901 METHOD(BallStealer, wr_checkammo2, bool(BallStealer this, entity actor))
902 {
903     TC(BallStealer, this);
904     return true;
905 }
906
907 void nb_DropBall(entity player)
908 {
909         if(player.ballcarried && g_nexball)
910                 DropBall(player.ballcarried, player.origin, player.velocity);
911 }
912
913 MUTATOR_HOOKFUNCTION(nb, ClientDisconnect)
914 {SELFPARAM();
915         nb_DropBall(self);
916         return false;
917 }
918
919 MUTATOR_HOOKFUNCTION(nb, PlayerDies)
920 {
921         nb_DropBall(frag_target);
922         return false;
923 }
924
925 MUTATOR_HOOKFUNCTION(nb, MakePlayerObserver)
926 {SELFPARAM();
927         nb_DropBall(self);
928         return false;
929 }
930
931 MUTATOR_HOOKFUNCTION(nb, PlayerPreThink)
932 {SELFPARAM();
933         makevectors(self.v_angle);
934         if(nexball_mode & NBM_BASKETBALL)
935         {
936                 if(self.ballcarried)
937                 {
938                         // 'view ball'
939                         self.ballcarried.velocity = self.velocity;
940                         self.ballcarried.customizeentityforclient = ball_customize;
941
942                         vector org = self.origin + self.view_ofs +
943                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
944                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
945                                           v_up * autocvar_g_nexball_viewmodel_offset.z;
946                         setorigin(self.ballcarried, org);
947
948                         // 'safe passing'
949                         if(autocvar_g_nexball_safepass_maxdist)
950                         {
951                                 if(self.ballcarried.wait < time && self.ballcarried.enemy)
952                                 {
953                                         //centerprint(self, sprintf("Lost lock on %s", self.ballcarried.enemy.netname));
954                                         self.ballcarried.enemy = world;
955                                 }
956
957
958                                 //tracebox(self.origin + self.view_ofs, '-2 -2 -2', '2 2 2', self.origin + self.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
959                                 crosshair_trace(self);
960                                 if( trace_ent &&
961                                         IS_CLIENT(trace_ent) &&
962                                         !IS_DEAD(trace_ent) &&
963                                         trace_ent.team == self.team &&
964                                         vlen(trace_ent.origin - self.origin) <= autocvar_g_nexball_safepass_maxdist )
965                                 {
966
967                                         //if(self.ballcarried.enemy != trace_ent)
968                                         //      centerprint(self, sprintf("Locked to %s", trace_ent.netname));
969                                         self.ballcarried.enemy = trace_ent;
970                                         self.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
971
972
973                                 }
974                         }
975                 }
976                 else
977                 {
978                         .entity weaponentity = weaponentities[0]; // TODO
979                         if(self.(weaponentity).weapons)
980                         {
981                                 self.weapons = self.(weaponentity).weapons;
982                                 Weapon w = WEP_NEXBALL;
983                                 w.wr_resetplayer(w, self);
984                                 PS(self).m_switchweapon = self.(weaponentity).m_switchweapon;
985                                 W_SwitchWeapon(self, PS(self).m_switchweapon);
986
987                                 self.(weaponentity).weapons = '0 0 0';
988                         }
989                 }
990
991         }
992
993         nexball_setstatus();
994
995         return false;
996 }
997
998 MUTATOR_HOOKFUNCTION(nb, SpectateCopy)
999 {
1000         SELFPARAM();
1001         this.metertime = other.metertime;
1002 }
1003
1004 MUTATOR_HOOKFUNCTION(nb, PlayerSpawn)
1005 {
1006         SELFPARAM();
1007         this.metertime = 0;
1008         .entity weaponentity = weaponentities[0];
1009         this.(weaponentity).weapons = '0 0 0';
1010
1011         if (nexball_mode & NBM_BASKETBALL)
1012                 this.weapons |= WEPSET(NEXBALL);
1013         else
1014                 this.weapons = '0 0 0';
1015
1016         return false;
1017 }
1018
1019 .float stat_sv_airspeedlimit_nonqw;
1020 .float stat_sv_maxspeed;
1021
1022 MUTATOR_HOOKFUNCTION(nb, PlayerPhysics)
1023 {SELFPARAM();
1024         if(self.ballcarried)
1025         {
1026                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_nexball_basketball_carrier_highspeed;
1027                 self.stat_sv_maxspeed *= autocvar_g_nexball_basketball_carrier_highspeed;
1028         }
1029         return false;
1030 }
1031
1032 MUTATOR_HOOKFUNCTION(nb, ForbidThrowCurrentWeapon)
1033 {SELFPARAM();
1034         return PS(self).m_weapon == WEP_NEXBALL;
1035 }
1036
1037 MUTATOR_HOOKFUNCTION(nb, ForbidDropCurrentWeapon)
1038 {SELFPARAM();
1039         return PS(self).m_weapon == WEP_MORTAR; // TODO: what is this for?
1040 }
1041
1042 MUTATOR_HOOKFUNCTION(nb, FilterItem)
1043 {SELFPARAM();
1044         if(self.classname == "droppedweapon")
1045         if(self.weapon == WEP_NEXBALL.m_id)
1046                 return true;
1047
1048         return false;
1049 }
1050
1051 MUTATOR_HOOKFUNCTION(nb, GetTeamCount)
1052 {
1053         ret_string = "nexball_team";
1054         return true;
1055 }
1056
1057 MUTATOR_HOOKFUNCTION(nb, WantWeapon)
1058 {
1059         ret_float = 0; // weapon is set a few lines later, apparently
1060         return true;
1061 }
1062
1063 MUTATOR_HOOKFUNCTION(nb, DropSpecialItems)
1064 {
1065         if(frag_target.ballcarried)
1066                 DropBall(frag_target.ballcarried, frag_target.origin, frag_target.velocity);
1067
1068         return false;
1069 }
1070
1071 MUTATOR_HOOKFUNCTION(nb, SendWaypoint)
1072 {
1073         wp_sendflags &= ~0x80;
1074         return false;
1075 }
1076
1077 REGISTER_MUTATOR(nb, g_nexball)
1078 {
1079         MUTATOR_ONADD
1080         {
1081                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
1082                 if(g_nexball_meter_period <= 0)
1083                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
1084                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
1085
1086                 // General settings
1087                 /*
1088                 CVTOV(g_nexball_football_boost_forward);   //100
1089                 CVTOV(g_nexball_football_boost_up);             //200
1090                 CVTOV(g_nexball_delay_idle);                       //10
1091                 CVTOV(g_nexball_football_physics);               //0
1092                 */
1093                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
1094
1095                 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
1096                 WEP_NEXBALL.spawnflags &= ~WEP_FLAG_MUTATORBLOCKED;
1097
1098                 ActivateTeamplay();
1099                 SetLimits(autocvar_g_nexball_goallimit, autocvar_g_nexball_goalleadlimit, autocvar_timelimit_override, -1);
1100                 have_team_spawns = -1; // request team spawns
1101         }
1102
1103         MUTATOR_ONROLLBACK_OR_REMOVE
1104         {
1105                 WEP_NEXBALL.spawnflags |= WEP_FLAG_MUTATORBLOCKED;
1106                 // we actually cannot roll back nb_delayedinit here
1107                 // BUT: we don't need to! If this gets called, adding always
1108                 // succeeds.
1109         }
1110
1111         MUTATOR_ONREMOVE
1112         {
1113                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
1114                 return -1;
1115         }
1116
1117         return 0;
1118 }
1119
1120 #endif
1121 #endif