]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/sv_spawn_near_teammate.qc
don't test all spots when a good one is found early
[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 float autocvar_g_spawn_near_teammate_distance;
4 int autocvar_g_spawn_near_teammate_ignore_spawnpoint;
5 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
6 float autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
7 int autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health;
8 bool autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath;
9
10 REGISTER_MUTATOR(spawn_near_teammate, cvar("g_spawn_near_teammate"));
11
12 .entity msnt_lookat;
13
14 .float msnt_timer;
15 .vector msnt_deathloc;
16
17 .float cvar_cl_spawn_near_teammate;
18
19 MUTATOR_HOOKFUNCTION(spawn_near_teammate, Spawn_Score)
20 {
21         entity player = M_ARGV(0, entity);
22         entity spawn_spot = M_ARGV(1, entity);
23         vector spawn_score = M_ARGV(2, vector);
24
25         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && player.cvar_cl_spawn_near_teammate))
26                 return;
27
28         spawn_spot.msnt_lookat = NULL;
29
30         if(!teamplay)
31                 return;
32
33         RandomSelection_Init();
34         FOREACH_CLIENT(IS_PLAYER(it) && it != player && SAME_TEAM(it, player) && !IS_DEAD(it), LAMBDA(
35                 if(vdist(spawn_spot.origin - it.origin, >, autocvar_g_spawn_near_teammate_distance))
36                         continue;
37                 if(vdist(spawn_spot.origin - it.origin, <, 48))
38                         continue;
39                 if(!checkpvs(spawn_spot.origin, it))
40                         continue;
41                 RandomSelection_AddEnt(it, 1, 1);
42         ));
43
44         if(RandomSelection_chosen_ent)
45         {
46                 spawn_spot.msnt_lookat = RandomSelection_chosen_ent;
47                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
48         }
49         else if(player.team == spawn_spot.team)
50                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
51
52         M_ARGV(2, vector) = spawn_score;
53 }
54
55 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
56 {
57         if(!teamplay) { return; }
58         entity player = M_ARGV(0, entity);
59         entity spawn_spot = M_ARGV(1, entity);
60
61         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
62         FOREACH_CLIENT(IS_PLAYER(it),
63         {
64                 switch(it.team)
65                 {
66                         case NUM_TEAM_1: ++num_red; break;
67                         case NUM_TEAM_2: ++num_blue; break;
68                         case NUM_TEAM_3: ++num_yellow; break;
69                         case NUM_TEAM_4: ++num_pink; break;
70                 }
71         });
72
73         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
74                 return; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
75
76         // Note: when entering this, fixangle is already set.
77         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && player.cvar_cl_spawn_near_teammate))
78         {
79                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
80                         player.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
81
82                 entity best_mate = NULL;
83                 vector best_pos = '0 0 0';
84                 float best_dist = 0;
85                 FOREACH_CLIENT_RANDOM(IS_PLAYER(it), LAMBDA(
86                         //LOG_INFOF("  for client: %s %v\n", it.netname, it.origin);
87                         if (!SAME_TEAM(player, it)) continue;
88                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health >= 0 && it.health < autocvar_g_balance_health_regenstable) continue;
89                         if (IS_DEAD(it)) continue;
90                         if (time < it.msnt_timer) continue;
91                         if (time < it.spawnshieldtime) continue;
92                         if (forbidWeaponUse(it)) continue;
93                         if (it == player) continue;
94
95                         vector horiz_vel = vec2(it.velocity);
96                         if (vdist(horiz_vel, >, 450))
97                                 fixedmakevectors(vectoangles(horiz_vel));
98                         else
99                                 fixedmakevectors(it.angles); // .angles is the angle of the model - usually/always 0 pitch
100
101                         // test different spots close to mate - trace upwards so it works on uneven surfaces
102                         // don't spawn in front of player or directly behind to avoid players shooting each other
103                         // test the potential spots in pairs but don't prefer one side
104                         RandomSelection_Init();
105                         for(int i = 0; i < 6; ++i)
106                         {
107                                 switch(i)
108                                 {
109                                         case 0:
110                                                 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);
111                                                 break;
112                                         case 1:
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 2:
116                                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
117                                                 break;
118                                         case 3:
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 4:
122                                                 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);
123                                                 break;
124                                         case 5:
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                                 }
128                                 do {
129                                         vector horizontal_trace_endpos = trace_endpos;
130                                         //te_lightning1(NULL, it.origin, horizontal_trace_endpos);
131                                         if (trace_fraction != 1.0) break;
132
133                                         // 400 is about the height of a typical laser jump (in overkill)
134                                         // not traceline because we need space for the whole player, not just his origin
135                                         tracebox(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), horizontal_trace_endpos - '0 0 400', MOVE_NORMAL, it);
136                                         vector vectical_trace_endpos = trace_endpos;
137                                         //te_lightning1(NULL, horizontal_trace_endpos, vectical_trace_endpos);
138                                         if (trace_startsolid) break; // inside another player
139                                         if (trace_fraction == 1.0) break; // above void or too high
140                                         if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY) break;
141                                         if (pointcontents(vectical_trace_endpos) != CONTENT_EMPTY) break; // no lava or slime (or water which i assume would be annoying anyway)
142                                         if (tracebox_hits_trigger_hurt(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), vectical_trace_endpos)) break;
143
144                                         // make sure the spawned player will have floor ahead (or at least a wall - he shouldn't fall as soon as he starts moving)
145                                         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
146                                         traceline(floor_test_start, floor_test_start + v_forward * 100 - v_up * 128, MOVE_NOMONSTERS, it);
147                                         //te_beam(NULL, floor_test_start, trace_endpos);
148                                         if (trace_fraction == 1.0) break;
149
150                                         if (autocvar_g_nades) {
151                                                 bool nade_in_range = false;
152                                                 IL_EACH(g_projectiles, it.classname == "nade",
153                                                 {
154                                                         if (vdist(it.origin - vectical_trace_endpos, <, autocvar_g_nades_nade_radius)) {
155                                                                 nade_in_range = true;
156                                                                 break;
157                                                         }
158                                                 });
159                                                 if (nade_in_range) break;
160                                         }
161
162                                         // here, we know we found a good spot
163                                         RandomSelection_Add(it, 0, string_null, vectical_trace_endpos, 1, 1);
164                                         //te_lightning1(NULL, vectical_trace_endpos, vectical_trace_endpos + v_forward * 10);
165                                         //LOG_INFOF("mod: %d, ent: %d\n", i % 2 == 1, etof(RandomSelection_chosen_ent));
166                                 } while(0);
167
168                                 if (i % 2 == 1 && RandomSelection_chosen_ent)
169                                 {
170                                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
171                                         {
172                                                 float dist = vlen(RandomSelection_chosen_ent.origin - player.msnt_deathloc);
173                                                 //LOG_INFOF("      dist: %f, best_dist %f\n", dist, best_dist);
174                                                 if (dist < best_dist || best_dist == 0)
175                                                 {
176                                                         //LOG_INFOF("      new best dist - pos: %v\n", RandomSelection_chosen_vec);
177                                                         best_dist = dist;
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                                         continue; // 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_dist)
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 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
218 {
219         entity frag_target = M_ARGV(0, entity);
220
221         frag_target.msnt_deathloc = frag_target.origin;
222 }
223
224 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");