]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/sv_spawn_near_teammate.qc
Merge branch 'master' into terencehill/ft_autorevive_progress
[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 string autocvar_g_spawn_near_teammate;
6 float autocvar_g_spawn_near_teammate_distance;
7 int autocvar_g_spawn_near_teammate_ignore_spawnpoint;
8 int autocvar_g_spawn_near_teammate_ignore_spawnpoint_max;
9 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
10 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
11 bool autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health;
12 bool autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath;
13
14 REGISTER_MUTATOR(spawn_near_teammate, expr_evaluate(autocvar_g_spawn_near_teammate));
15
16 .entity msnt_lookat;
17
18 .float msnt_timer;
19
20 .float cvar_cl_spawn_near_teammate;
21
22 MUTATOR_HOOKFUNCTION(spawn_near_teammate, Spawn_Score)
23 {
24         if (!teamplay) return;
25
26         entity player = M_ARGV(0, entity);
27         entity spawn_spot = M_ARGV(1, entity);
28         vector spawn_score = M_ARGV(2, vector);
29
30         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && CS(player).cvar_cl_spawn_near_teammate))
31                 return;
32
33         spawn_spot.msnt_lookat = NULL;
34
35         RandomSelection_Init();
36         FOREACH_CLIENT(IS_PLAYER(it) && it != player && SAME_TEAM(it, player) && !IS_DEAD(it), {
37                 if(vdist(spawn_spot.origin - it.origin, >, autocvar_g_spawn_near_teammate_distance))
38                         continue;
39                 if(vdist(spawn_spot.origin - it.origin, <, 48))
40                         continue;
41                 if(!checkpvs(spawn_spot.origin, it))
42                         continue;
43                 RandomSelection_AddEnt(it, 1, 1);
44         });
45
46         if(RandomSelection_chosen_ent)
47         {
48                 spawn_spot.msnt_lookat = RandomSelection_chosen_ent;
49                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
50         }
51         else if(player.team == spawn_spot.team)
52                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
53
54         M_ARGV(2, vector) = spawn_score;
55 }
56
57 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
58 {
59         if (!teamplay) return;
60
61         entity player = M_ARGV(0, entity);
62         entity spawn_spot = M_ARGV(1, entity);
63
64         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
65         FOREACH_CLIENT(IS_PLAYER(it),
66         {
67                 switch(it.team)
68                 {
69                         case NUM_TEAM_1: ++num_red; break;
70                         case NUM_TEAM_2: ++num_blue; break;
71                         case NUM_TEAM_3: ++num_yellow; break;
72                         case NUM_TEAM_4: ++num_pink; break;
73                 }
74         });
75
76         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
77                 return; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
78
79         // Note: when entering this, fixangle is already set.
80         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && CS(player).cvar_cl_spawn_near_teammate))
81         {
82                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
83                         player.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
84
85                 entity best_mate = NULL;
86                 vector best_pos = '0 0 0';
87                 float best_dist2 = FLOAT_MAX;
88                 int tested = 0;
89                 FOREACH_CLIENT_RANDOM(IS_PLAYER(it), {
90                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_max && tested >= autocvar_g_spawn_near_teammate_ignore_spawnpoint_max) break;
91
92                         if (PHYS_INPUT_BUTTON_CHAT(it)) continue;
93                         if (DIFF_TEAM(player, it)) continue;
94                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health && GetResource(it, RES_HEALTH) < autocvar_g_balance_health_regenstable) continue;
95                         if (IS_DEAD(it)) continue;
96                         if (time < it.msnt_timer) continue;
97                         if (time < it.spawnshieldtime) continue;
98                         if (weaponLocked(it)) continue;
99                         if (it == player) continue;
100
101                         tested++; // i consider a teammate to be available when he passes the checks above
102
103                         vector horiz_vel = vec2(it.velocity);
104                         // when walking slowly sideways, we assume the player wants a clear shot ahead - spawn behind him according to where he's looking
105                         // when running fast, spawn behind him according to his direction of movement to prevent colliding with the newly spawned player
106                         vector forward = '0 0 0'; vector right = '0 0 0'; vector up = '0 0 0';
107                         if (vdist(horiz_vel, >, autocvar_sv_maxspeed + 50))
108                         {
109                                 FIXED_MAKE_VECTORS(vectoangles(horiz_vel), forward, right, up);
110                         }
111                         else
112                         {
113                                 FIXED_MAKE_VECTORS(it.angles, forward, right, up);
114                         }
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 - forward * 64 + right * 128 + up * 64, MOVE_NOMONSTERS, it);
126                                                 break;
127                                         case 1:
128                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - forward * 64 - right * 128 + up * 64, MOVE_NOMONSTERS, it);
129                                                 break;
130                                         case 2:
131                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin + right * 192 + up * 64, MOVE_NOMONSTERS, it);
132                                                 break;
133                                         case 3:
134                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - right * 192 + up * 64, MOVE_NOMONSTERS, it);
135                                                 break;
136                                         case 4:
137                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - forward * 128 + right * 64 + up * 64, MOVE_NOMONSTERS, it);
138                                                 break;
139                                         case 5:
140                                                 tracebox(it.origin, STAT(PL_MIN, player), STAT(PL_MAX, player), it.origin - forward * 128 - right * 64 + 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 - 400 * up, 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                                 // top front of player's bbox - highest point we know is not inside solid
161                                 vector floor_test_start = vectical_trace_endpos + up * STAT(PL_MAX, player).z + forward * STAT(PL_MAX, player).x; 
162                                 traceline(floor_test_start, floor_test_start + forward * 100 - up * 128, MOVE_NOMONSTERS, it);
163                                 //te_beam(NULL, floor_test_start, trace_endpos);
164                                 if (trace_fraction == 1.0) goto skip;
165
166                                 if (autocvar_g_nades) {
167                                         bool nade_in_range = false;
168                                         IL_EACH(g_projectiles, it.classname == "nade",
169                                         {
170                                                 if (vdist(it.origin - vectical_trace_endpos, <, autocvar_g_nades_nade_radius)) {
171                                                         nade_in_range = true;
172                                                         goto skip;
173                                                 }
174                                         });
175                                         if (nade_in_range) goto skip;
176                                 }
177
178                                 // here, we know we found a good spot
179                                 RandomSelection_Add(it, 0, string_null, vectical_trace_endpos, 1, 1);
180                                 //te_lightning1(NULL, vectical_trace_endpos, vectical_trace_endpos + forward * 10);
181
182 LABEL(skip)
183                                 if (i % 2 == 1 && RandomSelection_chosen_ent)
184                                 {
185                                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
186                                         {
187                                                 float dist2 = vlen2(RandomSelection_chosen_ent.origin - player.death_origin);
188                                                 if (dist2 < best_dist2)
189                                                 {
190                                                         best_dist2 = dist2;
191                                                         best_pos = RandomSelection_chosen_vec;
192                                                         best_mate = RandomSelection_chosen_ent;
193                                                 }
194                                         }
195                                         else
196                                         {
197                                                 setorigin(player, RandomSelection_chosen_vec);
198                                                 player.angles = RandomSelection_chosen_ent.angles;
199                                                 player.angles_z = 0; // never spawn tilted even if the spot says to
200                                                 RandomSelection_chosen_ent.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
201                                                 return;
202                                         }
203                                         break; // don't test the other spots near this teammate, go to the next one
204                                 }
205                         }
206                 });
207
208                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
209                 if(best_mate)
210                 {
211                         setorigin(player, best_pos);
212                         player.angles = best_mate.angles;
213                         player.angles_z = 0; // never spawn tilted even if the spot says to
214                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
215                 }
216         }
217         else if(spawn_spot.msnt_lookat)
218         {
219                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
220                 player.angles_x = -player.angles.x;
221                 player.angles_z = 0; // never spawn tilted even if the spot says to
222                 /*
223                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
224                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
225                 sprint(player, "angles: ", vtos(player.angles), "\n");
226                 */
227         }
228 }
229
230 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");