telefrag avoidance feature
[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         o = e.origin + '0 0 1' * (1 - other.mins_z - 24);
159
160         if(self.enemy)
161         {
162                 e = self.enemy;
163         }
164         else
165         {
166                 RandomSelection_Init();
167                 for(e = world; (e = find(e, targetname, self.target)); )
168                 {
169                         p = 1;
170                         if(cvar("g_telefrag_avoid"))
171                                 if(check_tdeath(other, o, '0 0 0', '0 0 0'))
172                                         p = 0;
173                         if(e.cnt)
174                                 RandomSelection_Add(e, 0, string_null, e.cnt, p);
175                         else
176                                 RandomSelection_Add(e, 0, string_null, 1, p);
177                 }
178                 e = RandomSelection_chosen_ent;
179         }
180
181         if(!e)
182         {
183                 sprint(other, "Teleport destination vanished. Sorry... please complain to the mapper.\n");
184         }
185
186         if(e.speed)
187                 if(vlen(other.velocity) > e.speed)
188                         other.velocity = normalize(other.velocity) * max(0, e.speed);
189
190         TeleportPlayer(self, other, o, e.mangle, v_forward * vlen(other.velocity), '0 0 0', '0 0 0');
191
192         if(e.target)
193         {
194                 oldself = self;
195                 activator = other;
196                 self = e;
197                 SUB_UseTargets();
198                 self = oldself;
199         }
200 }
201
202 void spawnfunc_info_teleport_destination (void)
203 {
204         self.classname = "info_teleport_destination";
205
206         self.mangle = self.angles;
207         self.angles = '0 0 0';
208
209         //setorigin (self, self.origin + '0 0 27');     // To fix a mappers' habit as old as Quake
210         setorigin (self, self.origin);
211
212         IFTARGETED
213         {
214         }
215         else
216                 objerror ("^3Teleport destination without a targetname");
217 }
218
219 void spawnfunc_misc_teleporter_dest (void)
220 {
221         spawnfunc_info_teleport_destination();
222 }
223
224 void spawnfunc_target_teleporter (void)
225 {
226         spawnfunc_info_teleport_destination();
227 }
228
229 void teleport_findtarget (void)
230 {
231         entity e;
232         float n;
233
234         n = 0;
235         for(e = world; (e = find(e, targetname, self.target)); )
236         {
237                 ++n;
238                 if(e.movetype == MOVETYPE_NONE)
239                         waypoint_spawnforteleporter(self, e.origin, 0);
240                 if(e.classname != "info_teleport_destination")
241                         print("^3MAPPER ERROR: teleporter does target an invalid teleport destination entity. Angles will not work.\n");
242         }
243
244         if(n == 0)
245         {
246                 // no dest!
247                 objerror ("Teleporter with nonexistant target");
248                 return;
249         }
250         else if(n == 1)
251         {
252                 // exactly one dest - bots love that
253                 self.enemy = find(e, targetname, self.target);
254                 self.dest = self.enemy.origin;
255         }
256         else
257         {
258                 // have to use random selection every single time
259                 self.enemy = world;
260         }
261
262         // now enable touch
263         self.touch = Teleport_Touch;
264 }
265
266 void spawnfunc_trigger_teleport (void)
267 {
268         self.angles = '0 0 0';
269
270         EXACTTRIGGER_INIT;
271
272         self.use = trigger_teleport_use;
273
274         // this must be called to spawn the teleport waypoints for bots
275         InitializeEntity(self, teleport_findtarget, INITPRIO_FINDTARGET);
276
277         if (!self.target)
278         {
279                 objerror ("Teleporter with no target");
280                 return;
281         }
282 }