]> 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/msnt
[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         //LOG_INFOF("PlayerSpawn for player: %s %v spawn_spot: %v\n", player.netname, player.origin, spawn_spot.origin);
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_dist = 0;
87                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
88                         //LOG_INFOF("  for client: %s %v\n", it.netname, it.origin);
89                         if (!SAME_TEAM(player, it)) continue;
90                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health >= 0 && it.health < autocvar_g_balance_health_regenstable) continue;
91                         if (IS_DEAD(it)) continue;
92                         if (time < it.msnt_timer) continue;
93                         if (time < it.spawnshieldtime) continue;
94                         if (forbidWeaponUse(it)) continue;
95                         if (STAT(FROZEN, it)) continue;
96                         if (it == player) continue;
97
98                         vector horiz_vel = vec2(it.velocity);
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                         for(int i = 0; i < 6; ++i)
107                         {
108                                 switch(i)
109                                 {
110                                         case 0:
111                                                 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);
112                                                 break;
113                                         case 1:
114                                                 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);
115                                                 break;
116                                         case 2:
117                                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
118                                                 break;
119                                         case 3:
120                                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
121                                                 break;
122                                         case 4:
123                                                 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);
124                                                 break;
125                                         case 5:
126                                                 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);
127                                                 break;
128
129                                 }
130                                 vector horizontal_trace_endpos = trace_endpos;
131                                 //te_lightning1(NULL, it.origin, horizontal_trace_endpos);
132                                 if(trace_fraction != 1.0) continue;
133
134                                 // 400 is about the height of a typical laser jump (in overkill)
135                                 // not traceline because we need space for the whole player, not just his origin
136                                 tracebox(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), horizontal_trace_endpos - '0 0 400', MOVE_NORMAL, it);
137                                 vector vectical_trace_endpos = trace_endpos;
138                                 //te_lightning1(NULL, horizontal_trace_endpos, vectical_trace_endpos);
139                                 if (trace_startsolid) continue; // inside another player
140                                 if (trace_fraction == 1.0) continue; // above void or too high
141                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY) continue;
142                                 if (pointcontents(vectical_trace_endpos) != CONTENT_EMPTY) continue; // this also prevents spawning in water which i assume would be annoying
143                                 if (tracebox_hits_trigger_hurt(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), vectical_trace_endpos)) continue;
144
145                                 // make sure the spawned player will have floor ahead (or at least a wall - he shouldn't fall as soon as he starts moving)
146                                 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
147                                 traceline(floor_test_start, floor_test_start + v_forward * 100 - v_up * 128, MOVE_NOMONSTERS, it);
148                                 //te_beam(NULL, floor_test_start, trace_endpos);
149                                 if (trace_fraction == 1.0) continue;
150
151                                 if (autocvar_g_nades) {
152                                         bool nade_in_range = false;
153                                         IL_EACH(g_projectiles, it.classname == "nade",
154                                         {
155                                                 if (vdist(it.origin - vectical_trace_endpos, <, autocvar_g_nades_nade_radius)) {
156                                                         nade_in_range = true;
157                                                         break;
158                                                 }
159                                         });
160                                         if (nade_in_range) continue;
161                                 }
162
163                                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
164                                 {
165                                         float dist = vlen(vectical_trace_endpos - player.msnt_deathloc);
166                                         //LOG_INFOF("      dist: %f, best_dist %f\n", dist, best_dist);
167                                         if(dist < best_dist || best_dist == 0)
168                                         {
169                                                 //LOG_INFOF("      new best dist - pos: %v\n", vectical_trace_endpos);
170                                                 best_dist = dist;
171                                                 best_pos = vectical_trace_endpos;
172                                                 best_mate = it;
173                                         }
174                                 }
175                                 else // TODO randomize to avoid favoring players who joined early
176                                 {
177                                         setorigin(player, vectical_trace_endpos);
178                                         player.angles = it.angles;
179                                         player.angles_z = 0; // never spawn tilted even if the spot says to
180                                         it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
181                                         //LOG_INFOF("      PlayerSpawn return %v\n", player.origin);
182                                         return;
183                                 }
184                         }
185                 ));
186
187                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
188                 if(best_dist)
189                 {
190                         setorigin(player, best_pos);
191                         player.angles = best_mate.angles;
192                         player.angles_z = 0; // never spawn tilted even if the spot says to
193                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
194                 }
195         }
196         else if(spawn_spot.msnt_lookat)
197         {
198                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
199                 player.angles_x = -player.angles.x;
200                 player.angles_z = 0; // never spawn tilted even if the spot says to
201                 /*
202                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
203                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
204                 sprint(player, "angles: ", vtos(player.angles), "\n");
205                 */
206         }
207 }
208
209 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
210 {
211         entity frag_target = M_ARGV(0, entity);
212
213         frag_target.msnt_deathloc = frag_target.origin;
214 }
215
216 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");