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