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