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