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