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