]> 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 terencehill/hud_updates
[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_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
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         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
62         FOREACH_CLIENT(IS_PLAYER(it),
63         {
64                 switch(it.team)
65                 {
66                         case NUM_TEAM_1: ++num_red; break;
67                         case NUM_TEAM_2: ++num_blue; break;
68                         case NUM_TEAM_3: ++num_yellow; break;
69                         case NUM_TEAM_4: ++num_pink; break;
70                 }
71         });
72
73         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
74                 return; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
75
76         // Note: when entering this, fixangle is already set.
77         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && player.cvar_cl_spawn_near_teammate))
78         {
79                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
80                         player.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
81
82                 entity best_mate = NULL;
83                 vector best_spot = '0 0 0';
84                 float pc = 0, best_dist = 0, dist = 0;
85                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
86                         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)
87                         if(!IS_DEAD(it))
88                         if(it.msnt_timer < time)
89                         if(SAME_TEAM(player, it))
90                         if(time > it.spawnshieldtime) // spawn shielding
91                         if(!forbidWeaponUse(it))
92                         if(STAT(FROZEN, it) == 0)
93                         if(it != player)
94                         {
95                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - '0 0 100', MOVE_NOMONSTERS, it);
96                                 if(trace_fraction != 1.0)
97                                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
98                                 {
99                                         pc = pointcontents(trace_endpos + '0 0 1');
100                                         if(pc == CONTENT_EMPTY)
101                                         {
102                                                 if(vdist(it.velocity, >, 5))
103                                                         fixedmakevectors(vectoangles(it.velocity));
104                                                 else
105                                                         fixedmakevectors(it.angles);
106
107                                                 for(pc = 0; pc < 4; ++pc) // test 4 diffrent spots close to mate
108                                                 {
109                                                         switch(pc)
110                                                         {
111                                                                 case 0:
112                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128, MOVE_NOMONSTERS, it);
113                                                                         break;
114                                                                 case 1:
115                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 , MOVE_NOMONSTERS, it);
116                                                                         break;
117                                                                 case 2:
118                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
119                                                                         break;
120                                                                 case 3:
121                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
122                                                                         break;
123                                                                 //case 4:
124                                                                         //tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_forward * 128, MOVE_NOMONSTERS, it);
125                                                                         //break;
126                                                         }
127
128                                                         if(trace_fraction == 1.0)
129                                                         {
130                                                                 traceline(trace_endpos + '0 0 4', trace_endpos - '0 0 100', MOVE_NOMONSTERS, it);
131                                                                 if(trace_fraction != 1.0)
132                                                                 {
133                                                                         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
134                                                                         {
135                                                                                 dist = vlen(trace_endpos - player.msnt_deathloc);
136                                                                                 if(dist < best_dist || best_dist == 0)
137                                                                                 {
138                                                                                         best_dist = dist;
139                                                                                         best_spot = trace_endpos;
140                                                                                         best_mate = it;
141                                                                                 }
142                                                                         }
143                                                                         else
144                                                                         {
145                                                                                 setorigin(player, trace_endpos);
146                                                                                 player.angles = it.angles;
147                                                                                 player.angles_z = 0; // never spawn tilted even if the spot says to
148                                                                                 it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
149                                                                                 return;
150                                                                         }
151                                                                 }
152                                                         }
153                                                 }
154                                         }
155                                 }
156                         }
157                 ));
158
159                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
160                 if(best_dist)
161                 {
162                         setorigin(player, best_spot);
163                         player.angles = best_mate.angles;
164                         player.angles_z = 0; // never spawn tilted even if the spot says to
165                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
166                 }
167         }
168         else if(spawn_spot.msnt_lookat)
169         {
170                 player.angles = vectoangles(spawn_spot.msnt_lookat.origin - player.origin);
171                 player.angles_x = -player.angles.x;
172                 player.angles_z = 0; // never spawn tilted even if the spot says to
173                 /*
174                 sprint(player, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
175                 sprint(player, "distance: ", vtos(spawn_spot.msnt_lookat.origin - player.origin), "\n");
176                 sprint(player, "angles: ", vtos(player.angles), "\n");
177                 */
178         }
179 }
180
181 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
182 {
183         entity frag_target = M_ARGV(0, entity);
184
185         frag_target.msnt_deathloc = frag_target.origin;
186 }
187
188 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");