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