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