]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/sv_spawn_near_teammate.qc
reorder
[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         //LOG_INFOF("Spawn_Score player: %s %v spawn_spot: %v spawn_score: %f\n", player.netname, player.origin, spawn_spot.origin, spawn_score);
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_Add(it, 0, string_null, 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         //LOG_INFOF("Spawn_Score ret %v", spawn_score);
55         M_ARGV(2, vector) = spawn_score;
56 }
57
58 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
59 {
60         //if(!teamplay) { return; } // TODO DEBUGGING
61         entity player = M_ARGV(0, entity);
62         entity spawn_spot = M_ARGV(1, entity);
63
64         LOG_INFOF("PlayerSpawn for player: %s %v spawn_spot: %v\n", player.netname, player.origin, spawn_spot.origin);
65
66         /*
67         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
68         FOREACH_CLIENT(IS_PLAYER(it),
69         {
70                 switch(it.team)
71                 {
72                         case NUM_TEAM_1: ++num_red; break;
73                         case NUM_TEAM_2: ++num_blue; break;
74                         case NUM_TEAM_3: ++num_yellow; break;
75                         case NUM_TEAM_4: ++num_pink; break;
76                 }
77         });
78
79         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
80                 return; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
81         */ // // TODO DEBUGING
82
83         // Note: when entering this, fixangle is already set.
84         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && player.cvar_cl_spawn_near_teammate))
85         {
86                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
87                         player.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
88
89                 entity best_mate = NULL;
90                 vector best_pos = '0 0 0';
91                 float best_dist = 0;
92                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
93                         LOG_INFOF("  for client: %s %v\n", it.netname, it.origin);
94                         //if (!SAME_TEAM(player, it)) continue; // TODO DEBUGING
95                         if (autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health >= 0 && it.health < autocvar_g_balance_health_regenstable) continue;
96                         if (IS_DEAD(it)) continue;
97                         if (time < it.msnt_timer) continue;
98                         if (time < it.spawnshieldtime) continue;
99                         if (forbidWeaponUse(it)) continue;
100                         if (STAT(FROZEN, it)) continue;
101                         if (it == player) continue;
102
103                         vector horiz_vel = vec2(it.velocity);
104                         if (vdist(horiz_vel, >, 450))
105                                 fixedmakevectors(vectoangles(horiz_vel));
106                         else
107                                 fixedmakevectors(it.angles); // .angles is the angle of the model - usually/always 0 pitch
108
109                         // test diffrent spots close to mate - trace upwards so it works on uneven surfaces
110                         // don't spawn in front of player or directly behind to avoid players shooting each other
111                         for(int i = 0; i < 6; ++i)
112                         {
113                                 switch(i)
114                                 {
115                                         case 0:
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 1:
119                                                 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);
120                                                 break;
121                                         case 2:
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 3:
125                                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 192 + v_up * 64, MOVE_NOMONSTERS, it);
126                                                 break;
127                                         case 4:
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                                         case 5:
131                                                 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);
132                                                 break;
133
134                                 }
135                                 vector horizontal_trace_endpos = trace_endpos;
136                                 te_lightning1(NULL, it.origin, horizontal_trace_endpos);
137                                 if(trace_fraction != 1.0) continue;
138
139                                 // 400 is about the height of a typical laser jump (in overkill)
140                                 // not traceline because we need space for the whole payer, not just his origin
141                                 tracebox(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), horizontal_trace_endpos - '0 0 400', MOVE_NORMAL, it);
142                                 vector vectical_trace_endpos = trace_endpos;
143                                 te_lightning1(NULL, horizontal_trace_endpos, vectical_trace_endpos);
144                                 if (trace_startsolid) continue; // inside another player
145                                 if (trace_fraction == 1.0) continue; // above void or too high
146                                 if (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY) continue;
147                                 if (pointcontents(vectical_trace_endpos) != CONTENT_EMPTY) continue; // this also prevents spawning in water which i assume would be annoying
148                                 if (tracebox_hits_trigger_hurt(horizontal_trace_endpos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), vectical_trace_endpos)) continue;
149
150                                 // make sure the spawned player will have floor ahead (or at least a wall - he shouldn't fall as soon as he starts moving)
151                                 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
152                                 traceline(floor_test_start, floor_test_start + v_forward * 100 - v_up * 128, MOVE_NOMONSTERS, it);
153                                 te_beam(NULL, floor_test_start, trace_endpos);
154                                 if (trace_fraction == 1.0) continue;
155
156                                 if (autocvar_g_nades) {
157                                         bool nade_in_range = false;
158                                         IL_EACH(g_projectiles, it.classname == "nade",
159                                         {
160                                                 if (vdist(it.origin - vectical_trace_endpos, <, autocvar_g_nades_nade_radius)) {
161                                                         nade_in_range = true;
162                                                         break;
163                                                 }
164                                         });
165                                         if (nade_in_range) continue;
166                                 }
167
168                                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
169                                 {
170                                         float dist = vlen(vectical_trace_endpos - player.msnt_deathloc);
171                                         LOG_INFOF("      dist: %f, best_dist %f\n", dist, best_dist);
172                                         if(dist < best_dist || best_dist == 0)
173                                         {
174                                                 LOG_INFOF("      new best dist - pos: %v\n", vectical_trace_endpos);
175                                                 best_dist = dist;
176                                                 best_pos = vectical_trace_endpos;
177                                                 best_mate = it;
178                                         }
179                                 }
180                                 else // TODO random to avoid favoring players who joined early
181                                 {
182                                         setorigin(player, vectical_trace_endpos);
183                                         player.angles = it.angles;
184                                         player.angles_z = 0; // never spawn tilted even if the spot says to
185                                         it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
186                                         LOG_INFOF("      PlayerSpawn return %v\n", player.origin);
187                                         return;
188                                 }
189                         }
190                 ));
191
192                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
193                 if(best_dist)
194                 {
195                         setorigin(player, best_pos);
196                         player.angles = best_mate.angles;
197                         player.angles_z = 0; // never spawn tilted even if the spot says to
198                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
199                 }
200         }
201         else if(spawn_spot.msnt_lookat)
202         {
203                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
204                 player.angles_x = -player.angles.x;
205                 player.angles_z = 0; // never spawn tilted even if the spot says to
206                 /*
207                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
208                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
209                 sprint(player, "angles: ", vtos(player.angles), "\n");
210                 */
211         }
212
213         LOG_INFOF("PlayerSpawn end player: %s %v spawn_spot: %v\n", player.netname, player.origin, spawn_spot.origin);
214 }
215
216 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
217 {
218         entity frag_target = M_ARGV(0, entity);
219
220         frag_target.msnt_deathloc = frag_target.origin;
221 }
222
223 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");