]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/teleporters.qc
Use readable labels for q3compat bits
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / 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 "../mapobjects/subs.qh"
11     #include "../util.qh"
12     #include <server/weapons/csqcprojectile.qh>
13     #include <server/autocvars.qh>
14     #include <common/weapons/_all.qh>
15     #include <common/stats.qh>
16     #include "../deathtypes/all.qh"
17     #include <server/main.qh>
18     #include "../turrets/sv_turrets.qh"
19     #include "../vehicles/all.qh"
20     #include <common/gamemodes/_mod.qh>
21     #include <server/anticheat.qh>
22 #endif
23
24 #ifdef SVQC
25 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
26 {
27         if (IS_PLAYER(player) && !IS_DEAD(player))
28         {
29                 TDEATHLOOP(org)
30                 {
31                 #ifdef SVQC
32                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
33                 #endif
34                                 if(IS_PLAYER(head))
35                                         if(!IS_DEAD(head))
36                                                 return 1;
37                 }
38         }
39         return 0;
40 }
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) && GetResource(player, RES_HEALTH) >= 1)
49                 {
50                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
51                         {
52                                 if(IS_PLAYER(head))
53                                         if(GetResource(head, RES_HEALTH) >= 1)
54                                                 ++tdeath_hit;
55                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, DMG_NOWEP, 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, DMG_NOWEP, 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 #endif
68
69 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax, float tflags)
70 {
71         entity telefragger;
72         vector from;
73
74         if(teleporter.owner)
75                 telefragger = teleporter.owner;
76         else
77                 telefragger = player;
78
79         makevectors (to_angles);
80
81 #ifdef SVQC
82         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
83         {
84                 if(teleporter.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
85                 {
86                         if(tflags & TELEPORT_FLAG_SOUND)
87                         {
88                                 string thesound = SND(TELEPORT);
89                                 if(teleporter.noise != "")
90                                 {
91                                         RandomSelection_Init();
92                                         FOREACH_WORD(teleporter.noise, true,
93                                         {
94                                                 RandomSelection_AddString(it, 1, 1);
95                                         });
96                                         thesound = RandomSelection_chosen_string;
97                                 }
98                                 _sound (player, CH_TRIGGER, thesound, VOL_BASE, ATTEN_NORM);
99                         }
100                         if(tflags & TELEPORT_FLAG_PARTICLES)
101                         {
102                                 Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
103                                 Send_Effect(EFFECT_TELEPORT, to + v_forward * 32, '0 0 0', 1);
104                         }
105                         teleporter.pushltime = time + 0.2;
106                 }
107         }
108 #endif
109
110         // Relocate the player
111         // assuming to allows PL_MIN to PL_MAX box and some more
112 #ifdef SVQC
113         from = player.origin;
114         setorigin(player, to);
115         player.oldorigin = to; // don't undo the teleport by unsticking
116         player.angles = to_angles;
117         if (IS_BOT_CLIENT(player))
118         {
119                 player.v_angle = player.angles;
120                 bot_aim_reset(player);
121         }
122         player.fixangle = true;
123         player.velocity = to_velocity;
124         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
125
126         makevectors(player.angles);
127         Reset_ArcBeam(player, v_forward);
128         UpdateCSQCProjectileAfterTeleport(player);
129         UpdateItemAfterTeleport(player);
130 #elif defined(CSQC)
131         from = player.origin;
132         setorigin(player, to);
133         player.angles = to_angles;
134         player.velocity = to_velocity;
135         UNSET_ONGROUND(player);
136         player.iflags |= IFLAG_TELEPORTED | IFLAG_V_ANGLE | IFLAG_ANGLES;
137         player.csqcmodel_teleported = 1;
138         player.v_angle = to_angles;
139
140         if(player == csqcplayer) // not for anything but the main player
141         {
142                 setproperty(VF_ANGLES, player.angles);
143                 setproperty(VF_CL_VIEWANGLES, player.angles);
144         }
145 #endif
146
147 #ifdef SVQC
148         if(IS_PLAYER(player))
149         {
150                 if((tflags & TELEPORT_FLAG_TDEATH) && player.takedamage && !IS_DEAD(player)
151                         && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH))
152                         && !(round_handler_IsActive() && !round_handler_IsRoundStarted()))
153                 {
154                         tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
155                 }
156
157                 // player no longer is on ground
158                 UNSET_ONGROUND(player);
159
160                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
161                 player.oldvelocity = player.velocity;
162
163                 // reset tracking of who pushed you into a hazard (for kill credit)
164                 if(teleporter.owner)
165                 {
166                         player.pusher = teleporter.owner;
167                         player.pushltime = time + autocvar_g_maxpushtime;
168                         player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
169                 }
170                 else
171                 {
172                         player.pushltime = 0;
173                         player.istypefrag = 0;
174                 }
175
176                 player.lastteleporttime = time;
177                 player.lastteleport_origin = from;
178         }
179 #endif
180 }
181
182 entity Simple_TeleportPlayer(entity teleporter, entity player)
183 {
184         vector locout;
185         entity e = NULL;
186
187         // Find the output teleporter
188         if(teleporter.enemy)
189         {
190                 e = teleporter.enemy;
191         }
192         else
193         {
194                 // sorry CSQC, random stuff ain't gonna happen
195 #ifdef SVQC
196                 RandomSelection_Init();
197                 FOREACH_ENTITY_STRING(targetname, teleporter.target,
198                 {
199                         bool p = true;
200                         if(STAT(TELEPORT_TELEFRAG_AVOID, player))
201                         {
202                         #ifdef SVQC
203                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
204                         #elif defined(CSQC)
205                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
206                         #endif
207                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
208                                         p = false;
209                         }
210                         RandomSelection_AddEnt(it, (it.cnt ? it.cnt : 1), p);
211                 });
212                 e = RandomSelection_chosen_ent;
213 #endif
214         }
215
216 #ifdef SVQC
217         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
218 #elif defined(CSQC)
219         if(!e) { LOG_INFO("Teleport destination could not be found from CSQC."); }
220 #endif
221
222         makevectors(e.mangle);
223
224         if(e.speed)
225                 if(vdist(player.velocity, >, e.speed))
226                         player.velocity = normalize(player.velocity) * max(0, e.speed);
227
228         if(STAT(TELEPORT_MAXSPEED, player))
229                 if(vdist(player.velocity, >, STAT(TELEPORT_MAXSPEED, player)))
230                         player.velocity = normalize(player.velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
231
232         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
233
234         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
235
236         return e;
237 }
238
239 void teleport_findtarget(entity this)
240 {
241         bool istrigger = (this.solid == SOLID_TRIGGER);
242
243         int n = 0;
244         for(entity e = NULL; (e = find(e, targetname, this.target)); )
245         {
246                 ++n;
247 #ifdef SVQC
248                 if(e.move_movetype == MOVETYPE_NONE)
249                 {
250                         entity tracetest_ent = spawn();
251                         setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
252                         tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
253                         waypoint_spawnforteleporter(this, e.origin, 0, tracetest_ent);
254                         delete(tracetest_ent);
255                 }
256                 if(e.classname != "info_teleport_destination")
257                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.");
258 #endif
259         }
260
261         if(n == 0)
262         {
263                 // no dest!
264                 objerror (this, "Teleporter with nonexistent target");
265                 return;
266         }
267         else if(n == 1)
268         {
269                 // exactly one dest - bots love that
270                 this.enemy = find(NULL, targetname, this.target);
271         }
272         else
273         {
274                 // have to use random selection every single time
275                 this.enemy = NULL;
276         }
277
278         // now enable touch
279         if(istrigger)
280                 settouch(this, Teleport_Touch);
281 #ifdef SVQC
282         if(istrigger)
283                 trigger_teleport_link(this);
284 #endif
285 }
286
287 entity Teleport_Find(vector mi, vector ma)
288 {
289         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(mi, ma, it, NULL),
290         {
291                 return it;
292         });
293         return NULL;
294 }
295
296 void WarpZone_PostTeleportPlayer_Callback(entity pl)
297 {
298 #ifdef SVQC
299         makevectors(pl.angles);
300         Reset_ArcBeam(pl, v_forward);
301         UpdateCSQCProjectileAfterTeleport(pl);
302         UpdateItemAfterTeleport(pl);
303     if (IS_PLAYER(pl)) anticheat_fixangle(pl);
304 #endif
305         // "disown" projectiles after teleport
306         if(pl.owner)
307         if(pl.owner == pl.realowner)
308         {
309         #ifdef SVQC
310                 if(!(pl.flags & FL_PROJECTILE))
311         #elif defined(CSQC)
312                 if(!(pl.flags & BIT(15))) // FL_PROJECTILE
313         #endif
314                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".");
315                 pl.owner = NULL;
316         }
317         if(IS_PLAYER(pl))
318         {
319                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
320         #ifdef SVQC
321                 pl.oldvelocity = pl.velocity;
322         #endif
323         }
324 }