]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/teleporters.qc
Remove the g_ prefix from some server code files and rename sv_main to main
[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 <server/constants.qh>
15     #include <common/weapons/_all.qh>
16     #include <common/stats.qh>
17     #include "../deathtypes/all.qh"
18     #include <server/main.qh>
19     #include "../turrets/sv_turrets.qh"
20     #include "../vehicles/all.qh"
21     #include <common/gamemodes/_mod.qh>
22     #include <server/anticheat.qh>
23 #endif
24
25 #ifdef SVQC
26 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
27 {
28         if (IS_PLAYER(player) && !IS_DEAD(player))
29         {
30                 TDEATHLOOP(org)
31                 {
32                 #ifdef SVQC
33                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
34                 #endif
35                                 if(IS_PLAYER(head))
36                                         if(!IS_DEAD(head))
37                                                 return 1;
38                 }
39         }
40         return 0;
41 }
42
43 void trigger_teleport_link(entity this);
44
45 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
46 {
47         TDEATHLOOP(player.origin)
48         {
49                 if (IS_PLAYER(player) && GetResource(player, RES_HEALTH) >= 1)
50                 {
51                         if (!(teamplay && autocvar_g_telefrags_teamplay && head.team == player.team))
52                         {
53                                 if(IS_PLAYER(head))
54                                         if(GetResource(head, RES_HEALTH) >= 1)
55                                                 ++tdeath_hit;
56                                 Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, DMG_NOWEP, head.origin, '0 0 0');
57                         }
58                 }
59                 else // dead bodies and monsters gib themselves instead of telefragging
60                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG.m_id, DMG_NOWEP, telefragger.origin, '0 0 0');
61         }
62 }
63
64 void spawn_tdeath(vector v0, entity e, vector v)
65 {
66         tdeath(e, e, e, '0 0 0', '0 0 0');
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_AddString(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         if (IS_BOT_CLIENT(player))
119         {
120                 player.v_angle = player.angles;
121                 bot_aim_reset(player);
122         }
123         player.fixangle = true;
124         player.velocity = to_velocity;
125         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
126
127         makevectors(player.angles);
128         Reset_ArcBeam(player, v_forward);
129         UpdateCSQCProjectileAfterTeleport(player);
130         UpdateItemAfterTeleport(player);
131 #elif defined(CSQC)
132         from = player.origin;
133         setorigin(player, to);
134         player.angles = to_angles;
135         player.velocity = to_velocity;
136         UNSET_ONGROUND(player);
137         player.iflags |= IFLAG_TELEPORTED | IFLAG_V_ANGLE | IFLAG_ANGLES;
138         player.csqcmodel_teleported = 1;
139         player.v_angle = to_angles;
140
141         if(player == csqcplayer) // not for anything but the main player
142         {
143                 setproperty(VF_ANGLES, player.angles);
144                 setproperty(VF_CL_VIEWANGLES, player.angles);
145         }
146 #endif
147
148 #ifdef SVQC
149         if(IS_PLAYER(player))
150         {
151                 if((tflags & TELEPORT_FLAG_TDEATH) && player.takedamage && !IS_DEAD(player)
152                         && !g_race && !g_cts && (autocvar_g_telefrags || (tflags & TELEPORT_FLAG_FORCE_TDEATH))
153                         && !(round_handler_IsActive() && !round_handler_IsRoundStarted()))
154                 {
155                         tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
156                 }
157
158                 // player no longer is on ground
159                 UNSET_ONGROUND(player);
160
161                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
162                 player.oldvelocity = player.velocity;
163
164                 // reset tracking of who pushed you into a hazard (for kill credit)
165                 if(teleporter.owner)
166                 {
167                         player.pusher = teleporter.owner;
168                         player.pushltime = time + autocvar_g_maxpushtime;
169                         player.istypefrag = PHYS_INPUT_BUTTON_CHAT(player);
170                 }
171                 else
172                 {
173                         player.pushltime = 0;
174                         player.istypefrag = 0;
175                 }
176
177                 player.lastteleporttime = time;
178                 player.lastteleport_origin = from;
179         }
180 #endif
181 }
182
183 entity Simple_TeleportPlayer(entity teleporter, entity player)
184 {
185         vector locout;
186         entity e = NULL;
187
188         // Find the output teleporter
189         if(teleporter.enemy)
190         {
191                 e = teleporter.enemy;
192         }
193         else
194         {
195                 // sorry CSQC, random stuff ain't gonna happen
196 #ifdef SVQC
197                 RandomSelection_Init();
198                 FOREACH_ENTITY_STRING(targetname, teleporter.target,
199                 {
200                         bool p = true;
201                         if(STAT(TELEPORT_TELEFRAG_AVOID, player))
202                         {
203                         #ifdef SVQC
204                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
205                         #elif defined(CSQC)
206                                 locout = it.origin + '0 0 1' * (1 - player.mins.z - 24);
207                         #endif
208                                 if(check_tdeath(player, locout, '0 0 0', '0 0 0'))
209                                         p = false;
210                         }
211                         RandomSelection_AddEnt(it, (it.cnt ? it.cnt : 1), p);
212                 });
213                 e = RandomSelection_chosen_ent;
214 #endif
215         }
216
217 #ifdef SVQC
218         if(!e) { sprint(player, "Teleport destination vanished. Sorry... please complain to the mapper.\n"); }
219 #elif defined(CSQC)
220         if(!e) { LOG_INFO("Teleport destination could not be found from CSQC."); }
221 #endif
222
223         makevectors(e.mangle);
224
225         if(e.speed)
226                 if(vdist(player.velocity, >, e.speed))
227                         player.velocity = normalize(player.velocity) * max(0, e.speed);
228
229         if(STAT(TELEPORT_MAXSPEED, player))
230                 if(vdist(player.velocity, >, STAT(TELEPORT_MAXSPEED, player)))
231                         player.velocity = normalize(player.velocity) * max(0, STAT(TELEPORT_MAXSPEED, player));
232
233         locout = e.origin + '0 0 1' * (1 - player.mins.z - 24);
234
235         TeleportPlayer(teleporter, player, locout, e.mangle, v_forward * vlen(player.velocity), '0 0 0', '0 0 0', TELEPORT_FLAGS_TELEPORTER);
236
237         return e;
238 }
239
240 void teleport_findtarget(entity this)
241 {
242         bool istrigger = (this.solid == SOLID_TRIGGER);
243
244         int n = 0;
245         for(entity e = NULL; (e = find(e, targetname, this.target)); )
246         {
247                 ++n;
248 #ifdef SVQC
249                 if(e.move_movetype == MOVETYPE_NONE)
250                 {
251                         entity tracetest_ent = spawn();
252                         setsize(tracetest_ent, PL_MIN_CONST, PL_MAX_CONST);
253                         tracetest_ent.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP | DPCONTENTS_BOTCLIP;
254                         waypoint_spawnforteleporter(this, e.origin, 0, tracetest_ent);
255                         delete(tracetest_ent);
256                 }
257                 if(e.classname != "info_teleport_destination")
258                         LOG_INFO("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.");
259 #endif
260         }
261
262         if(n == 0)
263         {
264                 // no dest!
265                 objerror (this, "Teleporter with nonexistent target");
266                 return;
267         }
268         else if(n == 1)
269         {
270                 // exactly one dest - bots love that
271                 this.enemy = find(NULL, targetname, this.target);
272         }
273         else
274         {
275                 // have to use random selection every single time
276                 this.enemy = NULL;
277         }
278
279         // now enable touch
280         if(istrigger)
281                 settouch(this, Teleport_Touch);
282 #ifdef SVQC
283         if(istrigger)
284                 trigger_teleport_link(this);
285 #endif
286 }
287
288 entity Teleport_Find(vector mi, vector ma)
289 {
290         IL_EACH(g_teleporters, WarpZoneLib_BoxTouchesBrush(mi, ma, it, NULL),
291         {
292                 return it;
293         });
294         return NULL;
295 }
296
297 void WarpZone_PostTeleportPlayer_Callback(entity pl)
298 {
299 #ifdef SVQC
300         makevectors(pl.angles);
301         Reset_ArcBeam(pl, v_forward);
302         UpdateCSQCProjectileAfterTeleport(pl);
303         UpdateItemAfterTeleport(pl);
304     if (IS_PLAYER(pl)) anticheat_fixangle(pl);
305 #endif
306         // "disown" projectiles after teleport
307         if(pl.owner)
308         if(pl.owner == pl.realowner)
309         {
310         #ifdef SVQC
311                 if(!(pl.flags & FL_PROJECTILE))
312         #elif defined(CSQC)
313                 if(!(pl.flags & BIT(15))) // FL_PROJECTILE
314         #endif
315                         LOG_INFO("A non-projectile got through a warpzone and its owner cleared. It's a ", pl.classname, ".");
316                 pl.owner = NULL;
317         }
318         if(IS_PLAYER(pl))
319         {
320                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
321         #ifdef SVQC
322                 pl.oldvelocity = pl.velocity;
323         #endif
324         }
325 }