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