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