]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_nexball.qc
Replace print calls with logger calls
[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 //      Send_Effect("grenadelauncher_muzzleflash", w_shotorg, w_shotdir * 1000, 1);
782         missile = spawn();
783
784         missile.owner = self;
785         missile.classname = "ballstealer";
786
787         missile.movetype = MOVETYPE_FLY;
788         PROJECTILE_MAKETRIGGER(missile);
789
790         //setmodel(missile, "models/elaser.mdl");  // precision set below
791         setsize(missile, '0 0 0', '0 0 0');
792         setorigin(missile, w_shotorg);
793
794         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
795         missile.angles = vectoangles(missile.velocity);
796         missile.touch = W_Nexball_Touch;
797         missile.think = SUB_Remove;
798         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
799
800         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
801         missile.flags = FL_PROJECTILE;
802
803         CSQCProjectile(missile, true, PROJECTILE_ELECTRO, true);
804 }
805
806 float ball_customize()
807 {
808         if(!self.owner)
809         {
810                 self.effects &= ~EF_FLAME;
811                 self.scale = 1;
812                 self.customizeentityforclient = func_null;
813                 return true;
814         }
815
816         if(other == self.owner)
817         {
818                 self.scale = autocvar_g_nexball_viewmodel_scale;
819                 if(self.enemy)
820                         self.effects |= EF_FLAME;
821                 else
822                         self.effects &= ~EF_FLAME;
823         }
824         else
825         {
826                 self.effects &= ~EF_FLAME;
827                 self.scale = 1;
828         }
829
830         return true;
831 }
832
833 float w_nexball_weapon(float req)
834 {
835         if(req == WR_THINK)
836         {
837                 if(self.BUTTON_ATCK)
838                         if(weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
839                                 if(autocvar_g_nexball_basketball_meter)
840                                 {
841                                         if(self.ballcarried && !self.metertime)
842                                                 self.metertime = time;
843                                         else
844                                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
845                                 }
846                                 else
847                                 {
848                                         W_Nexball_Attack(-1);
849                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
850                                 }
851                 if(self.BUTTON_ATCK2)
852                         if(weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
853                         {
854                                 W_Nexball_Attack2();
855                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
856                         }
857
858                 if(!self.BUTTON_ATCK && self.metertime && self.ballcarried)
859                 {
860                         W_Nexball_Attack(time - self.metertime);
861                         // DropBall or stealing will set metertime back to 0
862                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
863                 }
864         }
865         else if(req == WR_INIT)
866         {
867                 precache_model(W_Model("g_porto.md3"));
868                 precache_model(W_Model("v_porto.md3"));
869                 precache_model(W_Model("h_porto.iqm"));
870                 precache_model("models/elaser.mdl");
871                 precache_sound("nexball/shoot1.wav");
872                 precache_sound("nexball/shoot2.wav");
873                 precache_sound("misc/typehit.wav");
874         }
875         else if(req == WR_SETUP)
876         {
877                 //weapon_setup(WEP_PORTO.m_id);
878         }
879         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return true
880         return true;
881 }
882
883 MUTATOR_HOOKFUNCTION(nexball_BallDrop)
884 {
885         if(self.ballcarried && g_nexball)
886                 DropBall(self.ballcarried, self.origin, self.velocity);
887
888         return 0;
889 }
890
891 MUTATOR_HOOKFUNCTION(nexball_PlayerPreThink)
892 {
893         makevectors(self.v_angle);
894         if(nexball_mode & NBM_BASKETBALL)
895         {
896                 if(self.ballcarried)
897                 {
898                         // 'view ball'
899                         self.ballcarried.velocity = self.velocity;
900                         self.ballcarried.customizeentityforclient = ball_customize;
901
902                         setorigin(self.ballcarried, self.origin + self.view_ofs +
903                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
904                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
905                                           v_up * autocvar_g_nexball_viewmodel_offset.z);
906
907                         // 'safe passing'
908                         if(autocvar_g_nexball_safepass_maxdist)
909                         {
910                                 if(self.ballcarried.wait < time && self.ballcarried.enemy)
911                                 {
912                                         //centerprint(self, sprintf("Lost lock on %s", self.ballcarried.enemy.netname));
913                                         self.ballcarried.enemy = world;
914                                 }
915
916
917                                 //tracebox(self.origin + self.view_ofs, '-2 -2 -2', '2 2 2', self.origin + self.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
918                                 crosshair_trace(self);
919                                 if( trace_ent &&
920                                         IS_CLIENT(trace_ent) &&
921                                         trace_ent.deadflag == DEAD_NO &&
922                                         trace_ent.team == self.team &&
923                                         vlen(trace_ent.origin - self.origin) <= autocvar_g_nexball_safepass_maxdist )
924                                 {
925
926                                         //if(self.ballcarried.enemy != trace_ent)
927                                         //      centerprint(self, sprintf("Locked to %s", trace_ent.netname));
928                                         self.ballcarried.enemy = trace_ent;
929                                         self.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
930
931
932                                 }
933                         }
934                 }
935                 else
936                 {
937                         if(self.weaponentity.weapons)
938                         {
939                                 self.weapons = self.weaponentity.weapons;
940                                 WEP_ACTION(WEP_PORTO.m_id, WR_RESETPLAYER);
941                                 self.switchweapon = self.weaponentity.switchweapon;
942                                 W_SwitchWeapon(self.switchweapon);
943
944                 self.weaponentity.weapons = '0 0 0';
945                         }
946                 }
947
948         }
949
950         nexball_setstatus();
951
952         return false;
953 }
954
955 MUTATOR_HOOKFUNCTION(nexball_PlayerSpawn)
956 {
957         self.weaponentity.weapons = '0 0 0';
958
959         if(nexball_mode & NBM_BASKETBALL)
960                 self.weapons |= WEPSET_PORTO;
961         else
962                 self.weapons = '0 0 0';
963
964         return false;
965 }
966
967 MUTATOR_HOOKFUNCTION(nexball_PlayerPhysics)
968 {
969         if(self.ballcarried)
970         {
971                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_nexball_basketball_carrier_highspeed;
972                 self.stat_sv_maxspeed *= autocvar_g_nexball_basketball_carrier_highspeed;
973         }
974         return false;
975 }
976
977 MUTATOR_HOOKFUNCTION(nexball_SetStartItems)
978 {
979         start_items |= IT_UNLIMITED_SUPERWEAPONS; // FIXME BAD BAD BAD BAD HACK, NEXBALL SHOULDN'T ABUSE PORTO'S WEAPON SLOT
980
981         return false;
982 }
983
984 MUTATOR_HOOKFUNCTION(nexball_ForbidThrowing)
985 {
986         if(self.weapon == WEP_MORTAR.m_id)
987                 return true;
988
989         return false;
990 }
991
992 MUTATOR_HOOKFUNCTION(nexball_FilterItem)
993 {
994         if(self.classname == "droppedweapon")
995         if(self.weapon == WEP_MORTAR.m_id)
996                 return true;
997
998         return false;
999 }
1000
1001 MUTATOR_DEFINITION(gamemode_nexball)
1002 {
1003         MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
1004         MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
1005         MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
1006         MUTATOR_HOOK(PlayerSpawn, nexball_PlayerSpawn, CBC_ORDER_ANY);
1007         MUTATOR_HOOK(PlayerPreThink, nexball_PlayerPreThink, CBC_ORDER_ANY);
1008         MUTATOR_HOOK(PlayerPhysics, nexball_PlayerPhysics, CBC_ORDER_ANY);
1009         MUTATOR_HOOK(SetStartItems, nexball_SetStartItems, CBC_ORDER_ANY);
1010         MUTATOR_HOOK(ForbidThrowCurrentWeapon, nexball_ForbidThrowing, CBC_ORDER_ANY);
1011         MUTATOR_HOOK(FilterItem, nexball_FilterItem, CBC_ORDER_ANY);
1012
1013         MUTATOR_ONADD
1014         {
1015                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
1016                 if(g_nexball_meter_period <= 0)
1017                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
1018                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
1019                 addstat(STAT_NB_METERSTART, AS_FLOAT, metertime);
1020
1021                 // General settings
1022                 /*
1023                 CVTOV(g_nexball_football_boost_forward);   //100
1024                 CVTOV(g_nexball_football_boost_up);             //200
1025                 CVTOV(g_nexball_delay_idle);                       //10
1026                 CVTOV(g_nexball_football_physics);               //0
1027                 */
1028                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
1029
1030                 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
1031         }
1032
1033         MUTATOR_ONROLLBACK_OR_REMOVE
1034         {
1035                 // we actually cannot roll back nb_delayedinit here
1036                 // BUT: we don't need to! If this gets called, adding always
1037                 // succeeds.
1038         }
1039
1040         MUTATOR_ONREMOVE
1041         {
1042                 LOG_INFO("This is a game type and it cannot be removed at runtime.");
1043                 return -1;
1044         }
1045
1046         return 0;
1047 }