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'
10 #define GOAL_TOUCHPLAYER 1
15 #define CVTOV(s) s = cvar( #s )
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;
31 void nb_delayedinit();
32 void nb_init() // Called early (worldspawn stage)
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);
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
46 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
48 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
51 float OtherTeam(float t) //works only if there are two teams on the map!
54 e = find(world, classname, "nexball_team");
56 e = find(e, classname, "nexball_team");
62 void LogNB(string mode, entity actor)
65 if(!autocvar_sv_eventlog)
67 s = strcat(":nexball:", mode);
69 s = strcat(s, ":", ftos(actor.playerid));
73 void ball_restart (void)
76 DropBall(self, self.owner.origin, '0 0 0');
80 void nexball_setstatus (void)
83 self.items &~= IT_KEY1;
86 if (self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
88 bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
90 self = self.ballcarried;
91 DropBall(self, self.owner.origin, '0 0 0');
95 self.items |= IT_KEY1;
99 void relocate_nexball (void)
101 tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, TRUE, self);
102 if (trace_startsolid)
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");
116 void basketball_touch();
117 void football_touch();
119 void DropOwner (void)
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;
129 void GiveBall (entity plyr, entity ball)
133 if ((ownr = ball.owner))
135 ownr.effects &~= g_nexball_basketball_effects_default;
136 ownr.ballcarried = world;
140 ownr.weaponentity.state = WS_READY;
142 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
146 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
149 setattachment(ball, plyr, "");
150 setorigin(ball, BALL_ATTACHORG);
152 if (ball.team != plyr.team)
153 ball.teamtime = time + g_nexball_basketball_delay_hold_forteam;
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;
160 plyr.effects |= g_nexball_basketball_effects_default;
161 ball.effects &~= g_nexball_basketball_effects_default;
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.
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);
173 if (g_nexball_basketball_delay_hold)
175 ball.think = DropOwner;
176 ball.nextthink = time + g_nexball_basketball_delay_hold;
180 void DropBall (entity ball, vector org, vector vel)
182 ball.effects |= g_nexball_basketball_effects_default;
183 ball.effects &~= EF_NOSHADOW;
184 ball.owner.effects &~= g_nexball_basketball_effects_default;
186 setattachment(ball, world, "");
187 setorigin (ball, org);
188 ball.movetype = MOVETYPE_BOUNCE;
189 ball.flags &~= FL_ONGROUND;
190 ball.scale = ball_scale;
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);
197 if (ball.owner.metertime)
199 ball.owner.metertime = 0;
200 ball.owner.weaponentity.state = WS_READY;
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); // no health bar please
206 WaypointSprite_UpdateRule(ball.waypointsprite_attachedforcarrier, 0, SPRITERULE_DEFAULT);
207 WaypointSprite_UpdateTeamRadar(ball.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
209 ball.owner.ballcarried = world;
215 if (gameover) return;
216 self.flags &~= FL_ONGROUND;
217 self.movetype = MOVETYPE_BOUNCE;
218 if (self.classname == "nexball_basketball")
219 self.touch = basketball_touch;
220 else if (self.classname == "nexball_football")
221 self.touch = football_touch;
223 self.think = ResetBall;
224 self.nextthink = time + g_nexball_delay_idle + 3;
228 sound (self, CHAN_PROJECTILE, self.noise1, VOL_BASE, ATTN_NORM);
229 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
230 LogNB("init", world);
233 void ResetBall (void)
235 if (self.cnt < 2) { // step 1
236 if (time == self.teamtime)
237 bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
238 self.touch = SUB_Null;
239 self.movetype = MOVETYPE_NOCLIP;
240 self.velocity = '0 0 0'; // just in case?
242 LogNB("resetidle", world);
244 self.nextthink = time;
245 } else if (self.cnt < 4) { // step 2 and 3
246 // dprint("Step ", ftos(self.cnt), ": Calculated velocity: ", vtos(self.spawnorigin - self.origin), ", time: ", ftos(time), "\n");
247 self.velocity = (self.spawnorigin - self.origin) * (self.cnt - 1); // 1 or 0.5 second movement
248 self.nextthink = time + 0.5;
251 // dprint("Step 4: time: ", ftos(time), "\n");
252 if (vlen(self.origin - self.spawnorigin) > 10) // should not happen anymore
253 dprint("The ball moved too far away from its spawn origin.\nOffset: ",
254 vtos(self.origin - self.spawnorigin), " Velocity: ", vtos(self.velocity), "\n");
255 self.velocity = '0 0 0';
256 setorigin(self, self.spawnorigin); // make sure it's positioned correctly anyway
257 self.movetype = MOVETYPE_NONE;
258 self.think = InitBall;
259 self.nextthink = max(time, game_starttime) + autocvar_g_nexball_delay_start;
263 void football_touch (void)
265 if (other.solid == SOLID_BSP) {
266 if (time > self.lastground + 0.1)
268 sound (self, CHAN_PROJECTILE, self.noise, VOL_BASE, ATTN_NORM);
269 self.lastground = time;
271 if (vlen(self.velocity) && !self.cnt)
272 self.nextthink = time + g_nexball_delay_idle;
275 if (other.classname != "player")
277 if (other.health < 1)
280 self.nextthink = time + g_nexball_delay_idle;
283 self.team = other.team;
285 if (g_nexball_football_physics == -1) { // MrBougo try 1, before decompiling Rev's original
286 if (vlen(other.velocity))
287 self.velocity = other.velocity * 1.5 + '0 0 1' * g_nexball_football_boost_up;
288 } else if (g_nexball_football_physics == 1) { // MrBougo's modded Rev style: partially independant of the height of the aiming point
289 makevectors(other.v_angle);
290 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + '0 0 1' * g_nexball_football_boost_up;
291 } else if (g_nexball_football_physics == 2) { // 2nd mod try: totally independant. Really playable!
292 makevectors(other.v_angle_y * '0 1 0');
293 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + v_up * g_nexball_football_boost_up;
294 } else { // Revenant's original style (from the original mod's disassembly, acknowledged by Revenant)
295 makevectors(other.v_angle);
296 self.velocity = other.velocity + v_forward * g_nexball_football_boost_forward + v_up * g_nexball_football_boost_up;
298 self.avelocity = -250 * v_forward; // maybe there is a way to make it look better?
301 void basketball_touch (void)
303 if (other.ballcarried)
308 if (!self.cnt && other.classname == "player" && (other.playerid != self.dropperid || time > self.ctf_droptime + autocvar_g_nexball_delay_collect)) {
309 if (other.health <= 0)
311 LogNB("caught", other);
312 GiveBall(other, self);
313 } else if (other.solid == SOLID_BSP) {
314 sound (self, CHAN_PROJECTILE, self.noise, VOL_BASE, ATTN_NORM);
315 if (vlen(self.velocity) && !self.cnt)
316 self.nextthink = min(time + g_nexball_delay_idle, self.teamtime);
320 void GoalTouch (void)
323 float isclient, pscore, otherteam;
326 if (gameover) return;
327 if ((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
328 ball = other.ballcarried;
331 if (ball.classname != "nexball_basketball")
332 if (ball.classname != "nexball_football")
334 if ((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
340 otherteam = OtherTeam(ball.team);
342 if((isclient = ball.pusher.flags & FL_CLIENT))
343 pname = ball.pusher.netname;
345 pname = "Someone (?)";
347 if (ball.team == self.team) //owngoal (regular goals)
349 LogNB("owngoal", ball.pusher);
350 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
352 } else if (self.team == GOAL_FAULT) {
353 LogNB("fault", ball.pusher);
355 bprint(ColoredTeamName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
357 bprint(ColoredTeamName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
359 } else if (self.team == GOAL_OUT) {
360 LogNB("out", ball.pusher);
361 if ((self.spawnflags & GOAL_TOUCHPLAYER) && ball.owner)
362 bprint(pname, "^7 went out of bounds.\n");
364 bprint("The ball was returned.\n");
367 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
368 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", ColoredTeamName(ball.team), ".\n");
372 sound (ball, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
374 if(ball.team && pscore)
376 if (nb_teams == 2 && pscore < 0)
377 TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
379 TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
384 PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
386 PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
389 if (ball.owner) // Happens on spawnflag GOAL_TOUCHPLAYER
390 DropBall(ball, ball.owner.origin, ball.owner.velocity);
392 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
395 ball.think = ResetBall;
396 if (ball.classname == "nexball_basketball")
397 ball.touch = football_touch; // better than SUB_Null: football control until the ball gets reset
398 ball.nextthink = time + autocvar_g_nexball_delay_goal * (self.team != GOAL_OUT);
401 //=======================//
403 //=======================//
404 void spawnfunc_nexball_team (void)
406 if(!g_nexball) { remove(self); return; }
407 self.team = self.cnt + 1;
410 void nb_spawnteam (string teamname, float teamcolor)
412 dprint("^2spawned team ", teamname, "\n");
415 e.classname = "nexball_team";
416 e.netname = teamname;
422 void nb_spawnteams (void)
424 float t_r, t_b, t_y, t_p;
426 for(e = world; (e = find(e, classname, "nexball_goal")); )
430 case COLOR_TEAM1: if(!t_r) { nb_spawnteam ("Red", e.team-1) ; t_r = 1; } break;
431 case COLOR_TEAM2: if(!t_b) { nb_spawnteam ("Blue", e.team-1) ; t_b = 1; } break;
432 case COLOR_TEAM3: if(!t_y) { nb_spawnteam ("Yellow", e.team-1); t_y = 1; } break;
433 case COLOR_TEAM4: if(!t_p) { nb_spawnteam ("Pink", e.team-1) ; t_p = 1; } break;
438 void nb_delayedinit (void)
440 if (find(world, classname, "nexball_team") == world)
442 ScoreRules_nexball(nb_teams);
446 //=======================//
448 //=======================//
450 void SpawnBall (void)
452 if(!g_nexball) { remove(self); return; }
454 // balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
457 self.model = "models/nexball/ball.md3";
461 precache_model (self.model);
462 setmodel (self, self.model);
463 setsize (self, BALL_MINS, BALL_MAXS);
464 ball_scale = self.scale;
467 self.spawnorigin = self.origin;
469 self.effects = self.effects | EF_LOWPRECISION;
471 if (cvar(strcat("g_", self.classname, "_trail"))) //nexball_basketball :p
473 self.glow_color = autocvar_g_nexball_trail_color;
474 self.glow_trail = TRUE;
477 self.movetype = MOVETYPE_FLY;
479 if (!autocvar_g_nexball_sound_bounce)
481 else if (!self.noise)
482 self.noise = "sound/nexball/bounce.wav";
483 //bounce sound placeholder (FIXME)
485 self.noise1 = "sound/nexball/drop.wav";
486 //ball drop sound placeholder (FIXME)
488 self.noise2 = "sound/nexball/steal.wav";
489 //stealing sound placeholder (FIXME)
490 if (self.noise) precache_sound (self.noise);
491 precache_sound (self.noise1);
492 precache_sound (self.noise2);
494 WaypointSprite_AttachCarrier("nb-ball", self); // the ball's team is not set yet, no rule update needed
495 WaypointSprite_UpdateTeamRadar(self.waypointsprite_attachedforcarrier, RADARICON_FLAGCARRIER, BALL_SPRITECOLOR);
497 self.reset = ball_restart;
498 self.think = InitBall;
499 self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
502 void spawnfunc_nexball_basketball (void)
504 self.classname = "nexball_basketball";
505 if not(balls & BALL_BASKET)
507 CVTOV(g_nexball_basketball_effects_default);
508 CVTOV(g_nexball_basketball_delay_hold);
509 CVTOV(g_nexball_basketball_delay_hold_forteam);
510 CVTOV(g_nexball_basketball_teamsteal);
511 g_nexball_basketball_effects_default = g_nexball_basketball_effects_default & BALL_EFFECTMASK;
514 self.effects = g_nexball_basketball_effects_default;
515 self.solid = SOLID_TRIGGER;
516 balls |= BALL_BASKET;
517 self.bouncefactor = autocvar_g_nexball_basketball_bouncefactor;
518 self.bouncestop = autocvar_g_nexball_basketball_bouncestop;
522 void spawnfunc_nexball_football (void)
524 self.classname = "nexball_football";
525 self.solid = SOLID_TRIGGER;
527 self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
528 self.bouncestop = autocvar_g_nexball_football_bouncestop;
532 void SpawnGoal (void)
534 if(!g_nexball) { remove(self); return; }
536 self.classname = "nexball_goal";
538 self.noise = "ctf/respawn.wav";
539 precache_sound(self.noise);
540 self.touch = GoalTouch;
543 void spawnfunc_nexball_redgoal (void)
545 self.team = COLOR_TEAM1;
548 void spawnfunc_nexball_bluegoal (void)
550 self.team = COLOR_TEAM2;
553 void spawnfunc_nexball_yellowgoal (void)
555 self.team = COLOR_TEAM3;
558 void spawnfunc_nexball_pinkgoal (void)
560 self.team = COLOR_TEAM4;
564 void spawnfunc_nexball_fault (void)
566 self.team = GOAL_FAULT;
568 self.noise = "misc/typehit.wav";
572 void spawnfunc_nexball_out (void)
574 self.team = GOAL_OUT;
576 self.noise = "misc/typehit.wav";
581 //Spawnfuncs preserved for compatibility
584 void spawnfunc_ball (void) { spawnfunc_nexball_football(); }
585 void spawnfunc_ball_football (void) { spawnfunc_nexball_football(); }
586 void spawnfunc_ball_basketball (void) { spawnfunc_nexball_basketball(); }
587 // The "red goal" is defended by blue team. A ball in there counts as a point for red.
588 void spawnfunc_ball_redgoal (void) { spawnfunc_nexball_bluegoal(); } // I blame Revenant
589 void spawnfunc_ball_bluegoal (void) { spawnfunc_nexball_redgoal(); } // but he didn't mean to cause trouble :p
590 void spawnfunc_ball_fault (void) { spawnfunc_nexball_fault(); }
591 void spawnfunc_ball_bound (void) { spawnfunc_nexball_out(); }
593 //=======================//
595 //=======================//
597 void W_Nexball_Touch (void)
599 local entity ball, attacker;
600 attacker = self.owner;
603 if(attacker.team != other.team || g_nexball_basketball_teamsteal)
604 if((ball = other.ballcarried) && (attacker.classname == "player" || attacker.classname == "gib"))
606 other.velocity = other.velocity + normalize(self.velocity) * other.damageforcescale * autocvar_g_balance_nexball_secondary_force;
607 other.flags &~= FL_ONGROUND;
608 if(!attacker.ballcarried)
610 LogNB("stole", attacker);
611 sound (other, CHAN_AUTO, ball.noise2, VOL_BASE, ATTN_NORM);
613 if(attacker.team == other.team && time > attacker.teamkill_complain)
615 attacker.teamkill_complain = time + 5;
616 attacker.teamkill_soundtime = time + 0.4;
617 attacker.teamkill_soundsource = other;
620 GiveBall(attacker, other.ballcarried);
626 void W_Nexball_Attack (float t)
629 local float mul, mi, ma;
630 if (!(ball = self.ballcarried))
633 W_SetupShot (self, FALSE, 4, "nexball/shoot1.wav", CHAN_WEAPON, 0);
634 tracebox(w_shotorg, BALL_MINS, BALL_MAXS, w_shotorg, MOVE_WORLDONLY, world);
638 self.metertime = 0; // Shot failed, hide the power meter
642 //Calculate multiplier
647 mi = autocvar_g_nexball_basketball_meter_minpower;
648 ma = max(mi, autocvar_g_nexball_basketball_meter_maxpower); // avoid confusion
649 //One triangle wave period with 1 as max
650 mul = 2 * mod(t, g_nexball_meter_period) / g_nexball_meter_period;
653 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
655 DropBall (ball, w_shotorg, W_CalculateProjectileVelocity(self.velocity, w_shotdir * autocvar_g_balance_nexball_primary_speed * mul, FALSE));
656 //TODO: use the speed_up cvar too ??
659 void W_Nexball_Attack2 (void)
661 local entity missile;
662 if (!(balls & BALL_BASKET))
664 W_SetupShot (self, FALSE, 2, "nexball/shoot2.wav", CHAN_WEAPON, 0);
665 // pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
668 missile.owner = self;
669 missile.classname = "ballstealer";
671 missile.movetype = MOVETYPE_FLY;
672 PROJECTILE_MAKETRIGGER(missile);
674 setmodel (missile, "models/elaser.mdl"); // precision set below
675 setsize (missile, '0 0 0', '0 0 0');
676 setorigin (missile, w_shotorg);
678 W_SetupProjectileVelocity(missile, autocvar_g_balance_nexball_secondary_speed, 0);
679 missile.angles = vectoangles (missile.velocity);
680 missile.touch = W_Nexball_Touch;
681 missile.think = SUB_Remove;
682 missile.nextthink = time + autocvar_g_balance_nexball_secondary_lifetime; //FIXME: use a distance instead?
684 missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
685 missile.flags = FL_PROJECTILE;
688 float w_nexball_weapon(float req)
692 if (self.BUTTON_ATCK)
693 if (weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
694 if (autocvar_g_nexball_basketball_meter)
696 if (self.ballcarried && !self.metertime)
697 self.metertime = time;
699 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
703 W_Nexball_Attack(-1);
704 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
706 if (self.BUTTON_ATCK2)
707 if (weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
710 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
713 if (!self.BUTTON_ATCK && self.metertime && self.ballcarried)
715 W_Nexball_Attack(time - self.metertime);
716 // DropBall or stealing will set metertime back to 0
717 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
720 else if (req == WR_PRECACHE)
722 precache_model ("models/weapons/g_porto.md3");
723 precache_model ("models/weapons/v_porto.md3");
724 precache_model ("models/weapons/h_porto.dpm");
725 precache_model ("models/elaser.mdl");
726 precache_sound ("nexball/shoot1.wav");
727 precache_sound ("nexball/shoot2.wav");
728 precache_sound ("misc/typehit.wav");
730 else if (req == WR_SETUP)
731 weapon_setup(WEP_PORTO);
732 else if (req == WR_SUICIDEMESSAGE)
734 w_deathtypestring = "is a weirdo";
736 else if (req == WR_KILLMESSAGE)
738 w_deathtypestring = "got killed by #'s black magic";
740 // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE