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