]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/t_teleporters.qc
cvar for enabling/disabling playerclips on a map (useful on eg defrag maps with ...
[divverent/nexuiz.git] / data / qcsrc / server / t_teleporters.qc
1 void trigger_teleport_use()
2 {
3         if(teams_matter)
4                 self.team = activator.team;
5 }
6
7 #define TDEATHLOOP(o) \
8         entity head; \
9         vector deathmin; \
10         vector deathmax; \
11         float deathradius; \
12         deathmin = (o) + player.mins; \
13         deathmax = (o) + player.maxs; \
14         if(telefragmin != telefragmax) \
15         { \
16                 if(deathmin_x > telefragmin_x) deathmin_x = telefragmin_x; \
17                 if(deathmin_y > telefragmin_y) deathmin_y = telefragmin_y; \
18                 if(deathmin_z > telefragmin_z) deathmin_z = telefragmin_z; \
19                 if(deathmax_x < telefragmax_x) deathmax_x = telefragmax_x; \
20                 if(deathmax_y < telefragmax_y) deathmax_y = telefragmax_y; \
21                 if(deathmax_z < telefragmax_z) deathmax_z = telefragmax_z; \
22         } \
23         deathradius = max(vlen(deathmin), vlen(deathmax)); \
24         for(head = findradius(o, deathradius); head; head = head.chain) \
25                 if(head != player) \
26                         if(head.takedamage) \
27                                 if(boxesoverlap(deathmin, deathmax, head.absmin, head.absmax))
28         
29
30 float check_tdeath(entity player, vector org, vector telefragmin, vector telefragmax)
31 {
32         TDEATHLOOP(org)
33         {
34                 if ((player.classname == "player") && (player.health >= 1))
35                 {
36                         if(head.classname == "player")
37                                 if(head.health >= 1)
38                                         return 1;
39                 }
40         }
41         return 0;
42 }
43 float tdeath_hit;
44 void tdeath(entity player, entity teleporter, entity telefragger, vector telefragmin, vector telefragmax)
45 {
46         TDEATHLOOP(player.origin)
47         {
48                 if ((player.classname == "player") && (player.health >= 1))
49                 {
50                         if(head.classname == "player")
51                                 if(head.health >= 1)
52                                         ++tdeath_hit;
53                         Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG, head.origin, '0 0 0');
54                 }
55                 else if (telefragger.health < 1) // corpses gib
56                         Damage (head, teleporter, telefragger, 10000, DEATH_TELEFRAG, head.origin, '0 0 0');
57                 else // dead bodies and monsters gib themselves instead of telefragging
58                         Damage (telefragger, teleporter, telefragger, 10000, DEATH_TELEFRAG, telefragger.origin, '0 0 0');
59         }
60 }
61
62 void spawn_tdeath(vector v0, entity e, vector v)
63 {
64         tdeath(e, e, e, '0 0 0', '0 0 0');
65 }
66
67 .entity pusher;
68 void TeleportPlayer(entity teleporter, entity player, vector to, vector to_angles, vector to_velocity, vector telefragmin, vector telefragmax)
69 {
70         entity oldself;
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         if(self.pushltime < time) // only show one teleport effect per teleporter per 0.2 seconds, for better fps
82         {
83                 sound (player, CHAN_TRIGGER, "misc/teleport.wav", VOL_BASE, ATTN_NORM);
84                 pointparticles(particleeffectnum("teleport"), player.origin, '0 0 0', 1);
85                 pointparticles(particleeffectnum("teleport"), to + v_forward * 32, '0 0 0', 1);
86                 self.pushltime = time + 0.2;
87         }
88
89         // Relocate the player
90         // assuming to allows PL_MIN to PL_MAX box and some more
91         from = player.origin;
92         setorigin (player, to);
93         player.oldorigin = to; // don't undo the teleport by unsticking
94         player.angles = to_angles;
95         player.fixangle = TRUE;
96         player.velocity = to_velocity;
97         BITXOR_ASSIGN(player.effects, EF_TELEPORT_BIT);
98
99         UpdateCSQCProjectileAfterTeleport(player);
100
101         if(player.classname == "player")
102         {
103                 if(player.takedamage && player.deadflag == DEAD_NO && !g_race && !g_cts && cvar("g_telefrags"))
104                         tdeath(player, teleporter, telefragger, telefragmin, telefragmax);
105
106                 // player no longer is on ground
107                 player.flags &~= FL_ONGROUND;
108
109                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
110                 player.oldvelocity = player.velocity;
111
112                 // reset tracking of who pushed you into a hazard (for kill credit)
113                 if(teleporter.owner)
114                 {
115                         player.pusher = teleporter.owner;
116                         player.pushltime = time + cvar("g_maxpushtime");
117                 }
118                 else
119                 {
120                         player.pushltime = 0;
121                 }
122
123                 if(player.isbot)
124                         player.lastteleporttime = time;
125
126                 // stop player name display
127                 {
128                         oldself = self;
129                         self = player;
130                         ClearSelectedPlayer();
131                         self = oldself;
132                 }
133         }
134 }
135
136 void Teleport_Touch (void)
137 {
138         entity oldself, e;
139         vector o;
140         float p;
141
142         if (other.health < 1)
143                 return;
144         if not(other.flags & FL_CLIENT) // FIXME: Make missiles firable through the teleport too
145                 return;
146
147         if(self.team)
148                 if((self.spawnflags & 4 == 0) == (self.team != other.team))
149                         return;
150
151         EXACTTRIGGER_TOUCH;
152
153         makevectors(self.enemy.mangle);
154
155         if(other.classname == "player")
156                 RemoveGrapplingHook(other);
157         
158         if(self.enemy)
159         {
160                 e = self.enemy;
161         }
162         else
163         {
164                 RandomSelection_Init();
165                 for(e = world; (e = find(e, targetname, self.target)); )
166                 {
167                         p = 1;
168                         if(cvar("g_telefrags_avoid"))
169                         {
170                                 o = e.origin + '0 0 1' * (1 - other.mins_z - 24);
171                                 if(check_tdeath(other, o, '0 0 0', '0 0 0'))
172                                         p = 0;
173                         }
174                         if(e.cnt)
175                                 RandomSelection_Add(e, 0, string_null, e.cnt, p);
176                         else
177                                 RandomSelection_Add(e, 0, string_null, 1, p);
178                 }
179                 e = RandomSelection_chosen_ent;
180         }
181
182         if(!e)
183         {
184                 sprint(other, "Teleport destination vanished. Sorry... please complain to the mapper.\n");
185         }
186
187         if(e.speed)
188                 if(vlen(other.velocity) > e.speed)
189                         other.velocity = normalize(other.velocity) * max(0, e.speed);
190         if(cvar("g_teleport_maxspeed"))
191                 if(vlen(other.velocity) > cvar("g_teleport_maxspeed"))
192                         other.velocity = normalize(other.velocity) * max(0, cvar("g_teleport_maxspeed"));
193
194         o = e.origin + '0 0 1' * (1 - other.mins_z - 24);
195         TeleportPlayer(self, other, o, e.mangle, v_forward * vlen(other.velocity), '0 0 0', '0 0 0');
196
197         if(e.target)
198         {
199                 oldself = self;
200                 activator = other;
201                 self = e;
202                 SUB_UseTargets();
203                 self = oldself;
204         }
205 }
206
207 void spawnfunc_info_teleport_destination (void)
208 {
209         self.classname = "info_teleport_destination";
210
211         self.mangle = self.angles;
212         self.angles = '0 0 0';
213
214         //setorigin (self, self.origin + '0 0 27');     // To fix a mappers' habit as old as Quake
215         setorigin (self, self.origin);
216
217         IFTARGETED
218         {
219         }
220         else
221                 objerror ("^3Teleport destination without a targetname");
222 }
223
224 void spawnfunc_misc_teleporter_dest (void)
225 {
226         spawnfunc_info_teleport_destination();
227 }
228
229 void spawnfunc_target_teleporter (void)
230 {
231         spawnfunc_info_teleport_destination();
232 }
233
234 void teleport_findtarget (void)
235 {
236         entity e;
237         float n;
238
239         n = 0;
240         for(e = world; (e = find(e, targetname, self.target)); )
241         {
242                 ++n;
243                 if(e.movetype == MOVETYPE_NONE)
244                         waypoint_spawnforteleporter(self, e.origin, 0);
245                 if(e.classname != "info_teleport_destination")
246                         print("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
247         }
248
249         if(n == 0)
250         {
251                 // no dest!
252                 objerror ("Teleporter with nonexistant target");
253                 return;
254         }
255         else if(n == 1)
256         {
257                 // exactly one dest - bots love that
258                 self.enemy = find(e, targetname, self.target);
259                 self.dest = self.enemy.origin;
260         }
261         else
262         {
263                 // have to use random selection every single time
264                 self.enemy = world;
265         }
266
267         // now enable touch
268         self.touch = Teleport_Touch;
269 }
270
271 void spawnfunc_trigger_teleport (void)
272 {
273         self.angles = '0 0 0';
274
275         EXACTTRIGGER_INIT;
276
277         self.use = trigger_teleport_use;
278
279         // this must be called to spawn the teleport waypoints for bots
280         InitializeEntity(self, teleport_findtarget, INITPRIO_FINDTARGET);
281
282         if (!self.target)
283         {
284                 objerror ("Teleporter with no target");
285                 return;
286         }
287 }