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