]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/sv_spawn_near_teammate.qc
fab94a1d7d34b54c87e35cff8fc17e6e68684931
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / spawn_near_teammate / sv_spawn_near_teammate.qc
1 #include "sv_spawn_near_teammate.qh"
2
3 #include <lib/float.qh>
4
5 float autocvar_g_spawn_near_teammate_distance;
6 int autocvar_g_spawn_near_teammate_ignore_spawnpoint;
7 int autocvar_g_spawn_near_teammate_ignore_spawnpoint_max;
8 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
9 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
10 bool autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health;
11 bool autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath;
12
13 REGISTER_MUTATOR(spawn_near_teammate, cvar("g_spawn_near_teammate"));
14
15 .entity msnt_lookat;
16
17 .float msnt_timer;
18
19 .float cvar_cl_spawn_near_teammate;
20
21 MUTATOR_HOOKFUNCTION(spawn_near_teammate, Spawn_Score)
22 {
23         if (!teamplay) return;
24
25         entity player = M_ARGV(0, entity);
26         entity spawn_spot = M_ARGV(1, entity);
27         vector spawn_score = M_ARGV(2, vector);
28
29         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && CS(player).cvar_cl_spawn_near_teammate))
30                 return;
31
32         spawn_spot.msnt_lookat = NULL;
33
34         RandomSelection_Init();
35         FOREACH_CLIENT(IS_PLAYER(it) && it != player && SAME_TEAM(it, player) && !IS_DEAD(it), {
36                 if(vdist(spawn_spot.origin - it.origin, >, autocvar_g_spawn_near_teammate_distance))
37                         continue;
38                 if(vdist(spawn_spot.origin - it.origin, <, 48))
39                         continue;
40                 if(!checkpvs(spawn_spot.origin, it))
41                         continue;
42                 RandomSelection_AddEnt(it, 1, 1);
43         });
44
45         if(RandomSelection_chosen_ent)
46         {
47                 spawn_spot.msnt_lookat = RandomSelection_chosen_ent;
48                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
49         }
50         else if(player.team == spawn_spot.team)
51                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
52
53         M_ARGV(2, vector) = spawn_score;
54 }
55
56 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
57 {
58         if (!teamplay) return;
59
60         // in assault it makes it prety much impossible to stop an attack
61         if (g_assault) return;
62
63         // when players fall off the map they respawn near a teammate which is usually a safe place and get unforzen too easily
64         if (g_freezetag) return;
65
66         entity player = M_ARGV(0, entity);
67         entity spawn_spot = M_ARGV(1, entity);
68
69         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
70         FOREACH_CLIENT(IS_PLAYER(it),
71         {
72                 switch(it.team)
73                 {
74                         case NUM_TEAM_1: ++num_red; break;
75                         case NUM_TEAM_2: ++num_blue; break;
76                         case NUM_TEAM_3: ++num_yellow; break;
77                         case NUM_TEAM_4: ++num_pink; break;
78                 }
79         });
80
81         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
82                 return; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
83
84         // Note: when entering this, fixangle is already set.
85         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && CS(player).cvar_cl_spawn_near_teammate))
86         {
87                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
88                         player.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
89
90                 entity best_mate = NULL;
91                 vector best_pos = '0 0 0';
92                 float best_dist2 = FLOAT_MAX;
93                 int tested = 0;
94                 FOREACH_CLIENT_RANDOM(IS_PLAYER(it), {
95                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_max && tested >= autocvar_g_spawn_near_teammate_ignore_spawnpoint_max) break;
96
97                         if (PHYS_INPUT_BUTTON_CHAT(it)) continue;
98                         if (!SAME_TEAM(player, it)) continue;
99                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health && it.health < autocvar_g_balance_health_regenstable) continue;
100                         if (IS_DEAD(it)) continue;
101                         if (time < it.msnt_timer) continue;
102                         if (time < it.spawnshieldtime) continue;
103                         if (forbidWeaponUse(it)) continue;
104                         if (it == player) continue;
105
106                         tested++; // i consider a teammate to be available when he passes the checks above
107
108                         vector horiz_vel = vec2(it.velocity);
109                         // when walking slowly sideways, we assume the player wants a clear shot ahead - spawn behind him according to where he's looking
110                         // when running fast, spawn behind him according to his direction of movement to prevent colliding with the newly spawned player
111                         if (vdist(horiz_vel, >, autocvar_sv_maxspeed + 50))
112                                 fixedmakevectors(vectoangles(horiz_vel));
113                         else
114                                 fixedmakevectors(it.angles); // .angles is the angle of the model - usually/always 0 pitch
115
116                         // test different spots close to mate - trace upwards so it works on uneven surfaces
117                         // don't spawn in front of player or directly behind to avoid players shooting each other
118                         // test the potential spots in pairs (first pair is better than second and so on) but don't prefer one side
119                         RandomSelection_Init();
120                         for(int i = 0; i < 6; ++i)
121                         {
122                                 switch(i)
123                                 {
124                                         case 0:
125                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - v_forward * 64 + v_right * 128 + v_up * 64, MOVE_NOMONSTERS, it);
126                                                 break;
127                                         case 1:
128                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - v_forward * 64 - v_right * 128 + v_up * 64, MOVE_NOMONSTERS, it);
129                                                 break;
130                                         case 2:
131                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin + v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
132                                                 break;
133                                         case 3:
134                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
135                                                 break;
136                                         case 4:
137                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - v_forward * 128 + v_right * 64 + v_up * 64, MOVE_NOMONSTERS, it);
138                                                 break;
139                                         case 5:
140                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - v_forward * 128 - v_right * 64 + v_up * 64, MOVE_NOMONSTERS, it);
141                                                 break;
142                                 }
143
144                                 vector horizontal_trace_endpos = trace_endpos;
145                                 //te_lightning1(NULL, it.origin, horizontal_trace_endpos);
146                                 if (trace_fraction != 1.0) goto skip;
147
148                                 // 400 is about the height of a typical laser jump (in overkill)
149                                 // not traceline because we need space for the whole player, not just his origin
150                                 tracebox(horizontal_trace_endpos, STAT(PL_MIN, player), STAT(PL_MAX, player), horizontal_trace_endpos - '0 0 400', MOVE_NORMAL, it);
151                                 vector vectical_trace_endpos = trace_endpos;
152                                 //te_lightning1(NULL, horizontal_trace_endpos, vectical_trace_endpos);
153                                 if (trace_startsolid) goto skip; // inside another player
154                                 if (trace_fraction == 1.0) goto skip; // above void or too high
155                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY) goto skip;
156                                 if (pointcontents(vectical_trace_endpos) != CONTENT_EMPTY) goto skip; // no lava or slime (or water which i assume would be annoying anyway)
157                                 if (tracebox_hits_trigger_hurt(horizontal_trace_endpos, STAT(PL_MIN, player), STAT(PL_MAX, player), vectical_trace_endpos)) goto skip;
158
159                                 // make sure the spawned player will have floor ahead (or at least a wall - he shouldn't fall as soon as he starts moving)
160                                 vector floor_test_start = vectical_trace_endpos + v_up * STAT(PL_MAX, player).z + v_forward * STAT(PL_MAX, player).x; // top front of player's bbox - highest point we know is not inside solid
161                                 traceline(floor_test_start, floor_test_start + v_forward * 100 - v_up * 128, MOVE_NOMONSTERS, it);
162                                 //te_beam(NULL, floor_test_start, trace_endpos);
163                                 if (trace_fraction == 1.0) goto skip;
164
165                                 if (autocvar_g_nades) {
166                                         bool nade_in_range = false;
167                                         IL_EACH(g_projectiles, it.classname == "nade",
168                                         {
169                                                 if (vdist(it.origin - vectical_trace_endpos, <, autocvar_g_nades_nade_radius)) {
170                                                         nade_in_range = true;
171                                                         goto skip;
172                                                 }
173                                         });
174                                         if (nade_in_range) goto skip;
175                                 }
176
177                                 // here, we know we found a good spot
178                                 RandomSelection_Add(it, 0, string_null, vectical_trace_endpos, 1, 1);
179                                 //te_lightning1(NULL, vectical_trace_endpos, vectical_trace_endpos + v_forward * 10);
180
181 LABEL(skip)
182                                 if (i % 2 == 1 && RandomSelection_chosen_ent)
183                                 {
184                                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
185                                         {
186                                                 float dist2 = vlen2(RandomSelection_chosen_ent.origin - player.death_origin);
187                                                 if (dist2 < best_dist2)
188                                                 {
189                                                         best_dist2 = dist2;
190                                                         best_pos = RandomSelection_chosen_vec;
191                                                         best_mate = RandomSelection_chosen_ent;
192                                                 }
193                                         }
194                                         else
195                                         {
196                                                 setorigin(player, RandomSelection_chosen_vec);
197                                                 player.angles = RandomSelection_chosen_ent.angles;
198                                                 player.angles_z = 0; // never spawn tilted even if the spot says to
199                                                 RandomSelection_chosen_ent.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
200                                                 return;
201                                         }
202                                         break; // don't test the other spots near this teammate, go to the next one
203                                 }
204                         }
205                 });
206
207                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
208                 if(best_mate)
209                 {
210                         setorigin(player, best_pos);
211                         player.angles = best_mate.angles;
212                         player.angles_z = 0; // never spawn tilted even if the spot says to
213                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
214                 }
215         }
216         else if(spawn_spot.msnt_lookat)
217         {
218                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
219                 player.angles_x = -player.angles.x;
220                 player.angles_z = 0; // never spawn tilted even if the spot says to
221                 /*
222                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
223                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
224                 sprint(player, "angles: ", vtos(player.angles), "\n");
225                 */
226         }
227 }
228
229 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");