]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/teleporters.qc
Makefile: use `-I.`
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / teleporters.qc
1 #include "teleporters.qh"
2
3 #if defined(CSQC)
4 #elif defined(MENUQC)
5 #elif defined(SVQC)
6     #include <lib/warpzone/common.qh>
7     #include <lib/warpzone/util_server.qh>
8     #include <lib/warpzone/server.qh>
9     #include "../constants.qh"
10         #include "../triggers/subs.qh"
11     #include "../util.qh"
12     #include <server/weapons/csqcprojectile.qh>
13     #include <server/autocvars.qh>
14     #include <server/constants.qh>
15     #include <server/defs.qh>
16     #include "../deathtypes/all.qh"
17     #include "../turrets/sv_turrets.qh"
18     #include "../vehicles/all.qh"
19     #include "../mapinfo.qh"
20     #include <server/anticheat.qh>
21 #endif
22
23 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
24 {
25         if (IS_PLAYER(player) && !PHYS_DEAD(player))
26         {
27                 TDEATHLOOP(org)
28                 {
29                 #ifdef SVQC
30                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
31                 #endif
32                                 if(IS_PLAYER(head))
33                                         if(!PHYS_DEAD(head))
34                                                 return 1;
35                 }
36         }
37         return 0;
38 }
39
40 #ifdef SVQC
41
42 void trigger_teleport_link(entity this);
43
44 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
45 {
46         TDEATHLOOP(player.origin)
47         {
48                 if (IS_PLAYER(player) && player.health >= 1)
49                 {
50                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
51                         {
52                                 if(IS_PLAYER(head))
53                                         if(head.health >= 1)
54                                                 ++tdeath_hit;
55                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, head.origin, '0 0 0');
56                         }
57                 }
58                 else // dead bodies and monsters gib themselves instead of telefragging
59                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, telefragger.origin, '0 0 0');
60         }
61 }
62
63 void spawn_tdeath(vector v0, entity e, vector v)
64 {
65         tdeath(e, e, e, '0 0 0', '0 0 0');
66 }
67
68 #endif
69
70 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
71 {SELFPARAM();
72         entity telefragger;
73         vector from;
74
75         if(teleporter.owner)
76                 telefragger = teleporter.owner;
77         else
78                 telefragger = player;
79
80         makevectors (to_angles);
81
82 #ifdef SVQC
83         if(player.teleportable == TELEPORT_NORMAL) // don't play sounds or show particles for anything that isn't a player, maybe change later to block only observers
84         {
85                 if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
86                 {
87                         if(tflags & TELEPORT_FLAG_SOUND)
88                                 sound (player, CH_TRIGGER, SND_TELEPORT, VOL_BASE, ATTEN_NORM);
89                         if(tflags & TELEPORT_FLAG_PARTICLES)
90                         {
91                                 Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
92                                 Send_Effect(EFFECT_TELEPORT, to + v_forward * 32, '0 0 0', 1);
93                         }
94                         self.pushltime = time + 0.2;
95                 }
96         }
97 #endif
98
99         // Relocate the player
100         // assuming to allows PL_MIN to PL_MAX box and some more
101 #ifdef SVQC
102         from = player.origin;
103         setorigin (player, to);
104         player.oldorigin = to; // don't undo the teleport by unsticking
105         player.angles = to_angles;
106         player.fixangle = true;
107         player.velocity = to_velocity;
108         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
109
110         makevectors(player.angles);
111         Reset_ArcBeam(player, v_forward);
112         UpdateCSQCProjectileAfterTeleport(player);
113         UpdateItemAfterTeleport(player);
114 #elif defined(CSQC)
115         from = player.move_origin;
116         player.move_origin = to;
117         player.move_angles = to_angles;
118         player.move_velocity = to_velocity;
119         player.move_flags &= ~FL_ONGROUND;
120         player.iflags |= IFLAG_TELEPORTED | IFLAG_V_ANGLE | IFLAG_ANGLES;
121         player.csqcmodel_teleported = 1;
122         player.v_angle = to_angles;
123
124         if(player.isplayermodel) // not for anything but the main player
125         {
126                 setproperty(VF_ANGLES, player.move_angles);
127                 setproperty(VF_CL_VIEWANGLES, player.move_angles);
128         }
129
130         makevectors(player.move_angles);
131 #endif
132
133 #ifdef SVQC
134         if(IS_PLAYER(player))
135         {
136                 if(tflags & TELEPORT_FLAG_TDEATH)
137                         if(player.takedamage && player.deadflag == DEAD_NO && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH)))
138                                 tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
139
140                 // player no longer is on ground
141                 UNSET_ONGROUND(player);
142
143                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
144                 player.oldvelocity = player.velocity;
145
146                 // reset tracking of who pushed you into a hazard (for kill credit)
147                 if(teleporter.owner)
148                 {
149                         player.pusher = teleporter.owner;
150                         player.pushltime = time + autocvar_g_maxpushtime;
151                         player.istypefrag = player.BUTTON_CHAT;
152                 }
153                 else
154                 {
155                         player.pushltime = 0;
156                         player.istypefrag = 0;
157                 }
158
159                 player.lastteleporttime = time;
160         }
161 #endif
162 }
163
164 entity Simple_TeleportPlayer(entity teleporter, entity player)
165 {
166         vector locout;
167         entity e;
168         float p;
169
170         // Find the output teleporter
171         if(teleporter.enemy)
172         {
173                 e = teleporter.enemy;
174         }
175         else
176         {
177                 RandomSelection_Init();
178                 for(e = world; (e = find(e, targetname, teleporter.target)); )
179                 {
180                         p = 1;
181                         if(STAT(TELEPORT_TELEFRAG_AVOID, player))
182                         {
183                         #ifdef SVQC
184                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
185                         #elif defined(CSQC)
186                                 locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
187                         #endif
188                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
189                                         p = 0;
190                         }
191                         RandomSelection_Add(e, 0, string_null, (e.cnt ? e.cnt : 1), p);
192                 }
193                 e = RandomSelection_chosen_ent;
194         }
195
196 #ifdef SVQC
197         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
198 #elif defined(CSQC)
199         if(!e) { LOG_INFO("Teleport destination could not be found from CSQC.\n"); }
200 #endif
201
202         makevectors(e.mangle);
203
204 #ifdef SVQC
205         if(e.speed)
206                 if(vlen(player.velocity) > e.speed)
207                         player.velocity = normalize(player.velocity) * max(0, e.speed);
208 #elif defined(CSQC)
209         if(e.speed)
210                 if(vlen(player.move_velocity) > e.speed)
211                         player.move_velocity = normalize(player.move_velocity) * max(0, e.speed);
212 #endif
213
214 #ifdef SVQC
215         if(STAT(TELEPORT_MAXSPEED, player))
216                 if(vlen(player.velocity) > STAT(TELEPORT_MAXSPEED, player))
217                         player.velocity = normalize(player.velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
218 #elif defined(CSQC)
219         if(STAT(TELEPORT_MAXSPEED, player))
220                 if(vlen(player.move_velocity) > STAT(TELEPORT_MAXSPEED, player))
221                         player.move_velocity = normalize(player.move_velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
222 #endif
223
224 #ifdef SVQC
225         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
226
227         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
228 #elif defined(CSQC)
229         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
230
231         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.move_velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
232 #endif
233
234         return e;
235 }
236
237 void teleport_findtarget()
238 {
239         int n = 0;
240         entity e;
241         for(e = world; (e = find(e, targetname, self.target)); )
242         {
243                 ++n;
244 #ifdef SVQC
245                 if(e.movetype == MOVETYPE_NONE)
246                         waypoint_spawnforteleporter(self, e.origin, 0);
247                 if(e.classname != "info_teleport_destination")
248                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
249 #endif
250         }
251
252         if(n == 0)
253         {
254                 // no dest!
255                 objerror ("Teleporter with nonexistant target");
256                 return;
257         }
258         else if(n == 1)
259         {
260                 // exactly one dest - bots love that
261                 self.enemy = find(e, targetname, self.target);
262         }
263         else
264         {
265                 // have to use random selection every single time
266                 self.enemy = world;
267         }
268
269         // now enable touch
270 #ifdef SVQC
271         self.touch = Teleport_Touch;
272
273         trigger_teleport_link(self);
274 #elif defined(CSQC)
275         self.move_touch = Teleport_Touch;
276 #endif
277 }
278
279 entity Teleport_Find(vector mi, vector ma)
280 {
281         entity e;
282         for(e = world; (e = find(e, classname, "trigger_teleport")); )
283                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, world))
284                         return e;
285         return world;
286 }
287
288 void WarpZone_PostTeleportPlayer_Callback(entity pl)
289 {SELFPARAM();
290 #ifdef SVQC
291         makevectors(pl.angles);
292         Reset_ArcBeam(pl, v_forward);
293         UpdateCSQCProjectileAfterTeleport(pl);
294         UpdateItemAfterTeleport(pl);
295         {
296                 WITH(entity, self, pl, anticheat_fixangle());
297         }
298 #endif
299         // "disown" projectiles after teleport
300         if(pl.owner)
301         if(pl.owner == pl.realowner)
302         {
303         #ifdef SVQC
304                 if(!(pl.flags & FL_PROJECTILE))
305         #elif defined(CSQC)
306                 if(!(pl.move_flags & BIT(15))) // FL_PROJECTILE
307         #endif
308                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
309                 pl.owner = world;
310         }
311         if(IS_PLAYER(pl))
312         {
313                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
314         #ifdef SVQC
315                 pl.oldvelocity = pl.velocity;
316         #endif
317                 // reset teleport time tracking too (or multijump can cause insane speeds)
318                 pl.lastteleporttime = time;
319         }
320 }