]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mutators/mutator/spawn_near_teammate/spawn_near_teammate.qc
Push the location further to the side when setting it a bit infront, hopefully fixes...
[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 {SELFPARAM();
21         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && self.cvar_cl_spawn_near_teammate))
22                 return 0;
23
24         spawn_spot.msnt_lookat = world;
25
26         if(!teamplay)
27                 return 0;
28
29         RandomSelection_Init();
30         FOREACH_CLIENT(IS_PLAYER(it) && it != self && SAME_TEAM(it, self) && !IS_DEAD(it), LAMBDA(
31                 if(vdist(spawn_spot.origin - it.origin, >, autocvar_g_spawn_near_teammate_distance))
32                         continue;
33                 if(vdist(spawn_spot.origin - it.origin, <, 48))
34                         continue;
35                 if(!checkpvs(spawn_spot.origin, it))
36                         continue;
37                 RandomSelection_Add(it, 0, string_null, 1, 1);
38         ));
39
40         if(RandomSelection_chosen_ent)
41         {
42                 spawn_spot.msnt_lookat = RandomSelection_chosen_ent;
43                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
44         }
45         else if(self.team == spawn_spot.team)
46                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
47
48         return 0;
49 }
50
51 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerSpawn)
52 {SELFPARAM();
53         if(!teamplay) { return false; }
54
55         int num_red = 0, num_blue = 0, num_yellow = 0, num_pink = 0;
56         FOREACH_CLIENT(IS_PLAYER(it),
57         {
58                 switch(it.team)
59                 {
60                         case NUM_TEAM_1: ++num_red; break;
61                         case NUM_TEAM_2: ++num_blue; break;
62                         case NUM_TEAM_3: ++num_yellow; break;
63                         case NUM_TEAM_4: ++num_pink; break;
64                 }
65         });
66
67         if(num_red == 1 || num_blue == 1 || num_yellow == 1 || num_pink == 1)
68                 return false; // at least 1 team has only 1 player, let's not give the bigger team too much of an advantage!
69
70         // Note: when entering this, fixangle is already set.
71         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && self.cvar_cl_spawn_near_teammate))
72         {
73                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
74                         self.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
75
76                 entity best_mate = world;
77                 vector best_spot = '0 0 0';
78                 float pc = 0, best_dist = 0, dist = 0;
79                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
80                         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)
81                         if(!IS_DEAD(it))
82                         if(it.msnt_timer < time)
83                         if(SAME_TEAM(self, it))
84                         if(time > it.spawnshieldtime) // spawn shielding
85                         if(STAT(FROZEN, it) == 0)
86                         if(it != self)
87                         {
88                                 tracebox(it.origin, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - '0 0 100', MOVE_NOMONSTERS, it);
89                                 if(trace_fraction != 1.0)
90                                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
91                                 {
92                                         pc = pointcontents(trace_endpos + '0 0 1');
93                                         if(pc == CONTENT_EMPTY)
94                                         {
95                                                 if(vdist(it.velocity, >, 5))
96                                                         fixedmakevectors(vectoangles(it.velocity));
97                                                 else
98                                                         fixedmakevectors(it.angles);
99
100                                                 for(pc = 0; pc < 4; ++pc) // test 4 diffrent spots close to mate
101                                                 {
102                                                         switch(pc)
103                                                         {
104                                                                 case 0:
105                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128, MOVE_NOMONSTERS, it);
106                                                                         break;
107                                                                 case 1:
108                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 , MOVE_NOMONSTERS, it);
109                                                                         break;
110                                                                 case 2:
111                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin + v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
112                                                                         break;
113                                                                 case 3:
114                                                                         tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_right * 128 - v_forward * 64, MOVE_NOMONSTERS, it);
115                                                                         break;
116                                                                 //case 4:
117                                                                         //tracebox(it.origin , STAT(PL_MIN, NULL), STAT(PL_MAX, NULL), it.origin - v_forward * 128, MOVE_NOMONSTERS, it);
118                                                                         //break;
119                                                         }
120
121                                                         if(trace_fraction == 1.0)
122                                                         {
123                                                                 traceline(trace_endpos + '0 0 4', trace_endpos - '0 0 100', MOVE_NOMONSTERS, it);
124                                                                 if(trace_fraction != 1.0)
125                                                                 {
126                                                                         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
127                                                                         {
128                                                                                 dist = vlen(trace_endpos - self.msnt_deathloc);
129                                                                                 if(dist < best_dist || best_dist == 0)
130                                                                                 {
131                                                                                         best_dist = dist;
132                                                                                         best_spot = trace_endpos;
133                                                                                         best_mate = it;
134                                                                                 }
135                                                                         }
136                                                                         else
137                                                                         {
138                                                                                 setorigin(self, trace_endpos);
139                                                                                 self.angles = it.angles;
140                                                                                 self.angles_z = 0; // never spawn tilted even if the spot says to
141                                                                                 it.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
142                                                                                 return 0;
143                                                                         }
144                                                                 }
145                                                         }
146                                                 }
147                                         }
148                                 }
149                         }
150                 ));
151
152                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
153                 if(best_dist)
154                 {
155                         setorigin(self, best_spot);
156                         self.angles = best_mate.angles;
157                         self.angles_z = 0; // never spawn tilted even if the spot says to
158                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
159                 }
160         }
161         else if(spawn_spot.msnt_lookat)
162         {
163                 self.angles = vectoangles(spawn_spot.msnt_lookat.origin - self.origin);
164                 self.angles_x = -self.angles.x;
165                 self.angles_z = 0; // never spawn tilted even if the spot says to
166                 /*
167                 sprint(self, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
168                 sprint(self, "distance: ", vtos(spawn_spot.msnt_lookat.origin - self.origin), "\n");
169                 sprint(self, "angles: ", vtos(self.angles), "\n");
170                 */
171         }
172
173         return 0;
174 }
175
176 MUTATOR_HOOKFUNCTION(spawn_near_teammate, PlayerDies)
177 {
178         frag_target.msnt_deathloc = frag_target.origin;
179         return 0;
180 }
181
182 REPLICATE(cvar_cl_spawn_near_teammate, bool, "cl_spawn_near_teammate");
183
184 #endif