]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/spawn_near_teammate.qc
Purge self from the damage/death mutator hooks
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mutators / mutator / spawn_near_teammate / spawn_near_teammate.qc
1 #ifdef IMPLEMENTATION
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 false;
27
28         spawn_spot.msnt_lookat = world;
29
30         if(!teamplay)
31                 return false;
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_Add(it, 0, string_null, 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         return false;
55 }
56
57 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
58 {
59         if(!teamplay) { return false; }
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 false; // 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 = world;
85                 vector best_spot = '0 0 0';
86                 float pc = 0, best_dist = 0, dist = 0;
87                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
88                         if((autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health >= 0 && it.health >= autocvar_g_balance_health_regenstable) || autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health == 0)
89                         if(!IS_DEAD(it))
90                         if(it.msnt_timer < time)
91                         if(SAME_TEAM(player, it))
92                         if(time > it.spawnshieldtime) // spawn shielding
93                         if(STAT(FROZEN, it) == 0)
94                         if(it != player)
95                         {
96                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - '0 0 100', MOVE_NOMONSTERS, it);
97                                 if(trace_fraction != 1.0)
98                                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
99                                 {
100                                         pc = pointcontents(trace_endpos + '0 0 1');
101                                         if(pc == CONTENT_EMPTY)
102                                         {
103                                                 if(vdist(it.velocity, >, 5))
104                                                         fixedmakevectors(vectoangles(it.velocity));
105                                                 else
106                                                         fixedmakevectors(it.angles);
107
108                                                 for(pc = 0; pc < 4; ++pc) // test 4 diffrent spots close to mate
109                                                 {
110                                                         switch(pc)
111                                                         {
112                                                                 case 0:
113                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128, MOVE_NOMONSTERS, it);
114                                                                         break;
115                                                                 case 1:
116                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 , MOVE_NOMONSTERS, it);
117                                                                         break;
118                                                                 case 2:
119                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
120                                                                         break;
121                                                                 case 3:
122                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
123                                                                         break;
124                                                                 //case 4:
125                                                                         //tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_forward * 128, MOVE_NOMONSTERS, it);
126                                                                         //break;
127                                                         }
128
129                                                         if(trace_fraction == 1.0)
130                                                         {
131                                                                 traceline(trace_endpos + '0 0 4', trace_endpos - '0 0 100', MOVE_NOMONSTERS, it);
132                                                                 if(trace_fraction != 1.0)
133                                                                 {
134                                                                         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
135                                                                         {
136                                                                                 dist = vlen(trace_endpos - player.msnt_deathloc);
137                                                                                 if(dist < best_dist || best_dist == 0)
138                                                                                 {
139                                                                                         best_dist = dist;
140                                                                                         best_spot = trace_endpos;
141                                                                                         best_mate = it;
142                                                                                 }
143                                                                         }
144                                                                         else
145                                                                         {
146                                                                                 setorigin(player, trace_endpos);
147                                                                                 player.angles = it.angles;
148                                                                                 player.angles_z = 0; // never spawn tilted even if the spot says to
149                                                                                 it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
150                                                                                 return false;
151                                                                         }
152                                                                 }
153                                                         }
154                                                 }
155                                         }
156                                 }
157                         }
158                 ));
159
160                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
161                 if(best_dist)
162                 {
163                         setorigin(player, best_spot);
164                         player.angles = best_mate.angles;
165                         player.angles_z = 0; // never spawn tilted even if the spot says to
166                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
167                 }
168         }
169         else if(spawn_spot.msnt_lookat)
170         {
171                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
172                 player.angles_x = -player.angles.x;
173                 player.angles_z = 0; // never spawn tilted even if the spot says to
174                 /*
175                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
176                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
177                 sprint(player, "angles: ", vtos(player.angles), "\n");
178                 */
179         }
180
181         return false;
182 }
183
184 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
185 {
186         entity frag_target = M_ARGV(0, entity);
187
188         frag_target.msnt_deathloc = frag_target.origin;
189         return false;
190 }
191
192 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");
193
194 #endif