Formating, protos on top, mutator dec at end
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / gamemode_nexball.qc
1 void basketball_touch();
2 void football_touch();
3 void ResetBall();
4
5 float OtherTeam(float t)  //works only if there are two teams on the map!
6 {
7         entity e;
8         e = find(world, classname, "nexball_team");
9         if(e.team == t)
10                 e = find(e, classname, "nexball_team");
11         return e.team;
12 }
13
14
15 void LogNB(string mode, entity actor)
16 {
17         string s;
18         if(!autocvar_sv_eventlog)
19                 return;
20         s = strcat(":nexball:", mode);
21         if(actor != world)
22                 s = strcat(s, ":", ftos(actor.playerid));
23         GameLogEcho(s);
24 }
25
26 void ball_restart(void)
27 {
28         if(self.owner)
29                 DropBall(self, self.owner.origin, '0 0 0');
30         ResetBall();
31 }
32
33 void nexball_setstatus(void)
34 {
35         entity oldself;
36         self.items &~= IT_KEY1;
37         if(self.ballcarried)
38         {
39                 if(self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
40                 {
41                         bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
42                         oldself = self;
43                         self = self.ballcarried;
44                         DropBall(self, self.owner.origin, '0 0 0');
45                         ResetBall();
46                         self = oldself;
47                 }
48                 else
49                         self.items |= IT_KEY1;
50         }
51 }
52
53 void relocate_nexball(void)
54 {
55         tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, TRUE, self);
56         if(trace_startsolid)
57         {
58                 vector o;
59                 o = self.origin;
60                 if(!move_out_of_solid(self))
61                         objerror("could not get out of solid at all!");
62                 print("^1NOTE: this map needs FIXING. ", self.classname, " at ", vtos(o - '0 0 1'));
63                 print(" needs to be moved out of solid, e.g. by '", ftos(self.origin_x - o_x));
64                 print(" ", ftos(self.origin_y - o_y));
65                 print(" ", ftos(self.origin_z - o_z), "'\n");
66                 self.origin = o;
67         }
68 }
69
70 void DropOwner(void)
71 {
72         entity ownr;
73         ownr = self.owner;
74         DropBall(self, ownr.origin, ownr.velocity);
75         makevectors(ownr.v_angle_y * '0 1 0');
76         ownr.velocity += ('0 0 0.75' - v_forward) * 1000;
77         ownr.flags &~= FL_ONGROUND;
78 }
79
80 void GiveBall(entity plyr, entity ball)
81 {
82         entity ownr;
83
84         if((ownr = ball.owner))
85         {
86                 ownr.effects &~= autocvar_g_nexball_basketball_effects_default;
87                 ownr.ballcarried = world;
88                 if(ownr.metertime)
89                 {
90                         ownr.metertime = 0;
91                         ownr.weaponentity.state = WS_READY;
92                 }
93                 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
94         }
95         else
96         {
97                 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
98         }
99
100         setattachment(ball, plyr, "");
101         setorigin(ball, BALL_ATTACHORG);
102
103         if(ball.team != plyr.team)
104                 ball.teamtime = time + autocvar_g_nexball_basketball_delay_hold_forteam;
105
106         ball.owner = ball.pusher = plyr; //"owner" is set to the player carrying, "pusher" to the last player who touched it
107         ball.team = plyr.team;
108         plyr.ballcarried = ball;
109         ball.dropperid = plyr.playerid;
110
111         plyr.effects |= autocvar_g_nexball_basketball_effects_default;
112         ball.effects &~= autocvar_g_nexball_basketball_effects_default;
113
114         ball.velocity = '0 0 0';
115         ball.movetype = MOVETYPE_NONE;
116         ball.touch = SUB_Null;
117         ball.effects |= EF_NOSHADOW;
118         ball.scale = 1; // scale down.
119
120         WaypointSprite_AttachCarrier("nb-ball", plyr, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
121         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
122
123         if(autocvar_g_nexball_basketball_delay_hold)
124         {
125                 ball.think = DropOwner;
126                 ball.nextthink = time + autocvar_g_nexball_basketball_delay_hold;
127         }
128 }
129
130 void DropBall(entity ball, vector org, vector vel)
131 {
132         ball.effects |= autocvar_g_nexball_basketball_effects_default;
133         ball.effects &~= EF_NOSHADOW;
134         ball.owner.effects &~= autocvar_g_nexball_basketball_effects_default;
135
136         setattachment(ball, world, "");
137         setorigin(ball, org);
138         ball.movetype = MOVETYPE_BOUNCE;
139         ball.flags &~= FL_ONGROUND;
140         ball.scale = ball_scale;
141         ball.velocity = vel;
142         ball.ctf_droptime = time;
143         ball.touch = basketball_touch;
144         ball.think = ResetBall;
145         ball.nextthink = min(time + autocvar_g_nexball_delay_idle, ball.teamtime);
146
147         if(ball.owner.metertime)
148         {
149                 ball.owner.metertime = 0;
150                 ball.owner.weaponentity.state = WS_READY;
151         }
152
153         WaypointSprite_Kill(ball.owner.waypointsprite_attachedforcarrier);
154         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
155         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
156
157         ball.owner.ballcarried = world;
158         ball.owner = world;
159 }
160
161 void InitBall(void)
162 {
163         if(gameover) return;
164         self.flags &~= FL_ONGROUND;
165         self.movetype = MOVETYPE_BOUNCE;
166         if(self.classname == "nexball_basketball")
167                 self.touch = basketball_touch;
168         else if(self.classname == "nexball_football")
169                 self.touch = football_touch;
170         self.cnt = 0;
171         self.think = ResetBall;
172         self.nextthink = time + autocvar_g_nexball_delay_idle + 3;
173         self.teamtime = 0;
174         self.pusher = world;
175         self.team = FALSE;
176         sound(self, CH_TRIGGER, self.noise1, VOL_BASE, ATTN_NORM);
177         WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
178         LogNB("init", world);
179 }
180
181 void ResetBall(void)
182 {
183         if(self.cnt < 2)    // step 1
184         {
185                 if(time == self.teamtime)
186                         bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
187                 self.touch = SUB_Null;
188                 self.movetype = MOVETYPE_NOCLIP;
189                 self.velocity = '0 0 0'; // just in case?
190                 if(!self.cnt)
191                         LogNB("resetidle", world);
192                 self.cnt = 2;
193                 self.nextthink = time;
194         }
195         else if(self.cnt < 4)      // step 2 and 3
196         {
197 //              dprint("Step ", ftos(self.cnt), ": Calculated velocity: ", vtos(self.spawnorigin - self.origin), ", time: ", ftos(time), "\n");
198                 self.velocity = (self.spawnorigin - self.origin) * (self.cnt - 1); // 1 or 0.5 second movement
199                 self.nextthink = time + 0.5;
200                 self.cnt += 1;
201         }
202         else     // step 4
203         {
204 //              dprint("Step 4: time: ", ftos(time), "\n");
205                 if(vlen(self.origin - self.spawnorigin) > 10)  // should not happen anymore
206                         dprint("The ball moved too far away from its spawn origin.\nOffset: ",
207                                    vtos(self.origin - self.spawnorigin), " Velocity: ", vtos(self.velocity), "\n");
208                 self.velocity = '0 0 0';
209                 setorigin(self, self.spawnorigin); // make sure it's positioned correctly anyway
210                 self.movetype = MOVETYPE_NONE;
211                 self.think = InitBall;
212                 self.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
213         }
214 }
215
216 void football_touch(void)
217 {
218         if(other.solid == SOLID_BSP)
219         {
220                 if(time > self.lastground + 0.1)
221                 {
222                         sound(self, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NORM);
223                         self.lastground = time;
224                 }
225                 if(vlen(self.velocity) && !self.cnt)
226                         self.nextthink = time + autocvar_g_nexball_delay_idle;
227                 return;
228         }
229         if(other.classname != "player")
230                 return;
231         if(other.health < 1)
232                 return;
233         if(!self.cnt)
234                 self.nextthink = time + autocvar_g_nexball_delay_idle;
235
236         self.pusher = other;
237         self.team = other.team;
238
239         if(autocvar_g_nexball_football_physics == -1)    // MrBougo try 1, before decompiling Rev's original
240         {
241                 if(vlen(other.velocity))
242                         self.velocity = other.velocity * 1.5 + '0 0 1' * autocvar_g_nexball_football_boost_up;
243         }
244         else if(autocvar_g_nexball_football_physics == 1)      // MrBougo's modded Rev style: partially independant of the height of the aiming point
245         {
246                 makevectors(other.v_angle);
247                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + '0 0 1' * autocvar_g_nexball_football_boost_up;
248         }
249         else if(autocvar_g_nexball_football_physics == 2)      // 2nd mod try: totally independant. Really playable!
250         {
251                 makevectors(other.v_angle_y * '0 1 0');
252                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
253         }
254         else     // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
255         {
256                 makevectors(other.v_angle);
257                 self.velocity = other.velocity + v_forward * autocvar_g_nexball_football_boost_forward + v_up * autocvar_g_nexball_football_boost_up;
258         }
259         self.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
260 }
261
262 void basketball_touch(void)
263 {
264         if(other.ballcarried)
265         {
266                 football_touch();
267                 return;
268         }
269         if(!self.cnt && other.classname == "player" && (other.playerid != self.dropperid || time > self.ctf_droptime + autocvar_g_nexball_delay_collect))
270         {
271                 if(other.health <= 0)
272                         return;
273                 LogNB("caught", other);
274                 GiveBall(other, self);
275         }
276         else if(other.solid == SOLID_BSP)
277         {
278                 sound(self, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NORM);
279                 if(vlen(self.velocity) && !self.cnt)
280                         self.nextthink = min(time + autocvar_g_nexball_delay_idle, self.teamtime);
281         }
282 }
283
284 void GoalTouch(void)
285 {
286         entity ball;
287         float isclient, pscore, otherteam;
288         string pname;
289
290         if(gameover) return;
291         if((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
292                 ball = other.ballcarried;
293         else
294                 ball = other;
295         if(ball.classname != "nexball_basketball")
296                 if(ball.classname != "nexball_football")
297                         return;
298         if((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
299                 return;
300         EXACTTRIGGER_TOUCH;
301
302
303         if(nb_teams == 2)
304                 otherteam = OtherTeam(ball.team);
305
306         if((isclient = ball.pusher.flags & FL_CLIENT))
307                 pname = ball.pusher.netname;
308         else
309                 pname = "Someone (?)";
310
311         if(ball.team == self.team)         //owngoal (regular goals)
312         {
313                 LogNB("owngoal", ball.pusher);
314                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
315                 pscore = -1;
316         }
317         else if(self.team == GOAL_FAULT)
318         {
319                 LogNB("fault", ball.pusher);
320                 if(nb_teams == 2)
321                         bprint(ColoredTeamName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
322                 else
323                         bprint(ColoredTeamName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
324                 pscore = -1;
325         }
326         else if(self.team == GOAL_OUT)
327         {
328                 LogNB("out", ball.pusher);
329                 if((self.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
330                         bprint(pname, "^7 went out of bounds.\n");
331                 else
332                         bprint("The ball was returned.\n");
333                 pscore = 0;
334         }
335         else                               //score
336         {
337                 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
338                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", ColoredTeamName(ball.team), ".\n");
339                 pscore = 1;
340         }
341
342         sound(ball, CH_TRIGGER, self.noise, VOL_BASE, ATTN_NONE);
343
344         if(ball.team && pscore)
345         {
346                 if(nb_teams == 2 && pscore < 0)
347                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
348                 else
349                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
350         }
351         if(isclient)
352         {
353                 if(pscore > 0)
354                         PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
355                 else if(pscore < 0)
356                         PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
357         }
358
359         if(ball.owner)  // Happens on spawnflag GOAL_TOUCHPLAYER
360                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
361
362         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
363
364         ball.cnt = 1;
365         ball.think = ResetBall;
366         if(ball.classname == "nexball_basketball")
367                 ball.touch = football_touch; // better than SUB_Null: football control until the ball gets reset
368         ball.nextthink = time + autocvar_g_nexball_delay_goal * (self.team != GOAL_OUT);
369 }
370
371 //=======================//
372 //       team ents       //
373 //=======================//
374 void spawnfunc_nexball_team(void)
375 {
376         if(!g_nexball)
377         {
378                 remove(self);
379                 return;
380         }
381         self.team = self.cnt + 1;
382 }
383
384 void nb_spawnteam(string teamname, float teamcolor)
385 {
386         dprint("^2spawned team ", teamname, "\n");
387         entity e;
388         e = spawn();
389         e.classname = "nexball_team";
390         e.netname = teamname;
391         e.cnt = teamcolor;
392         e.team = e.cnt + 1;
393         nb_teams += 1;
394 }
395
396 void nb_spawnteams(void)
397 {
398         float t_r, t_b, t_y, t_p;
399         entity e;
400         for(e = world; (e = find(e, classname, "nexball_goal"));)
401         {
402                 switch(e.team)
403                 {
404                 case COLOR_TEAM1:
405                         if(!t_r)
406                         {
407                                 nb_spawnteam("Red", e.team-1)   ;
408                                 t_r = 1;
409                         }
410                         break;
411                 case COLOR_TEAM2:
412                         if(!t_b)
413                         {
414                                 nb_spawnteam("Blue", e.team-1)  ;
415                                 t_b = 1;
416                         }
417                         break;
418                 case COLOR_TEAM3:
419                         if(!t_y)
420                         {
421                                 nb_spawnteam("Yellow", e.team-1);
422                                 t_y = 1;
423                         }
424                         break;
425                 case COLOR_TEAM4:
426                         if(!t_p)
427                         {
428                                 nb_spawnteam("Pink", e.team-1)  ;
429                                 t_p = 1;
430                         }
431                         break;
432                 }
433         }
434 }
435
436 void nb_delayedinit(void)
437 {
438         if(find(world, classname, "nexball_team") == world)
439                 nb_spawnteams();
440         ScoreRules_nexball(nb_teams);
441 }
442
443
444 //=======================//
445 //      spawnfuncs       //
446 //=======================//
447
448 void SpawnBall(void)
449 {
450         if(!g_nexball)
451         {
452                 remove(self);
453                 return;
454         }
455
456 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
457
458         if(!self.model)
459         {
460                 self.model = "models/nexball/ball.md3";
461                 self.scale = 1.3;
462         }
463
464         precache_model(self.model);
465         setmodel(self, self.model);
466         setsize(self, BALL_MINS, BALL_MAXS);
467         ball_scale = self.scale;
468
469         relocate_nexball();
470         self.spawnorigin = self.origin;
471
472         self.effects = self.effects | EF_LOWPRECISION;
473
474         if(cvar(strcat("g_", self.classname, "_trail")))  //nexball_basketball :p
475         {
476                 self.glow_color = autocvar_g_nexball_trail_color;
477                 self.glow_trail = TRUE;
478         }
479
480         self.movetype = MOVETYPE_FLY;
481
482         if(!autocvar_g_nexball_sound_bounce)
483                 self.noise = "";
484         else if(!self.noise)
485                 self.noise = "sound/nexball/bounce.wav";
486         //bounce sound placeholder (FIXME)
487         if(!self.noise1)
488                 self.noise1 = "sound/nexball/drop.wav";
489         //ball drop sound placeholder (FIXME)
490         if(!self.noise2)
491                 self.noise2 = "sound/nexball/steal.wav";
492         //stealing sound placeholder (FIXME)
493         if(self.noise) precache_sound(self.noise);
494         precache_sound(self.noise1);
495         precache_sound(self.noise2);
496
497         WaypointSprite_AttachCarrier("nb-ball", self, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR); // the ball's team is not set yet, no rule update needed
498
499         self.reset = ball_restart;
500         self.think = InitBall;
501         self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
502 }
503
504 void spawnfunc_nexball_basketball(void)
505 {
506         self.classname = "nexball_basketball";
507         if not(balls & BALL_BASKET)
508         {
509                 /*
510                 CVTOV(g_nexball_basketball_effects_default);
511                 CVTOV(g_nexball_basketball_delay_hold);
512                 CVTOV(g_nexball_basketball_delay_hold_forteam);
513                 CVTOV(g_nexball_basketball_teamsteal);
514                 */
515                 autocvar_g_nexball_basketball_effects_default = autocvar_g_nexball_basketball_effects_default & BALL_EFFECTMASK;
516         }
517         if(!self.effects)
518                 self.effects = autocvar_g_nexball_basketball_effects_default;
519         self.solid = SOLID_TRIGGER;
520         balls |= BALL_BASKET;
521         self.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
522         self.bouncestop = autocvar_g_nexball_basketball_bouncestop;
523         SpawnBall();
524 }
525
526 void spawnfunc_nexball_football(void)
527 {
528         self.classname = "nexball_football";
529         self.solid = SOLID_TRIGGER;
530         balls |= BALL_FOOT;
531         self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
532         self.bouncestop = autocvar_g_nexball_football_bouncestop;
533         SpawnBall();
534 }
535
536 void SpawnGoal(void)
537 {
538         if(!g_nexball)
539         {
540                 remove(self);
541                 return;
542         }
543         EXACTTRIGGER_INIT;
544         self.classname = "nexball_goal";
545         if(!self.noise)
546                 self.noise = "ctf/respawn.wav";
547         precache_sound(self.noise);
548         self.touch = GoalTouch;
549 }
550
551 void spawnfunc_nexball_redgoal(void)
552 {
553         self.team = COLOR_TEAM1;
554         SpawnGoal();
555 }
556 void spawnfunc_nexball_bluegoal(void)
557 {
558         self.team = COLOR_TEAM2;
559         SpawnGoal();
560 }
561 void spawnfunc_nexball_yellowgoal(void)
562 {
563         self.team = COLOR_TEAM3;
564         SpawnGoal();
565 }
566 void spawnfunc_nexball_pinkgoal(void)
567 {
568         self.team = COLOR_TEAM4;
569         SpawnGoal();
570 }
571
572 void spawnfunc_nexball_fault(void)
573 {
574         self.team = GOAL_FAULT;
575         if(!self.noise)
576                 self.noise = "misc/typehit.wav";
577         SpawnGoal();
578 }
579
580 void spawnfunc_nexball_out(void)
581 {
582         self.team = GOAL_OUT;
583         if(!self.noise)
584                 self.noise = "misc/typehit.wav";
585         SpawnGoal();
586 }
587
588 //
589 //Spawnfuncs preserved for compatibility
590 //
591
592 void spawnfunc_ball(void)
593 {
594         spawnfunc_nexball_football();
595 }
596 void spawnfunc_ball_football(void)
597 {
598         spawnfunc_nexball_football();
599 }
600 void spawnfunc_ball_basketball(void)
601 {
602         spawnfunc_nexball_basketball();
603 }
604 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
605 void spawnfunc_ball_redgoal(void)
606 {
607         spawnfunc_nexball_bluegoal();    // I blame Revenant
608 }
609 void spawnfunc_ball_bluegoal(void)
610 {
611         spawnfunc_nexball_redgoal();    // but he didn't mean to cause trouble :p
612 }
613 void spawnfunc_ball_fault(void)
614 {
615         spawnfunc_nexball_fault();
616 }
617 void spawnfunc_ball_bound(void)
618 {
619         spawnfunc_nexball_out();
620 }
621
622 //=======================//
623 //      Weapon code      //
624 //=======================//
625
626 void W_Nexball_Touch(void)
627 {
628         entity ball, attacker;
629         attacker = self.owner;
630
631         PROJECTILE_TOUCH;
632         if(attacker.team != other.team || autocvar_g_nexball_basketball_teamsteal)
633                 if((ball = other.ballcarried) && (attacker.classname == "player"))
634                 {
635                         other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
636                         other.flags &~= FL_ONGROUND;
637                         if(!attacker.ballcarried)
638                         {
639                                 LogNB("stole", attacker);
640                                 sound(other, CH_TRIGGER, ball.noise2, VOL_BASE, ATTN_NORM);
641
642                                 if(attacker.team == other.team && time > attacker.teamkill_complain)
643                                 {
644                                         attacker.teamkill_complain = time + 5;
645                                         attacker.teamkill_soundtime = time + 0.4;
646                                         attacker.teamkill_soundsource = other;
647                                 }
648
649                                 GiveBall(attacker, other.ballcarried);
650                         }
651                 }
652         remove(self);
653 }
654
655 void W_Nexball_Attack(float t)
656 {
657         entity ball;
658         float mul, mi, ma;
659         if(!(ball = self.ballcarried))
660                 return;
661
662         W_SetupShot(self, FALSE, 4, "nexball/shoot1.wav", CH_WEAPON_A, 0);
663         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, world);
664         if(trace_startsolid)
665         {
666                 if(self.metertime)
667                         self.metertime = 0; // Shot failed, hide the power meter
668                 return;
669         }
670
671         //Calculate multiplier
672         if(t < 0)
673                 mul = 1;
674         else
675         {
676                 mi = autocvar_g_nexball_basketball_meter_minpower;
677                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
678                 //One triangle wave period with 1 as max
679                 mul = 2 * mod(t, g_nexball_meter_period) / g_nexball_meter_period;
680                 if(mul > 1)
681                         mul = 2 - mul;
682                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
683         }
684         DropBall(ball, w_shotorg, W_CalculateProjectileVelocity(self.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, FALSE));
685         //TODO: use the speed_up cvar too ??
686 }
687
688 void W_Nexball_Attack2(void)
689 {
690         entity missile;
691         if(!(balls & BALL_BASKET))
692                 return;
693         W_SetupShot(self, FALSE, 2, "nexball/shoot2.wav", CH_WEAPON_A, 0);
694 //      pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
695         missile = spawn();
696
697         missile.owner = self;
698         missile.classname = "ballstealer";
699
700         missile.movetype = MOVETYPE_FLY;
701         PROJECTILE_MAKETRIGGER(missile);
702
703         setmodel(missile, "models/elaser.mdl");  // precision set below
704         setsize(missile, '0 0 0', '0 0 0');
705         setorigin(missile, w_shotorg);
706
707         W_SetupProjectileVelocity(missile, autocvar_g_balance_nexball_secondary_speed, 0);
708         missile.angles = vectoangles(missile.velocity);
709         missile.touch = W_Nexball_Touch;
710         missile.think = SUB_Remove;
711         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
712
713         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
714         missile.flags = FL_PROJECTILE;
715 }
716
717 float w_nexball_weapon(float req)
718 {
719         if(req == WR_THINK)
720         {
721                 if(self.BUTTON_ATCK)
722                         if(weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
723                                 if(autocvar_g_nexball_basketball_meter)
724                                 {
725                                         if(self.ballcarried && !self.metertime)
726                                                 self.metertime = time;
727                                         else
728                                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
729                                 }
730                                 else
731                                 {
732                                         W_Nexball_Attack(-1);
733                                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
734                                 }
735                 if(self.BUTTON_ATCK2)
736                         if(weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
737                         {
738                                 W_Nexball_Attack2();
739                                 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
740                         }
741
742                 if(!self.BUTTON_ATCK && self.metertime && self.ballcarried)
743                 {
744                         W_Nexball_Attack(time - self.metertime);
745                         // DropBall or stealing will set metertime back to 0
746                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
747                 }
748         }
749         else if(req == WR_PRECACHE)
750         {
751                 precache_model("models/weapons/g_porto.md3");
752                 precache_model("models/weapons/v_porto.md3");
753                 precache_model("models/weapons/h_porto.iqm");
754                 precache_model("models/elaser.mdl");
755                 precache_sound("nexball/shoot1.wav");
756                 precache_sound("nexball/shoot2.wav");
757                 precache_sound("misc/typehit.wav");
758         }
759         else if(req == WR_SETUP)
760                 weapon_setup(WEP_PORTO);
761         else if(req == WR_SUICIDEMESSAGE)
762         {
763                 w_deathtypestring = "is a weirdo";
764         }
765         else if(req == WR_KILLMESSAGE)
766         {
767                 w_deathtypestring = "got killed by #'s black magic";
768         }
769         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE
770         return TRUE;
771 }
772
773 MUTATOR_HOOKFUNCTION(nexball_BallDrop)
774 {
775         if(self.ballcarried && g_nexball)
776                 DropBall(self.ballcarried, self.origin, self.velocity);
777
778         return 0;
779 }
780
781 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsString)
782 {
783         ret_string = strcat(ret_string, ":NB");
784         return 0;
785 }
786
787 MUTATOR_HOOKFUNCTION(nexball_BuildMutatorsPrettyString)
788 {
789         ret_string = strcat(ret_string, ", NexBall");
790         return 0;
791 }
792
793 MUTATOR_DEFINITION(gamemode_nexball)
794 {
795         MUTATOR_HOOK(PlayerDies, nexball_BallDrop, CBC_ORDER_ANY);
796         MUTATOR_HOOK(MakePlayerObserver, nexball_BallDrop, CBC_ORDER_ANY);
797         MUTATOR_HOOK(ClientDisconnect, nexball_BallDrop, CBC_ORDER_ANY);
798         MUTATOR_HOOK(BuildMutatorsPrettyString, nexball_BuildMutatorsPrettyString, CBC_ORDER_ANY);
799         MUTATOR_HOOK(BuildMutatorsString, nexball_BuildMutatorsString, CBC_ORDER_ANY);
800
801         MUTATOR_ONADD
802         {
803                 g_nexball_meter_period = autocvar_g_nexball_meter_period;
804                 if(g_nexball_meter_period <= 0)
805                         g_nexball_meter_period = 2; // avoid division by zero etc. due to silly users
806                 g_nexball_meter_period = rint(g_nexball_meter_period * 32) / 32; //Round to 1/32ths to send as a byte multiplied by 32
807                 addstat(STAT_NB_METERSTART, AS_FLOAT, metertime);
808
809                 // General settings
810                 /*
811                 CVTOV(g_nexball_football_boost_forward);   //100
812                 CVTOV(g_nexball_football_boost_up);        //200
813                 CVTOV(g_nexball_delay_idle);               //10
814                 CVTOV(g_nexball_football_physics);         //0
815                 */
816                 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
817
818                 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
819         }
820
821         return 0;
822 }