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