Merge branch 'master' into Mario/vaporizer_damage
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / mutators / mutator_spawn_near_teammate.qc
1 #include "../_all.qh"
2
3 #include "mutator.qh"
4
5 .entity msnt_lookat;
6
7 .float msnt_timer;
8 .vector msnt_deathloc;
9
10 .float cvar_cl_spawn_near_teammate;
11
12 MUTATOR_HOOKFUNCTION(msnt_Spawn_Score)
13 {
14         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && self.cvar_cl_spawn_near_teammate))
15                 return 0;
16
17         entity p;
18
19         spawn_spot.msnt_lookat = world;
20
21         if(!teamplay)
22                 return 0;
23
24         RandomSelection_Init();
25         FOR_EACH_PLAYER(p) if(p != self) if(p.team == self.team) if(!p.deadflag)
26         {
27                 float l = vlen(spawn_spot.origin - p.origin);
28                 if(l > autocvar_g_spawn_near_teammate_distance)
29                         continue;
30                 if(l < 48)
31                         continue;
32                 if(!checkpvs(spawn_spot.origin, p))
33                         continue;
34                 RandomSelection_Add(p, 0, string_null, 1, 1);
35         }
36
37         if(RandomSelection_chosen_ent)
38         {
39                 spawn_spot.msnt_lookat = RandomSelection_chosen_ent;
40                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_FOUND;
41         }
42         else if(self.team == spawn_spot.team)
43                 spawn_score.x += SPAWN_PRIO_NEAR_TEAMMATE_SAMETEAM; // prefer same team, if we can't find a spawn near teammate
44
45         return 0;
46 }
47
48 MUTATOR_HOOKFUNCTION(msnt_PlayerSpawn)
49 {
50         // Note: when entering this, fixangle is already set.
51         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint == 1 || (autocvar_g_spawn_near_teammate_ignore_spawnpoint == 2 && self.cvar_cl_spawn_near_teammate))
52         {
53                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death)
54                         self.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay_death;
55
56                 entity team_mate, best_mate = world;
57                 vector best_spot = '0 0 0';
58                 float pc = 0, best_dist = 0, dist = 0;
59                 FOR_EACH_PLAYER(team_mate)
60                 {
61                         if((autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health >= 0 && team_mate.health >= autocvar_g_balance_health_regenstable) || autocvar_g_spawn_near_teammate_ignore_spawnpoint_check_health == 0)
62                         if(team_mate.deadflag == DEAD_NO)
63                         if(team_mate.msnt_timer < time)
64                         if(SAME_TEAM(self, team_mate))
65                         if(time > team_mate.spawnshieldtime) // spawn shielding
66                         if(team_mate.frozen == 0)
67                         if(team_mate != self)
68                         {
69                                 tracebox(team_mate.origin, PL_MIN, PL_MAX, team_mate.origin - '0 0 100', MOVE_WORLDONLY, team_mate);
70                                 if(trace_fraction != 1.0)
71                                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_SKY))
72                                 {
73                                         pc = pointcontents(trace_endpos + '0 0 1');
74                                         if(pc == CONTENT_EMPTY)
75                                         {
76                                                 if(vlen(team_mate.velocity) > 5)
77                                                         fixedmakevectors(vectoangles(team_mate.velocity));
78                                                 else
79                                                         fixedmakevectors(team_mate.angles);
80
81                                                 for(pc = 0; pc != 5; ++pc) // test 5 diffrent spots close to mate
82                                                 {
83                                                         switch(pc)
84                                                         {
85                                                                 case 0:
86                                                                         tracebox(team_mate.origin , PL_MIN, PL_MAX, team_mate.origin + v_right * 128, MOVE_NORMAL, team_mate);
87                                                                         break;
88                                                                 case 1:
89                                                                         tracebox(team_mate.origin , PL_MIN, PL_MAX, team_mate.origin - v_right * 128 , MOVE_NORMAL, team_mate);
90                                                                         break;
91                                                                 case 2:
92                                                                         tracebox(team_mate.origin , PL_MIN, PL_MAX, team_mate.origin + v_right * 64 - v_forward * 64, MOVE_NORMAL, team_mate);
93                                                                         break;
94                                                                 case 3:
95                                                                         tracebox(team_mate.origin , PL_MIN, PL_MAX, team_mate.origin - v_right * 64 - v_forward * 64, MOVE_NORMAL, team_mate);
96                                                                         break;
97                                                                 case 4:
98                                                                         tracebox(team_mate.origin , PL_MIN, PL_MAX, team_mate.origin - v_forward * 128, MOVE_NORMAL, team_mate);
99                                                                         break;
100                                                         }
101
102                                                         if(trace_fraction == 1.0)
103                                                         {
104                                                                 traceline(trace_endpos + '0 0 4', trace_endpos - '0 0 100', MOVE_NORMAL, team_mate);
105                                                                 if(trace_fraction != 1.0)
106                                                                 {
107                                                                         if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
108                                                                         {
109                                                                                 dist = vlen(trace_endpos - self.msnt_deathloc);
110                                                                                 if(dist < best_dist || best_dist == 0)
111                                                                                 {
112                                                                                         best_dist = dist;
113                                                                                         best_spot = trace_endpos;
114                                                                                         best_mate = team_mate;
115                                                                                 }
116                                                                         }
117                                                                         else
118                                                                         {
119                                                                                 setorigin(self, trace_endpos);
120                                                                                 self.angles = team_mate.angles;
121                                                                                 self.angles_z = 0; // never spawn tilted even if the spot says to
122                                                                                 team_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
123                                                                                 return 0;
124                                                                         }
125                                                                 }
126                                                         }
127                                                 }
128                                         }
129                                 }
130                         }
131                 }
132
133                 if(autocvar_g_spawn_near_teammate_ignore_spawnpoint_closetodeath)
134                 if(best_dist)
135                 {
136                         setorigin(self, best_spot);
137                         self.angles = best_mate.angles;
138                         self.angles_z = 0; // never spawn tilted even if the spot says to
139                         best_mate.msnt_timer = time + autocvar_g_spawn_near_teammate_ignore_spawnpoint_delay;
140                 }
141         }
142         else if(spawn_spot.msnt_lookat)
143         {
144                 self.angles = vectoangles(spawn_spot.msnt_lookat.origin - self.origin);
145                 self.angles_x = -self.angles.x;
146                 self.angles_z = 0; // never spawn tilted even if the spot says to
147                 /*
148                 sprint(self, "You should be looking at ", spawn_spot.msnt_lookat.netname, "^7.\n");
149                 sprint(self, "distance: ", vtos(spawn_spot.msnt_lookat.origin - self.origin), "\n");
150                 sprint(self, "angles: ", vtos(self.angles), "\n");
151                 */
152         }
153
154         return 0;
155 }
156
157 MUTATOR_HOOKFUNCTION(msnt_PlayerDies)
158 {
159         self.msnt_deathloc = self.origin;
160         return 0;
161 }
162
163 MUTATOR_HOOKFUNCTION(msnt_GetCvars)
164 {
165         GetCvars_handleFloat(get_cvars_s, get_cvars_f, cvar_cl_spawn_near_teammate, "cl_spawn_near_teammate");
166         return false;
167 }
168
169 MUTATOR_DEFINITION(mutator_spawn_near_teammate)
170 {
171         MUTATOR_HOOK(Spawn_Score, msnt_Spawn_Score, CBC_ORDER_ANY);
172         MUTATOR_HOOK(PlayerSpawn, msnt_PlayerSpawn, CBC_ORDER_ANY);
173         MUTATOR_HOOK(PlayerDies, msnt_PlayerDies, CBC_ORDER_ANY);
174         MUTATOR_HOOK(GetCvars, msnt_GetCvars, CBC_ORDER_ANY);
175
176         return 0;
177 }