]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/nexball.qc
waypointsprites: always add color to WaypointSprite_Spawn()
[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         local 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         local 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         local 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);
170         WaypointSprite_UpdateRule(plyr.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
171         WaypointSprite_UpdateTeamRadar(plyr.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
172
173         if (g_nexball_basketball_delay_hold)
174         {
175                 ball.think = DropOwner;
176                 ball.nextthink = time + g_nexball_basketball_delay_hold;
177         }
178 }
179
180 void DropBall (entity ball, vector org, vector vel)
181 {
182         ball.effects |= g_nexball_basketball_effects_default;
183         ball.effects &~= EF_NOSHADOW;
184         ball.owner.effects &~= g_nexball_basketball_effects_default;
185
186         setattachment(ball, world, "");
187         setorigin (ball, org);
188         ball.movetype = MOVETYPE_BOUNCE;
189         ball.flags &~= FL_ONGROUND;
190         ball.scale = ball_scale;
191         ball.velocity = vel;
192         ball.ctf_droptime = time;
193         ball.touch = basketball_touch;
194         ball.think = ResetBall;
195         ball.nextthink = min(time + g_nexball_delay_idle, ball.teamtime);
196
197         if (ball.owner.metertime)
198         {
199                 ball.owner.metertime = 0;
200                 ball.owner.weaponentity.state = WS_READY;
201         }
202
203         WaypointSprite_Kill(ball.owner.waypointsprite_attachedforcarrier);
204         //WaypointSprite_AttachCarrier("nb-ball", ball);
205         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
206         WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
207
208         ball.owner.ballcarried = world;
209         ball.owner = world;
210 }
211
212 void InitBall (void)
213 {
214         if (gameover) return;
215         self.flags &~= FL_ONGROUND;
216         self.movetype = MOVETYPE_BOUNCE;
217         if (self.classname == "nexball_basketball")
218                 self.touch = basketball_touch;
219         else if (self.classname == "nexball_football")
220                 self.touch = football_touch;
221         self.cnt = 0;
222         self.think = ResetBall;
223         self.nextthink = time + g_nexball_delay_idle + 3;
224         self.teamtime = 0;
225         self.pusher = world;
226         self.team = FALSE;
227         sound (self, CHAN_PROJECTILE, self.noise1, VOL_BASE, ATTN_NORM);
228         WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
229         LogNB("init", world);
230 }
231
232 void ResetBall (void)
233 {
234         if (self.cnt < 2) { // step 1
235                 if (time == self.teamtime)
236                         bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
237                 self.touch = SUB_Null;
238                 self.movetype = MOVETYPE_NOCLIP;
239                 self.velocity = '0 0 0'; // just in case?
240                 if(!self.cnt)
241                         LogNB("resetidle", world);
242                 self.cnt = 2;
243                 self.nextthink = time;
244         } else if (self.cnt < 4) { // step 2 and 3
245 //              dprint("Step ", ftos(self.cnt), ": Calculated velocity: ", vtos(self.spawnorigin - self.origin), ", time: ", ftos(time), "\n");
246                 self.velocity = (self.spawnorigin - self.origin) * (self.cnt - 1); // 1 or 0.5 second movement
247                 self.nextthink = time + 0.5;
248                 self.cnt += 1;
249         } else { // step 4
250 //              dprint("Step 4: time: ", ftos(time), "\n");
251                 if (vlen(self.origin - self.spawnorigin) > 10) // should not happen anymore
252                         dprint("The ball moved too far away from its spawn origin.\nOffset: ",
253                                vtos(self.origin - self.spawnorigin), " Velocity: ", vtos(self.velocity), "\n");
254                 self.velocity = '0 0 0';
255                 setorigin(self, self.spawnorigin); // make sure it's positioned correctly anyway
256                 self.movetype = MOVETYPE_NONE;
257                 self.think = InitBall;
258                 self.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
259         }
260 }
261
262 void football_touch (void)
263 {
264         if (other.solid == SOLID_BSP) {
265                 if (time > self.lastground + 0.1)
266                 {
267                         sound (self, CHAN_PROJECTILE, self.noise, VOL_BASE, ATTN_NORM);
268                         self.lastground = time;
269                 }
270                 if (vlen(self.velocity) && !self.cnt)
271                         self.nextthink = time + g_nexball_delay_idle;
272                 return;
273         }
274         if (other.classname != "player")
275                 return;
276         if (other.health < 1)
277                 return;
278         if (!self.cnt)
279                 self.nextthink = time + g_nexball_delay_idle;
280
281         self.pusher = other;
282         self.team = other.team;
283
284         if (g_nexball_football_physics == -1) { // MrBougo try 1, before decompiling Rev's original
285                 if (vlen(other.velocity))
286                         self.velocity = other.velocity * 1.5 + '0 0 1' * g_nexball_football_boost_up;
287         } else if (g_nexball_football_physics == 1) { // MrBougo's modded Rev style: partially independant of the height of the aiming point
288                 makevectors(other.v_angle);
289                 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + '0 0 1' * g_nexball_football_boost_up;
290         } else if (g_nexball_football_physics == 2) { // 2nd mod try: totally independant. Really playable!
291                 makevectors(other.v_angle_y * '0 1 0');
292                 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + v_up * g_nexball_football_boost_up;
293         } else { // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
294                 makevectors(other.v_angle);
295                 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + v_up * g_nexball_football_boost_up;
296         }
297         self.avelocity = -250 * v_forward;  // maybe there is a way to make it look better?
298 }
299
300 void basketball_touch (void)
301 {
302         if (other.ballcarried)
303         {
304                 football_touch();
305                 return;
306         }
307         if (!self.cnt && other.classname == "player" && (other.playerid != self.dropperid || time > self.ctf_droptime + autocvar_g_nexball_delay_collect)) {
308                 if (other.health <= 0)
309                         return;
310                 LogNB("caught", other);
311                 GiveBall(other, self);
312         } else if (other.solid == SOLID_BSP) {
313                 sound (self, CHAN_PROJECTILE, self.noise, VOL_BASE, ATTN_NORM);
314                 if (vlen(self.velocity) && !self.cnt)
315                         self.nextthink = min(time + g_nexball_delay_idle, self.teamtime);
316         }
317 }
318
319 void GoalTouch (void)
320 {
321         entity ball;
322         float isclient, pscore, otherteam;
323         string pname;
324
325         if (gameover) return;
326         if ((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
327                 ball = other.ballcarried;
328         else
329                 ball = other;
330         if (ball.classname != "nexball_basketball")
331         if (ball.classname != "nexball_football")
332                 return;
333         if ((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
334                 return;
335         EXACTTRIGGER_TOUCH;
336
337
338         if(nb_teams == 2)
339                 otherteam = OtherTeam(ball.team);
340
341         if((isclient = ball.pusher.flags & FL_CLIENT))
342                 pname = ball.pusher.netname;
343         else
344                 pname = "Someone (?)";
345
346         if        (ball.team == self.team) //owngoal (regular goals)
347         {
348                 LogNB("owngoal", ball.pusher);
349                 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
350                 pscore = -1;
351         } else if (self.team == GOAL_FAULT) {
352                 LogNB("fault", ball.pusher);
353                 if (nb_teams == 2)
354                         bprint(ColoredTeamName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
355                 else
356                         bprint(ColoredTeamName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
357                 pscore = -1;
358         } else if (self.team == GOAL_OUT) {
359                 LogNB("out", ball.pusher);
360                 if ((self.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
361                         bprint(pname, "^7 went out of bounds.\n");
362                 else
363                         bprint("The ball was returned.\n");
364                 pscore = 0;
365         } else {                           //score
366                 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
367                 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", ColoredTeamName(ball.team), ".\n");
368                 pscore = 1;
369         }
370
371         sound (ball, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
372
373         if(ball.team && pscore)
374         {
375                 if (nb_teams == 2 && pscore < 0)
376                         TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
377                 else
378                         TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
379         }
380         if (isclient)
381         {
382                 if (pscore > 0)
383                         PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
384                 else if (pscore < 0)
385                         PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
386         }
387
388         if (ball.owner) // Happens on spawnflag GOAL_TOUCHPLAYER
389                 DropBall(ball, ball.owner.origin, ball.owner.velocity);
390
391         WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
392
393         ball.cnt = 1;
394         ball.think = ResetBall;
395         if (ball.classname == "nexball_basketball")
396                 ball.touch = football_touch; // better than SUB_Null: football control until the ball gets reset
397         ball.nextthink = time + autocvar_g_nexball_delay_goal * (self.team != GOAL_OUT);
398 }
399
400 //=======================//
401 //       team ents       //
402 //=======================//
403 void spawnfunc_nexball_team (void)
404 {
405         if(!g_nexball) { remove(self); return; }
406         self.team = self.cnt + 1;
407 }
408
409 void nb_spawnteam (string teamname, float teamcolor)
410 {
411         dprint("^2spawned team ", teamname, "\n");
412         local entity e;
413         e = spawn();
414         e.classname = "nexball_team";
415         e.netname = teamname;
416         e.cnt = teamcolor;
417         e.team = e.cnt + 1;
418         nb_teams += 1;
419 };
420
421 void nb_spawnteams (void)
422 {
423         float t_r, t_b, t_y, t_p;
424         entity e;
425         for(e = world; (e = find(e, classname, "nexball_goal")); )
426         {
427                 switch(e.team)
428                 {
429                         case COLOR_TEAM1: if(!t_r) { nb_spawnteam ("Red", e.team-1)   ; t_r = 1; } break;
430                         case COLOR_TEAM2: if(!t_b) { nb_spawnteam ("Blue", e.team-1)  ; t_b = 1; } break;
431                         case COLOR_TEAM3: if(!t_y) { nb_spawnteam ("Yellow", e.team-1); t_y = 1; } break;
432                         case COLOR_TEAM4: if(!t_p) { nb_spawnteam ("Pink", e.team-1)  ; t_p = 1; } break;
433                 }
434         }
435 }
436
437 void nb_delayedinit (void)
438 {
439         if (find(world, classname, "nexball_team") == world)
440                 nb_spawnteams();
441         ScoreRules_nexball(nb_teams);
442 }
443
444
445 //=======================//
446 //      spawnfuncs       //
447 //=======================//
448
449 void SpawnBall (void)
450 {
451         if(!g_nexball) { remove(self); return; }
452
453 //      balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
454
455         if (!self.model) {
456                 self.model = "models/nexball/ball.md3";
457                 self.scale = 1.3;
458         }
459
460         precache_model (self.model);
461         setmodel (self, self.model);
462         setsize (self, BALL_MINS, BALL_MAXS);
463         ball_scale = self.scale;
464
465         relocate_nexball();
466         self.spawnorigin = self.origin;
467
468         self.effects = self.effects | EF_LOWPRECISION;
469
470         if (cvar(strcat("g_", self.classname, "_trail"))) //nexball_basketball :p
471         {
472                 self.glow_color = autocvar_g_nexball_trail_color;
473                 self.glow_trail = TRUE;
474         }
475
476         self.movetype = MOVETYPE_FLY;
477
478         if (!autocvar_g_nexball_sound_bounce)
479                 self.noise = "";
480         else if (!self.noise)
481                 self.noise = "sound/nexball/bounce.wav";
482                 //bounce sound placeholder (FIXME)
483         if (!self.noise1)
484                 self.noise1 = "sound/nexball/drop.wav";
485                 //ball drop sound placeholder (FIXME)
486         if (!self.noise2)
487                 self.noise2 = "sound/nexball/steal.wav";
488                 //stealing sound placeholder (FIXME)
489         if (self.noise) precache_sound (self.noise);
490         precache_sound (self.noise1);
491         precache_sound (self.noise2);
492
493         WaypointSprite_AttachCarrier("nb-ball", self); // the ball's team is not set yet, no rule update needed
494         WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
495
496         self.reset = ball_restart;
497         self.think = InitBall;
498         self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
499 }
500
501 void spawnfunc_nexball_basketball (void)
502 {
503         self.classname = "nexball_basketball";
504         if not(balls & BALL_BASKET)
505         {
506                 CVTOV(g_nexball_basketball_effects_default);
507                 CVTOV(g_nexball_basketball_delay_hold);
508                 CVTOV(g_nexball_basketball_delay_hold_forteam);
509                 CVTOV(g_nexball_basketball_teamsteal);
510                 g_nexball_basketball_effects_default = g_nexball_basketball_effects_default & BALL_EFFECTMASK;
511         }
512         if (!self.effects)
513                 self.effects = g_nexball_basketball_effects_default;
514         self.solid = SOLID_TRIGGER;
515         balls |= BALL_BASKET;
516         self.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
517         self.bouncestop = autocvar_g_nexball_basketball_bouncestop;
518         SpawnBall();
519 }
520
521 void spawnfunc_nexball_football (void)
522 {
523         self.classname = "nexball_football";
524         self.solid = SOLID_TRIGGER;
525         balls |= BALL_FOOT;
526         self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
527         self.bouncestop = autocvar_g_nexball_football_bouncestop;
528         SpawnBall();
529 }
530
531 void SpawnGoal (void)
532 {
533         if(!g_nexball) { remove(self); return; }
534         EXACTTRIGGER_INIT;
535         self.classname = "nexball_goal";
536         if (!self.noise)
537                 self.noise = "ctf/respawn.wav";
538         precache_sound(self.noise);
539         self.touch = GoalTouch;
540 }
541
542 void spawnfunc_nexball_redgoal (void)
543 {
544         self.team = COLOR_TEAM1;
545         SpawnGoal();
546 }
547 void spawnfunc_nexball_bluegoal (void)
548 {
549         self.team = COLOR_TEAM2;
550         SpawnGoal();
551 }
552 void spawnfunc_nexball_yellowgoal (void)
553 {
554         self.team = COLOR_TEAM3;
555         SpawnGoal();
556 }
557 void spawnfunc_nexball_pinkgoal (void)
558 {
559         self.team = COLOR_TEAM4;
560         SpawnGoal();
561 }
562
563 void spawnfunc_nexball_fault (void)
564 {
565         self.team = GOAL_FAULT;
566         if (!self.noise)
567                 self.noise = "misc/typehit.wav";
568         SpawnGoal();
569 }
570
571 void spawnfunc_nexball_out (void)
572 {
573         self.team = GOAL_OUT;
574         if (!self.noise)
575                 self.noise = "misc/typehit.wav";
576         SpawnGoal();
577 }
578
579 //
580 //Spawnfuncs preserved for compatibility
581 //
582
583 void spawnfunc_ball            (void) { spawnfunc_nexball_football(); }
584 void spawnfunc_ball_football   (void) { spawnfunc_nexball_football(); }
585 void spawnfunc_ball_basketball (void) { spawnfunc_nexball_basketball(); }
586 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
587 void spawnfunc_ball_redgoal    (void) { spawnfunc_nexball_bluegoal(); } // I blame Revenant
588 void spawnfunc_ball_bluegoal   (void) { spawnfunc_nexball_redgoal(); }  // but he didn't mean to cause trouble :p
589 void spawnfunc_ball_fault      (void) { spawnfunc_nexball_fault(); }
590 void spawnfunc_ball_bound      (void) { spawnfunc_nexball_out(); }
591
592 //=======================//
593 //      Weapon code      //
594 //=======================//
595
596 void W_Nexball_Touch (void)
597 {
598         local entity ball, attacker;
599         attacker = self.owner;
600
601         PROJECTILE_TOUCH;
602         if(attacker.team != other.team || g_nexball_basketball_teamsteal)
603         if((ball = other.ballcarried) && (attacker.classname == "player" || attacker.classname == "gib"))
604         {
605                 other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
606                 other.flags &~= FL_ONGROUND;
607                 if(!attacker.ballcarried)
608                 {
609                         LogNB("stole", attacker);
610                         sound (other, CHAN_AUTO, ball.noise2, VOL_BASE, ATTN_NORM);
611
612                         if(attacker.team == other.team && time > attacker.teamkill_complain)
613                         {
614                                 attacker.teamkill_complain = time + 5;
615                                 attacker.teamkill_soundtime = time + 0.4;
616                                 attacker.teamkill_soundsource = other;
617                         }
618
619                         GiveBall(attacker, other.ballcarried);
620                 }
621         }
622         remove(self);
623 }
624
625 void W_Nexball_Attack (float t)
626 {
627         local entity ball;
628         local float mul, mi, ma;
629         if (!(ball = self.ballcarried))
630                 return;
631
632         W_SetupShot (self, FALSE, 4, "nexball/shoot1.wav", CHAN_WEAPON, 0);
633         tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, world);
634         if(trace_startsolid)
635         {
636                 if(self.metertime)
637                         self.metertime = 0; // Shot failed, hide the power meter
638                 return;
639         }
640
641         //Calculate multiplier
642         if (t < 0)
643                 mul = 1;
644         else
645         {
646                 mi = autocvar_g_nexball_basketball_meter_minpower;
647                 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
648                 //One triangle wave period with 1 as max
649                 mul = 2 * mod(t, g_nexball_meter_period) / g_nexball_meter_period;
650                 if (mul > 1)
651                         mul = 2 - mul;
652                 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
653         }
654         DropBall (ball, w_shotorg, W_CalculateProjectileVelocity(self.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, FALSE));
655         //TODO: use the speed_up cvar too ??
656 }
657
658 void W_Nexball_Attack2 (void)
659 {
660         local entity missile;
661         if (!(balls & BALL_BASKET))
662                 return;
663         W_SetupShot (self, FALSE, 2, "nexball/shoot2.wav", CHAN_WEAPON, 0);
664 //      pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
665         missile = spawn ();
666
667         missile.owner = self;
668         missile.classname = "ballstealer";
669
670         missile.movetype = MOVETYPE_FLY;
671         PROJECTILE_MAKETRIGGER(missile);
672
673         setmodel (missile, "models/elaser.mdl"); // precision set below
674         setsize (missile, '0 0 0', '0 0 0');
675         setorigin (missile, w_shotorg);
676
677         W_SetupProjectileVelocity(missile, autocvar_g_balance_nexball_secondary_speed, 0);
678         missile.angles = vectoangles (missile.velocity);
679         missile.touch = W_Nexball_Touch;
680         missile.think = SUB_Remove;
681         missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
682
683         missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
684         missile.flags = FL_PROJECTILE;
685 }
686
687 float w_nexball_weapon(float req)
688 {
689         if (req == WR_THINK)
690         {
691                 if (self.BUTTON_ATCK)
692                 if (weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
693                 if (autocvar_g_nexball_basketball_meter)
694                 {
695                         if (self.ballcarried && !self.metertime)
696                                 self.metertime = time;
697                         else
698                                 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
699                 }
700                 else
701                 {
702                         W_Nexball_Attack(-1);
703                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
704                 }
705                 if (self.BUTTON_ATCK2)
706                 if (weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
707                 {
708                         W_Nexball_Attack2();
709                         weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
710                 }
711
712                 if (!self.BUTTON_ATCK && self.metertime && self.ballcarried)
713                 {
714                         W_Nexball_Attack(time - self.metertime);
715                         // DropBall or stealing will set metertime back to 0
716                         weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
717                 }
718         }
719         else if (req == WR_PRECACHE)
720         {
721                 precache_model ("models/weapons/g_porto.md3");
722                 precache_model ("models/weapons/v_porto.md3");
723                 precache_model ("models/weapons/h_porto.dpm");
724                 precache_model ("models/elaser.mdl");
725                 precache_sound ("nexball/shoot1.wav");
726                 precache_sound ("nexball/shoot2.wav");
727                 precache_sound ("misc/typehit.wav");
728         }
729         else if (req == WR_SETUP)
730                 weapon_setup(WEP_PORTO);
731         else if (req == WR_SUICIDEMESSAGE)
732         {
733                 w_deathtypestring = "is a weirdo";
734         }
735         else if (req == WR_KILLMESSAGE)
736         {
737                 w_deathtypestring = "got killed by #'s black magic";
738         }
739         // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE
740         return TRUE;
741 }