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