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