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;
34 void nb_delayedinit();
35 void nb_init() // Called early (worldspawn stage)
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);
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
49 radar_showennemies = autocvar_g_nexball_radar_showallplayers;
51 InitializeEntity(world, nb_delayedinit, INITPRIO_GAMETYPE);
54 float OtherTeam(float t) //works only if there are two teams on the map!
57 e = find(world, classname, "nexball_team");
59 e = find(e, classname, "nexball_team");
65 void LogNB(string mode, entity actor)
68 if(!autocvar_sv_eventlog)
70 s = strcat(":nexball:", mode);
72 s = strcat(s, ":", ftos(actor.playerid));
76 void ball_restart (void)
79 DropBall(self, self.owner.origin, '0 0 0');
83 void nexball_setstatus (void)
86 self.items &~= IT_KEY1;
89 if (self.ballcarried.teamtime && (self.ballcarried.teamtime < time))
91 bprint("The ", ColoredTeamName(self.team), " held the ball for too long.\n");
93 self = self.ballcarried;
94 DropBall(self, self.owner.origin, '0 0 0');
98 self.items |= IT_KEY1;
102 void relocate_nexball (void)
104 tracebox(self.origin, BALL_MINS, BALL_MAXS, self.origin, TRUE, self);
105 if (trace_startsolid)
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");
119 void basketball_touch();
120 void football_touch();
122 void DropOwner (void)
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;
132 void GiveBall (entity plyr, entity ball)
136 if ((ownr = ball.owner))
138 ownr.effects &~= g_nexball_basketball_effects_default;
139 ownr.ballcarried = world;
143 ownr.weaponentity.state = WS_READY;
145 WaypointSprite_Kill(ownr.waypointsprite_attachedforcarrier);
149 WaypointSprite_Kill(ball.waypointsprite_attachedforcarrier);
152 setattachment(ball, plyr, "");
153 setorigin(ball, BALL_ATTACHORG);
155 if (ball.team != plyr.team)
156 ball.teamtime = time + g_nexball_basketball_delay_hold_forteam;
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;
163 plyr.effects |= g_nexball_basketball_effects_default;
164 ball.effects &~= g_nexball_basketball_effects_default;
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.
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);
176 if (g_nexball_basketball_delay_hold)
178 ball.think = DropOwner;
179 ball.nextthink = time + g_nexball_basketball_delay_hold;
183 void DropBall (entity ball, vector org, vector vel)
185 ball.effects |= g_nexball_basketball_effects_default;
186 ball.effects &~= EF_NOSHADOW;
187 ball.owner.effects &~= g_nexball_basketball_effects_default;
189 setattachment(ball, world, "");
190 setorigin (ball, org);
191 ball.movetype = MOVETYPE_BOUNCE;
192 ball.flags &~= FL_ONGROUND;
193 ball.scale = ball_scale;
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);
200 if (ball.owner.metertime)
202 ball.owner.metertime = 0;
203 ball.owner.weaponentity.state = WS_READY;
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);
212 ball.owner.ballcarried = world;
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;
226 self.think = ResetBall;
227 self.nextthink = time + g_nexball_delay_idle + 3;
231 sound (self, CHAN_PROJECTILE, self.noise1, VOL_BASE, ATTN_NORM);
232 WaypointSprite_Ping(self.waypointsprite_attachedforcarrier);
233 LogNB("init", world);
236 void ResetBall (void)
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?
245 LogNB("resetidle", world);
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;
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;
266 void football_touch (void)
268 if (other.solid == SOLID_BSP) {
269 if (time > self.lastground + 0.1)
271 sound (self, CHAN_PROJECTILE, self.noise, VOL_BASE, ATTN_NORM);
272 self.lastground = time;
274 if (vlen(self.velocity) && !self.cnt)
275 self.nextthink = time + g_nexball_delay_idle;
278 if (other.classname != "player")
280 if (other.health < 1)
283 self.nextthink = time + g_nexball_delay_idle;
286 self.team = other.team;
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;
301 self.avelocity = -250 * v_forward; // maybe there is a way to make it look better?
304 void basketball_touch (void)
306 if (other.ballcarried)
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)
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);
323 void GoalTouch (void)
326 float isclient, pscore, otherteam;
329 if (gameover) return;
330 if ((self.spawnflags & GOAL_TOUCHPLAYER) && other.ballcarried)
331 ball = other.ballcarried;
334 if (ball.classname != "nexball_basketball")
335 if (ball.classname != "nexball_football")
337 if ((!ball.pusher && self.team != GOAL_OUT) || ball.cnt)
343 otherteam = OtherTeam(ball.team);
345 if((isclient = ball.pusher.flags & FL_CLIENT))
346 pname = ball.pusher.netname;
348 pname = "Someone (?)";
350 if (ball.team == self.team) //owngoal (regular goals)
352 LogNB("owngoal", ball.pusher);
353 bprint("Boo! ", pname, "^7 scored a goal against their own team!\n");
355 } else if (self.team == GOAL_FAULT) {
356 LogNB("fault", ball.pusher);
358 bprint(ColoredTeamName(otherteam), " gets a point due to ", pname, "^7's silliness.\n");
360 bprint(ColoredTeamName(ball.team), " loses a point due to ", pname, "^7's silliness.\n");
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");
367 bprint("The ball was returned.\n");
370 LogNB(strcat("goal:", ftos(self.team)), ball.pusher);
371 bprint("Goaaaaal! ", pname, "^7 scored a point for the ", ColoredTeamName(ball.team), ".\n");
375 sound (ball, CHAN_AUTO, self.noise, VOL_BASE, ATTN_NONE);
377 if(ball.team && pscore)
379 if (nb_teams == 2 && pscore < 0)
380 TeamScore_AddToTeam(otherteam, ST_NEXBALL_GOALS, -pscore);
382 TeamScore_AddToTeam(ball.team, ST_NEXBALL_GOALS, pscore);
387 PlayerScore_Add(ball.pusher, SP_NEXBALL_GOALS, pscore);
389 PlayerScore_Add(ball.pusher, SP_NEXBALL_FAULTS, -pscore);
392 if (ball.owner) // Happens on spawnflag GOAL_TOUCHPLAYER
393 DropBall(ball, ball.owner.origin, ball.owner.velocity);
395 WaypointSprite_Ping(ball.waypointsprite_attachedforcarrier);
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);
404 //=======================//
406 //=======================//
407 void spawnfunc_nexball_team (void)
409 if(!g_nexball) { remove(self); return; }
410 self.team = self.cnt + 1;
413 void nb_spawnteam (string teamname, float teamcolor)
415 dprint("^2spawned team ", teamname, "\n");
418 e.classname = "nexball_team";
419 e.netname = teamname;
425 void nb_spawnteams (void)
427 float t_r, t_b, t_y, t_p;
429 for(e = world; (e = find(e, classname, "nexball_goal")); )
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;
441 void nb_delayedinit (void)
443 if (find(world, classname, "nexball_team") == world)
445 ScoreRules_nexball(nb_teams);
449 //=======================//
451 //=======================//
453 void SpawnBall (void)
455 if(!g_nexball) { remove(self); return; }
457 // balls += 4; // using the remaining bits to count balls will leave more than the max edict count, so it's fine
460 self.model = "models/nexball/ball.md3";
464 precache_model (self.model);
465 setmodel (self, self.model);
466 setsize (self, BALL_MINS, BALL_MAXS);
467 ball_scale = self.scale;
470 self.spawnorigin = self.origin;
472 self.effects = self.effects | EF_LOWPRECISION;
474 if (cvar(strcat("g_", self.classname, "_trail"))) //nexball_basketball :p
476 self.glow_color = autocvar_g_nexball_trail_color;
477 self.glow_trail = TRUE;
480 self.movetype = MOVETYPE_FLY;
482 if (!autocvar_g_nexball_sound_bounce)
484 else if (!self.noise)
485 self.noise = "sound/nexball/bounce.wav";
486 //bounce sound placeholder (FIXME)
488 self.noise1 = "sound/nexball/drop.wav";
489 //ball drop sound placeholder (FIXME)
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);
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);
500 self.reset = ball_restart;
501 self.think = InitBall;
502 self.nextthink = game_starttime + autocvar_g_nexball_delay_start;
505 void spawnfunc_nexball_basketball (void)
507 self.classname = "nexball_basketball";
508 if not(balls & BALL_BASKET)
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;
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;
525 void spawnfunc_nexball_football (void)
527 self.classname = "nexball_football";
528 self.solid = SOLID_TRIGGER;
530 self.bouncefactor = autocvar_g_nexball_football_bouncefactor;
531 self.bouncestop = autocvar_g_nexball_football_bouncestop;
535 void SpawnGoal (void)
537 if(!g_nexball) { remove(self); return; }
539 self.classname = "nexball_goal";
541 self.noise = "ctf/respawn.wav";
542 precache_sound(self.noise);
543 self.touch = GoalTouch;
546 void spawnfunc_nexball_redgoal (void)
548 self.team = COLOR_TEAM1;
551 void spawnfunc_nexball_bluegoal (void)
553 self.team = COLOR_TEAM2;
556 void spawnfunc_nexball_yellowgoal (void)
558 self.team = COLOR_TEAM3;
561 void spawnfunc_nexball_pinkgoal (void)
563 self.team = COLOR_TEAM4;
567 void spawnfunc_nexball_fault (void)
569 self.team = GOAL_FAULT;
571 self.noise = "misc/typehit.wav";
575 void spawnfunc_nexball_out (void)
577 self.team = GOAL_OUT;
579 self.noise = "misc/typehit.wav";
584 //Spawnfuncs preserved for compatibility
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(); }
596 //=======================//
598 //=======================//
600 void W_Nexball_Touch (void)
602 local entity ball, attacker;
603 attacker = self.owner;
606 if(attacker.team != other.team || g_nexball_basketball_teamsteal)
607 if((ball = other.ballcarried) && (attacker.classname == "player" || attacker.classname == "gib"))
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)
613 LogNB("stole", attacker);
614 sound (other, CHAN_AUTO, ball.noise2, VOL_BASE, ATTN_NORM);
616 if(attacker.team == other.team && time > attacker.teamkill_complain)
618 attacker.teamkill_complain = time + 5;
619 attacker.teamkill_soundtime = time + 0.4;
620 attacker.teamkill_soundsource = other;
623 GiveBall(attacker, other.ballcarried);
629 void W_Nexball_Attack (float t)
632 local float mul, mi, ma;
633 if (!(ball = self.ballcarried))
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);
641 self.metertime = 0; // Shot failed, hide the power meter
645 //Calculate multiplier
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;
656 mul = mi + (ma - mi) * mul; // range from the minimal power to the maximal power
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 ??
662 void W_Nexball_Attack2 (void)
664 local entity missile;
665 if (!(balls & BALL_BASKET))
667 W_SetupShot (self, FALSE, 2, "nexball/shoot2.wav", CHAN_WEAPON, 0);
668 // pointparticles(particleeffectnum("grenadelauncher_muzzleflash"), w_shotorg, w_shotdir * 1000, 1);
671 missile.owner = self;
672 missile.classname = "ballstealer";
674 missile.movetype = MOVETYPE_FLY;
675 PROJECTILE_MAKETRIGGER(missile);
677 setmodel (missile, "models/elaser.mdl"); // precision set below
678 setsize (missile, '0 0 0', '0 0 0');
679 setorigin (missile, w_shotorg);
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?
687 missile.effects = EF_BRIGHTFIELD | EF_LOWPRECISION;
688 missile.flags = FL_PROJECTILE;
691 float w_nexball_weapon(float req)
695 if (self.BUTTON_ATCK)
696 if (weapon_prepareattack(0, autocvar_g_balance_nexball_primary_refire))
697 if (autocvar_g_nexball_basketball_meter)
699 if (self.ballcarried && !self.metertime)
700 self.metertime = time;
702 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
706 W_Nexball_Attack(-1);
707 weapon_thinkf(WFRAME_FIRE1, autocvar_g_balance_nexball_primary_animtime, w_ready);
709 if (self.BUTTON_ATCK2)
710 if (weapon_prepareattack(1, autocvar_g_balance_nexball_secondary_refire))
713 weapon_thinkf(WFRAME_FIRE2, autocvar_g_balance_nexball_secondary_animtime, w_ready);
716 if (!self.BUTTON_ATCK && self.metertime && self.ballcarried)
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);
723 else if (req == WR_PRECACHE)
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");
733 else if (req == WR_SETUP)
734 weapon_setup(WEP_PORTO);
735 else if (req == WR_SUICIDEMESSAGE)
737 w_deathtypestring = "is a weirdo";
739 else if (req == WR_KILLMESSAGE)
741 w_deathtypestring = "got killed by #'s black magic";
743 // No need to check WR_CHECKAMMO* or WR_AIM, it should always return TRUE