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