]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/sv_spawn_near_teammate.qc
msnt: don't check for ground under teammate
[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; }
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 pc = 0, best_dist = 0, 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                         // TODO all tracing: proper dphitcontentsmask???
104                         // TODO check for nades
105
106                         pc = pointcontents(trace_endpos + '0 0 1');
107                         LOG_INFOF("    pointcontents %f\n", pc);
108                         if(pc == CONTENT_EMPTY)
109                         {
110                                 // TODO ignore vertical velocity
111                                 // TODO higher limit - don't spawn in front of player when strafing slowly - or detect where he's looking?
112
113                                 if(vdist(it.velocity, >, 5)) {
114                                         fixedmakevectors(vectoangles(vec2(it.velocity)));
115                                         LOG_INFOF("    using velocity\n");
116                                 }
117                                 else
118                                         fixedmakevectors(it.angles);
119
120                                 // TODO go up - fix uneven surfaces
121                                 for(pc = 0; pc < 4; ++pc) // test 4 diffrent spots close to mate
122                                 {
123                                         switch(pc)
124                                         {
125                                                 case 0:
126                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128, MOVE_NOMONSTERS, it);
127                                                         break;
128                                                 case 1:
129                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 , MOVE_NOMONSTERS, it);
130                                                         break;
131                                                 case 2:
132                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
133                                                         break;
134                                                 case 3:
135                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
136                                                         break;
137                                                 //case 4:
138                                                         //tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_forward * 128, MOVE_NOMONSTERS, it);
139                                                         //break;
140                                         }
141                                         vector prev_endpos = trace_endpos;
142
143                                         te_lightning1(NULL, it.origin, trace_endpos);
144                                         LOG_INFOF("    pc: %d trace_fraction %f\n", pc, trace_fraction);
145
146                                         if(trace_fraction == 1.0)
147                                         {
148                                                 traceline(trace_endpos + '0 0 4', trace_endpos - '0 0 100', MOVE_NOMONSTERS, it); // TODO tracebox to make sure enough space
149                                                 te_lightning2(NULL, prev_endpos, trace_endpos);
150                                                 LOG_INFOF("      trace_fraction2 %f\n", trace_fraction);
151
152                                                 if(trace_fraction != 1.0) // TODO not skybox or hurttriggers
153                                                 {
154                                                         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
155                                                         {
156                                                                 dist = vlen(trace_endpos - player.msnt_deathloc);
157                                                                 LOG_INFOF("      dist: %f, best_dist %f\n", dist, best_dist);
158                                                                 if(dist < best_dist || best_dist == 0)
159                                                                 {
160                                                                         LOG_INFOF("      new best dist - pos: %v\n", trace_endpos);
161                                                                         best_dist = dist;
162                                                                         best_pos = trace_endpos;
163                                                                         best_mate = it;
164                                                                 }
165                                                         }
166                                                         else // TODO random to avoid favoring players who joined early
167                                                         {
168                                                                 setorigin(player, trace_endpos);
169                                                                 player.angles = it.angles;
170                                                                 player.angles_z = 0; // never spawn tilted even if the spot says to
171                                                                 it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
172                                                                 LOG_INFOF("      PlayerSpawn return %v\n", player.origin);
173                                                                 if (player.origin != trace_endpos) LOG_WARNF("wrong origin\n");
174                                                                 return;
175                                                         }
176                                                 }
177                                         }
178                                 }
179                         }
180                 ));
181
182                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
183                 if(best_dist)
184                 {
185                         tracebox(best_pos, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), best_pos + '0 0 1', MOVE_NOMONSTERS, player);
186                         if (trace_startsolid || trace_fraction != 1.0) LOG_WARNF("bad spawn? %d %f", trace_startsolid, trace_fraction);
187                         setorigin(player, best_pos);
188                         LOG_INFOF("PlayerSpawn best_dist: pos: %v\n", best_pos);
189                         if (player.origin != best_pos) LOG_WARNF("wrong origin: %v", player.origin);
190
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         LOG_INFOF("PlayerSpawn end player: %s %v spawn_spot: %v\n", player.netname, player.origin, spawn_spot.origin);
209 }
210
211 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
212 {
213         entity frag_target = M_ARGV(0, entity);
214
215         frag_target.msnt_deathloc = frag_target.origin;
216 }
217
218 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");