Remove .move_* fields and MOVETYPE_PUSH logic (doesn't work)
[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) && !IS_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(!IS_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 {
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(teleporter.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
86                 {
87                         if(tflags & TELEPORT_FLAG_SOUND)
88                         {
89                                 string thesound = SND(TELEPORT);
90                                 if(teleporter.noise != "")
91                                 {
92                                         RandomSelection_Init();
93                                         FOREACH_WORD(teleporter.noise, true,
94                                         {
95                                                 RandomSelection_Add(NULL, 0, it, 1, 1);
96                                         });
97                                         thesound = RandomSelection_chosen_string;
98                                 }
99                                 _sound (player, CH_TRIGGER, thesound, VOL_BASE, ATTEN_NORM);
100                         }
101                         if(tflags & TELEPORT_FLAG_PARTICLES)
102                         {
103                                 Send_Effect(EFFECT_TELEPORT, player.origin, '0 0 0', 1);
104                                 Send_Effect(EFFECT_TELEPORT, to + v_forward * 32, '0 0 0', 1);
105                         }
106                         teleporter.pushltime = time + 0.2;
107                 }
108         }
109 #endif
110
111         // Relocate the player
112         // assuming to allows PL_MIN to PL_MAX box and some more
113 #ifdef SVQC
114         from = player.origin;
115         setorigin(player, to);
116         player.oldorigin = to; // don't undo the teleport by unsticking
117         player.angles = to_angles;
118         player.fixangle = true;
119         player.velocity = to_velocity;
120         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
121
122         makevectors(player.angles);
123         Reset_ArcBeam(player, v_forward);
124         UpdateCSQCProjectileAfterTeleport(player);
125         UpdateItemAfterTeleport(player);
126 #elif defined(CSQC)
127         from = player.origin;
128         setorigin(player, to);
129         player.angles = to_angles;
130         player.velocity = to_velocity;
131         player.flags &= ~FL_ONGROUND;
132         player.iflags |= IFLAG_TELEPORTED | IFLAG_V_ANGLE | IFLAG_ANGLES;
133         player.csqcmodel_teleported = 1;
134         player.v_angle = to_angles;
135
136         if(player == csqcplayer) // not for anything but the main player
137         {
138                 setproperty(VF_ANGLES, player.angles);
139                 setproperty(VF_CL_VIEWANGLES, player.angles);
140         }
141 #endif
142
143 #ifdef SVQC
144         if(IS_PLAYER(player))
145         {
146                 if(tflags & TELEPORT_FLAG_TDEATH)
147                         if(player.takedamage && !IS_DEAD(player) && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH)))
148                                 tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
149
150                 // player no longer is on ground
151                 UNSET_ONGROUND(player);
152
153                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
154                 player.oldvelocity = player.velocity;
155
156                 // reset tracking of who pushed you into a hazard (for kill credit)
157                 if(teleporter.owner)
158                 {
159                         player.pusher = teleporter.owner;
160                         player.pushltime = time + autocvar_g_maxpushtime;
161                         player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
162                 }
163                 else
164                 {
165                         player.pushltime = 0;
166                         player.istypefrag = 0;
167                 }
168
169                 player.lastteleporttime = time;
170         }
171 #endif
172 }
173
174 entity Simple_TeleportPlayer(entity teleporter, entity player)
175 {
176         vector locout;
177         entity e;
178         float p;
179
180         // Find the output teleporter
181         if(teleporter.enemy)
182         {
183                 e = teleporter.enemy;
184         }
185         else
186         {
187                 RandomSelection_Init();
188                 FOREACH_ENTITY_STRING(targetname, teleporter.target,
189                 {
190                         p = 1;
191                         if(STAT(TELEPORT_TELEFRAG_AVOID, player))
192                         {
193                         #ifdef SVQC
194                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
195                         #elif defined(CSQC)
196                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
197                         #endif
198                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
199                                         p = 0;
200                         }
201                         RandomSelection_Add(it, 0, string_null, (it.cnt ? it.cnt : 1), p);
202                 });
203                 e = RandomSelection_chosen_ent;
204         }
205
206 #ifdef SVQC
207         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
208 #elif defined(CSQC)
209         if(!e) { LOG_INFO("Teleport destination could not be found from CSQC.\n"); }
210 #endif
211
212         makevectors(e.mangle);
213
214         if(e.speed)
215                 if(vdist(player.velocity, >, e.speed))
216                         player.velocity = normalize(player.velocity) * max(0, e.speed);
217
218         if(STAT(TELEPORT_MAXSPEED, player))
219                 if(vdist(player.velocity, >, STAT(TELEPORT_MAXSPEED, player)))
220                         player.velocity = normalize(player.velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
221
222         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
223
224         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
225
226         return e;
227 }
228
229 void teleport_findtarget(entity this)
230 {
231         int n = 0;
232         entity e;
233         for(e = NULL; (e = find(e, targetname, this.target)); )
234         {
235                 ++n;
236 #ifdef SVQC
237                 if(e.movetype == MOVETYPE_NONE)
238                         waypoint_spawnforteleporter(this, e.origin, 0);
239                 if(e.classname != "info_teleport_destination")
240                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
241 #endif
242         }
243
244         if(n == 0)
245         {
246                 // no dest!
247                 objerror (this, "Teleporter with nonexistant target");
248                 return;
249         }
250         else if(n == 1)
251         {
252                 // exactly one dest - bots love that
253                 this.enemy = find(e, targetname, this.target);
254         }
255         else
256         {
257                 // have to use random selection every single time
258                 this.enemy = NULL;
259         }
260
261         // now enable touch
262         settouch(this, Teleport_Touch);
263 #ifdef SVQC
264         trigger_teleport_link(this);
265 #endif
266 }
267
268 entity Teleport_Find(vector mi, vector ma)
269 {
270         entity e;
271         for(e = NULL; (e = find(e, classname, "trigger_teleport")); )
272                 if(WarpZoneLib_BoxTouchesBrush(mi, ma, e, NULL))
273                         return e;
274         return NULL;
275 }
276
277 void WarpZone_PostTeleportPlayer_Callback(entity pl)
278 {
279 #ifdef SVQC
280         makevectors(pl.angles);
281         Reset_ArcBeam(pl, v_forward);
282         UpdateCSQCProjectileAfterTeleport(pl);
283         UpdateItemAfterTeleport(pl);
284     if (IS_PLAYER(pl)) anticheat_fixangle(pl);
285 #endif
286         // "disown" projectiles after teleport
287         if(pl.owner)
288         if(pl.owner == pl.realowner)
289         {
290         #ifdef SVQC
291                 if(!(pl.flags & FL_PROJECTILE))
292         #elif defined(CSQC)
293                 if(!(pl.flags & BIT(15))) // FL_PROJECTILE
294         #endif
295                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".\n");
296                 pl.owner = NULL;
297         }
298         if(IS_PLAYER(pl))
299         {
300                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
301         #ifdef SVQC
302                 pl.oldvelocity = pl.velocity;
303         #endif
304                 // reset teleport time tracking too (or multijump can cause insane speeds)
305                 pl.lastteleporttime = time;
306         }
307 }