]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/mutators/gamemode_nexball.qc
10690001d49dd2dfa2aa20c63fef302ea931ec2b
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_nexball.qc
1 #include "gamemode_nexball.qh"
2 #include "../_all.qh"
3
4 #include "gamemode.qh"
5
6 float autocvar_g_nexball_basketball_bouncefactor;
7 float autocvar_g_nexball_basketball_bouncestop;
8 float autocvar_g_nexball_basketball_carrier_highspeed;
9 bool autocvar_g_nexball_basketball_meter;
10 float autocvar_g_nexball_basketball_meter_maxpower;
11 float autocvar_g_nexball_basketball_meter_minpower;
12 float autocvar_g_nexball_delay_collect;
13 float autocvar_g_nexball_delay_goal;
14 float autocvar_g_nexball_delay_start;
15 float autocvar_g_nexball_football_bouncefactor;
16 float autocvar_g_nexball_football_bouncestop;
17 bool autocvar_g_nexball_radar_showallplayers;
18 bool autocvar_g_nexball_sound_bounce;
19 int autocvar_g_nexball_trail_color;
20
21 float autocvar_g_nexball_safepass_turnrate;
22 float autocvar_g_nexball_safepass_maxdist;
23 float autocvar_g_nexball_safepass_holdtime;
24 float autocvar_g_nexball_viewmodel_scale;
25 float autocvar_g_nexball_tackling;
26 vector autocvar_g_nexball_viewmodel_offset;
27
28 void basketball_touch();
29 void football_touch();
30 void ResetBall();
31 const float NBM_NONE = 0;
32 const float NBM_FOOTBALL = 2;
33 const float NBM_BASKETBALL = 4;
34 float nexball_mode;
35
36 float OtherTeam(float t)  //works only if there are two teams on the map!
37 {
38         entity e;
39         e = find(world, classname, "nexball_team");
40         if(e.team == t)
41                 e = find(e, classname, "nexball_team");
42         return e.team;
43 }
44
45 const float ST_NEXBALL_GOALS = 1;
46 const float SP_NEXBALL_GOALS = 4;
47 const float SP_NEXBALL_FAULTS = 5;
48 void nb_ScoreRules(float teams)
49 {
50         ScoreRules_basics(teams, 0, 0, true);
51         ScoreInfo_SetLabel_TeamScore(   ST_NEXBALL_GOALS,  "goals", SFL_SORT_PRIO_PRIMARY);
52         ScoreInfo_SetLabel_PlayerScore( SP_NEXBALL_GOALS,  "goals", SFL_SORT_PRIO_PRIMARY);
53         ScoreInfo_SetLabel_PlayerScore(SP_NEXBALL_FAULTS, "faults", SFL_SORT_PRIO_SECONDARY | SFL_LOWER_IS_BETTER);
54         ScoreRules_basics_end();
55 }
56
57 void LogNB(string mode, entity actor)
58 {
59         string s;
60         if(!autocvar_sv_eventlog)
61                 return;
62         s = strcat(":nexball:", mode);
63         if(actor != world)
64                 s = strcat(s, ":", ftos(actor.playerid));
65         GameLogEcho(s);
66 }
67
68 void ball_restart(void)
69 {SELFPARAM();
70         if(self.owner)
71                 DropBall(self, self.owner.origin, '0 0 0');
72         ResetBall();
73 }
74
75 void nexball_setstatus(void)
76 {SELFPARAM();
77         self.items &= ~IT_KEY1;
78         if(self.ballcarried)
79         {
80                 if(self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
81                 {
82                         bprint("The ", Team_ColoredFullName(self.team), " held the ball for too long.\n");
83                         setself(self.ballcarried);
84                         DropBall(self, self.owner.origin, '0 0 0');
85                         ResetBall();
86                         setself(this);
87                 }
88                 else
89                         self.items |= IT_KEY1;
90         }
91 }
92
93 void relocate_nexball(void)
94 {SELFPARAM();
95         tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, true, self);
96         if(trace_startsolid)
97         {
98                 vector o;
99                 o = self.origin;
100                 if(!move_out_of_solid(self))
101                         objerror("could not get out of solid at all!");
102                 LOG_INFO("^1NOTE: this map needs FIXING. ", self.classname, " at ", vtos(o - '0 0 1'));
103                 LOG_INFO(" needs to be moved out of solid, e.g. by '", ftos(self.origin.x - o.x));
104                 LOG_INFO(" ", ftos(self.origin.y - o.y));
105                 LOG_INFO(" ", ftos(self.origin.z - o.z), "'\n");
106                 self.origin = o;
107         }
108 }
109
110 void DropOwner(void)
111 {SELFPARAM();
112         entity ownr;
113         ownr = self.owner;
114         DropBall(self, ownr.origin, ownr.velocity);
115         makevectors(ownr.v_angle.y * '0 1 0');
116         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
117         ownr.flags &= ~FL_ONGROUND;
118 }
119
120 void GiveBall(entity plyr, entity ball)
121 {SELFPARAM();
122         entity ownr;
123
124         if((ownr = ball.owner))
125         {
126                 ownr.effects &= ~autocvar_g_nexball_basketball_effects_default;
127                 ownr.ballcarried = world;
128                 if(ownr.metertime)
129                 {
130                         ownr.metertime = 0;
131                         ownr.weaponentity.state = WS_READY;
132                 }
133                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
134         }
135         else
136         {
137                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
138         }
139
140         //setattachment(ball, plyr, "");
141         setorigin(ball, plyr.origin + plyr.view_ofs);
142
143         if(ball.team != plyr.team)
144                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
145
146         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
147         ball.team = plyr.team;
148         plyr.ballcarried = ball;
149         ball.nb_dropper = plyr;
150
151         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
152         ball.effects &= ~autocvar_g_nexball_basketball_effects_default;
153
154         ball.velocity = '0 0 0';
155         ball.movetype = MOVETYPE_NONE;
156         ball.touch = func_null;
157         ball.effects |= EF_NOSHADOW;
158         ball.scale = 1; // scale down.
159
160         WaypointSprite_AttachCarrier(WP_NbBall, plyr, RADARICON_FLAGCARRIER);
161         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
162
163         if(autocvar_g_nexball_basketball_delay_hold)
164         {
165                 ball.think = DropOwner;
166                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
167         }
168
169         plyr.weaponentity.weapons = plyr.weapons;
170         plyr.weaponentity.switchweapon = plyr.weapon;
171         plyr.weapons = WEPSET_NEXBALL;
172         setself(plyr);
173         WEP_ACTION(WEP_NEXBALL, WR_RESETPLAYER);
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 void spawnfunc_nexball_team(void)
427 {SELFPARAM();
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 void spawnfunc_nexball_basketball(void)
553 {SELFPARAM();
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 void spawnfunc_nexball_football(void)
576 {SELFPARAM();
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 void spawnfunc_nexball_redgoal(void)
617 {SELFPARAM();
618         self.team = NUM_TEAM_1;
619         SpawnGoal();
620 }
621 void spawnfunc_nexball_bluegoal(void)
622 {SELFPARAM();
623         self.team = NUM_TEAM_2;
624         SpawnGoal();
625 }
626 void spawnfunc_nexball_yellowgoal(void)
627 {SELFPARAM();
628         self.team = NUM_TEAM_3;
629         SpawnGoal();
630 }
631 void spawnfunc_nexball_pinkgoal(void)
632 {SELFPARAM();
633         self.team = NUM_TEAM_4;
634         SpawnGoal();
635 }
636
637 void spawnfunc_nexball_fault(void)
638 {SELFPARAM();
639         self.team = GOAL_FAULT;
640         if(self.noise == "")
641                 self.noise = SND(TYPEHIT);
642         SpawnGoal();
643 }
644
645 void spawnfunc_nexball_out(void)
646 {SELFPARAM();
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 void spawnfunc_ball(void)
658 {
659         spawnfunc_nexball_football();
660 }
661 void spawnfunc_ball_football(void)
662 {
663         spawnfunc_nexball_football();
664 }
665 void spawnfunc_ball_basketball(void)
666 {
667         spawnfunc_nexball_basketball();
668 }
669 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
670 void spawnfunc_ball_redgoal(void)
671 {
672         spawnfunc_nexball_bluegoal();   // I blame Revenant
673 }
674 void spawnfunc_ball_bluegoal(void)
675 {
676         spawnfunc_nexball_redgoal();    // but he didn't mean to cause trouble :p
677 }
678 void spawnfunc_ball_fault(void)
679 {
680         spawnfunc_nexball_fault();
681 }
682 void spawnfunc_ball_bound(void)
683 {
684         spawnfunc_nexball_out();
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 void W_Nexball_Attack2(void)
775 {SELFPARAM();
776         if(self.ballcarried.enemy)
777         {
778                 entity _ball = self.ballcarried;
779                 W_SetupShot(self, false, 4, SND(NB_SHOOT1), CH_WEAPON_A, 0);
780                 DropBall(_ball, w_shotorg, trigger_push_calculatevelocity(_ball.origin, _ball.enemy, 32));
781                 _ball.think = W_Nexball_Think;
782                 _ball.nextthink = time;
783                 return;
784         }
785
786         if(!autocvar_g_nexball_tackling)
787                 return;
788
789         entity missile;
790         if(!(balls & BALL_BASKET))
791                 return;
792         W_SetupShot(self, false, 2, SND(NB_SHOOT2), CH_WEAPON_A, 0);
793         missile = spawn();
794
795         missile.owner = self;
796         missile.classname = "ballstealer";
797
798         missile.movetype = MOVETYPE_FLY;
799         PROJECTILE_MAKETRIGGER(missile);
800
801         //setmodel(missile, "models/elaser.mdl");  // precision set below
802         setsize(missile, '0 0 0', '0 0 0');
803         setorigin(missile, w_shotorg);
804
805         W_SetupProjVelocity_Basic(missile, autocvar_g_balance_nexball_secondary_speed, 0);
806         missile.angles = vectoangles(missile.velocity);
807         missile.touch = W_Nexball_Touch;
808         missile.think = SUB_Remove;
809         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
810
811         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
812         missile.flags = FL_PROJECTILE;
813
814         CSQCProjectile(missile, true, PROJECTILE_ELECTRO, true);
815 }
816
817 float ball_customize()
818 {SELFPARAM();
819         if(!self.owner)
820         {
821                 self.effects &= ~EF_FLAME;
822                 self.scale = 1;
823                 self.customizeentityforclient = func_null;
824                 return true;
825         }
826
827         if(other == self.owner)
828         {
829                 self.scale = autocvar_g_nexball_viewmodel_scale;
830                 if(self.enemy)
831                         self.effects |= EF_FLAME;
832                 else
833                         self.effects &= ~EF_FLAME;
834         }
835         else
836         {
837                 self.effects &= ~EF_FLAME;
838                 self.scale = 1;
839         }
840
841         return true;
842 }
843
844 float w_nexball_weapon(float req);
845 METHOD(BallStealer, weapon_func, bool(entity this, int req)) {
846         return w_nexball_weapon(req);
847 }
848
849 float w_nexball_weapon(float req)
850 {SELFPARAM();
851         if(req == WR_THINK)
852         {
853                 if(self.BUTTON_ATCK)
854                         if(weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
855                                 if(autocvar_g_nexball_basketball_meter)
856                                 {
857                                         if(self.ballcarried && !self.metertime)
858                                                 self.metertime = time;
859                                         else
860                                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
861                                 }
862                                 else
863                                 {
864                                         W_Nexball_Attack(-1);
865                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
866                                 }
867                 if(self.BUTTON_ATCK2)
868                         if(weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
869                         {
870                                 W_Nexball_Attack2();
871                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
872                         }
873
874                 if(!self.BUTTON_ATCK && self.metertime && self.ballcarried)
875                 {
876                         W_Nexball_Attack(time - self.metertime);
877                         // DropBall or stealing will set metertime back to 0
878                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
879                 }
880         }
881         else if(req == WR_INIT)
882         {
883         }
884         else if(req == WR_SETUP)
885         {
886                 //weapon_setup(WEP_PORTO.m_id);
887         }
888         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return true
889         return true;
890 }
891
892 MUTATOR_HOOKFUNCTION(nexball_BallDrop)
893 {SELFPARAM();
894         if(self.ballcarried && g_nexball)
895                 DropBall(self.ballcarried, self.origin, self.velocity);
896
897         return 0;
898 }
899
900 MUTATOR_HOOKFUNCTION(nexball_PlayerPreThink)
901 {SELFPARAM();
902         makevectors(self.v_angle);
903         if(nexball_mode & NBM_BASKETBALL)
904         {
905                 if(self.ballcarried)
906                 {
907                         // 'view ball'
908                         self.ballcarried.velocity = self.velocity;
909                         self.ballcarried.customizeentityforclient = ball_customize;
910
911                         setorigin(self.ballcarried, self.origin + self.view_ofs +
912                                           v_forward * autocvar_g_nexball_viewmodel_offset.x +
913                                           v_right * autocvar_g_nexball_viewmodel_offset.y +
914                                           v_up * autocvar_g_nexball_viewmodel_offset.z);
915
916                         // 'safe passing'
917                         if(autocvar_g_nexball_safepass_maxdist)
918                         {
919                                 if(self.ballcarried.wait < time && self.ballcarried.enemy)
920                                 {
921                                         //centerprint(self, sprintf("Lost lock on %s", self.ballcarried.enemy.netname));
922                                         self.ballcarried.enemy = world;
923                                 }
924
925
926                                 //tracebox(self.origin + self.view_ofs, '-2 -2 -2', '2 2 2', self.origin + self.view_ofs + v_forward * autocvar_g_nexball_safepass_maxdist);
927                                 crosshair_trace(self);
928                                 if( trace_ent &&
929                                         IS_CLIENT(trace_ent) &&
930                                         trace_ent.deadflag == DEAD_NO &&
931                                         trace_ent.team == self.team &&
932                                         vlen(trace_ent.origin - self.origin) <= autocvar_g_nexball_safepass_maxdist )
933                                 {
934
935                                         //if(self.ballcarried.enemy != trace_ent)
936                                         //      centerprint(self, sprintf("Locked to %s", trace_ent.netname));
937                                         self.ballcarried.enemy = trace_ent;
938                                         self.ballcarried.wait = time + autocvar_g_nexball_safepass_holdtime;
939
940
941                                 }
942                         }
943                 }
944                 else
945                 {
946                         if(self.weaponentity.weapons)
947                         {
948                                 self.weapons = self.weaponentity.weapons;
949                                 WEP_ACTION(WEP_NEXBALL, WR_RESETPLAYER);
950                                 self.switchweapon = self.weaponentity.switchweapon;
951                                 W_SwitchWeapon(self.switchweapon);
952
953                 self.weaponentity.weapons = '0 0 0';
954                         }
955                 }
956
957         }
958
959         nexball_setstatus();
960
961         return false;
962 }
963
964 MUTATOR_HOOKFUNCTION(nexball_PlayerSpawn)
965 {SELFPARAM();
966         self.weaponentity.weapons = '0 0 0';
967
968         if(nexball_mode & NBM_BASKETBALL)
969                 self.weapons |= WEPSET_NEXBALL;
970         else
971                 self.weapons = '0 0 0';
972
973         return false;
974 }
975
976 MUTATOR_HOOKFUNCTION(nexball_PlayerPhysics)
977 {SELFPARAM();
978         if(self.ballcarried)
979         {
980                 self.stat_sv_airspeedlimit_nonqw *= autocvar_g_nexball_basketball_carrier_highspeed;
981                 self.stat_sv_maxspeed *= autocvar_g_nexball_basketball_carrier_highspeed;
982         }
983         return false;
984 }
985
986 MUTATOR_HOOKFUNCTION(nexball_ForbidThrowing)
987 {SELFPARAM();
988         if(self.weapon == WEP_MORTAR.m_id)
989                 return true;
990
991         return false;
992 }
993
994 MUTATOR_HOOKFUNCTION(nexball_FilterItem)
995 {SELFPARAM();
996         if(self.classname == "droppedweapon")
997         if(self.weapon == WEP_MORTAR.m_id)
998                 return true;
999
1000         return false;
1001 }
1002
1003 MUTATOR_DEFINITION(gamemode_nexball)
1004 {
1005         MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
1006         MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
1007         MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
1008         MUTATOR_HOOK(PlayerSpawn, nexball_PlayerSpawn, CBC_ORDER_ANY);
1009         MUTATOR_HOOK(PlayerPreThink, nexball_PlayerPreThink, CBC_ORDER_ANY);
1010         MUTATOR_HOOK(PlayerPhysics, nexball_PlayerPhysics, CBC_ORDER_ANY);
1011         MUTATOR_HOOK(ForbidThrowCurrentWeapon, nexball_ForbidThrowing, CBC_ORDER_ANY);
1012         MUTATOR_HOOK(FilterItem, nexball_FilterItem, CBC_ORDER_ANY);
1013
1014         MUTATOR_ONADD
1015         {
1016                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
1017                 if(g_nexball_meter_period <= 0)
1018                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
1019                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
1020                 addstat(STAT_NB_METERSTART, AS_FLOAT, metertime);
1021
1022                 // General settings
1023                 /*
1024                 CVTOV(g_nexball_football_boost_forward);   //100
1025                 CVTOV(g_nexball_football_boost_up);             //200
1026                 CVTOV(g_nexball_delay_idle);                       //10
1027                 CVTOV(g_nexball_football_physics);               //0
1028                 */
1029                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
1030
1031                 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
1032         }
1033
1034         MUTATOR_ONROLLBACK_OR_REMOVE
1035         {
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 }