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