]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/trigger/jumppads.qc
Still broken, but more fun
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / trigger / jumppads.qc
1 // TODO: split target_push and put it in the target folder
2
3 #ifdef SVQC
4
5 const float PUSH_ONCE = 1;
6 const float PUSH_SILENT = 2;
7
8 .float pushltime;
9 .float istypefrag;
10
11 void() SUB_UseTargets;
12
13 void trigger_push_use()
14 {
15         if(teamplay)
16         {
17                 self.team = activator.team;
18                 self.SendFlags |= 2;
19         }
20 }
21 #endif
22
23 float trigger_push_calculatevelocity_flighttime;
24
25 /*
26         trigger_push_calculatevelocity
27
28         Arguments:
29           org - origin of the object which is to be pushed
30           tgt - target entity (can be either a point or a model entity; if it is
31                 the latter, its midpoint is used)
32           ht  - jump height, measured from the higher one of org and tgt's midpoint
33
34         Returns: velocity for the jump
35         the global trigger_push_calculatevelocity_flighttime is set to the total
36         jump time
37  */
38
39 vector trigger_push_calculatevelocity(vector org, entity tgt, float ht)
40 {
41         float grav, sdist, zdist, vs, vz, jumpheight;
42         vector sdir, torg;
43
44         torg = tgt.origin + (tgt.mins + tgt.maxs) * 0.5;
45
46         grav = PHYS_GRAVITY;
47         if(PHYS_ENTGRAVITY(other))
48                 grav *= PHYS_ENTGRAVITY(other);
49
50         zdist = torg_z - org_z;
51         sdist = vlen(torg - org - zdist * '0 0 1');
52         sdir = normalize(torg - org - zdist * '0 0 1');
53
54         // how high do we need to push the player?
55         jumpheight = fabs(ht);
56         if(zdist > 0)
57                 jumpheight = jumpheight + zdist;
58
59         /*
60                 STOP.
61
62                 You will not understand the following equations anyway...
63                 But here is what I did to get them.
64
65                 I used the functions
66
67                   s(t) = t * vs
68                   z(t) = t * vz - 1/2 grav t^2
69
70                 and solved for:
71
72                   s(ti) = sdist
73                   z(ti) = zdist
74                   max(z, ti) = jumpheight
75
76                 From these three equations, you will find the three parameters vs, vz
77                 and ti.
78          */
79
80         // push him so high...
81         vz = sqrt(fabs(2 * grav * jumpheight)); // NOTE: sqrt(positive)!
82
83         // we start with downwards velocity only if it's a downjump and the jump apex should be outside the jump!
84         if(ht < 0)
85                 if(zdist < 0)
86                         vz = -vz;
87
88         vector solution;
89         solution = solve_quadratic(0.5 * grav, -vz, zdist); // equation "z(ti) = zdist"
90         // ALWAYS solvable because jumpheight >= zdist
91         if(!solution_z)
92                 solution_y = solution_x; // just in case it is not solvable due to roundoff errors, assume two equal solutions at their center (this is mainly for the usual case with ht == 0)
93         if(zdist == 0)
94                 solution_x = solution_y; // solution_x is 0 in this case, so don't use it, but rather use solution_y (which will be sqrt(0.5 * jumpheight / grav), actually)
95
96         if(zdist < 0)
97         {
98                 // down-jump
99                 if(ht < 0)
100                 {
101                         // almost straight line type
102                         // jump apex is before the jump
103                         // we must take the larger one
104                         trigger_push_calculatevelocity_flighttime = solution_y;
105                 }
106                 else
107                 {
108                         // regular jump
109                         // jump apex is during the jump
110                         // we must take the larger one too
111                         trigger_push_calculatevelocity_flighttime = solution_y;
112                 }
113         }
114         else
115         {
116                 // up-jump
117                 if(ht < 0)
118                 {
119                         // almost straight line type
120                         // jump apex is after the jump
121                         // we must take the smaller one
122                         trigger_push_calculatevelocity_flighttime = solution_x;
123                 }
124                 else
125                 {
126                         // regular jump
127                         // jump apex is during the jump
128                         // we must take the larger one
129                         trigger_push_calculatevelocity_flighttime = solution_y;
130                 }
131         }
132         vs = sdist / trigger_push_calculatevelocity_flighttime;
133
134         // finally calculate the velocity
135         return sdir * vs + '0 0 1' * vz;
136 }
137
138 void trigger_push_touch()
139 {
140         if (self.active == ACTIVE_NOT)
141                 return;
142
143 #ifdef SVQC
144         if (!isPushable(other))
145                 return;
146 #endif
147
148         if(self.team)
149                 if(((self.spawnflags & 4) == 0) == (DIFF_TEAM(self, other)))
150                         return;
151
152         EXACTTRIGGER_TOUCH;
153
154         if(self.enemy)
155         {
156                 other.velocity = trigger_push_calculatevelocity(other.origin, self.enemy, self.height);
157         }
158         else if(self.target)
159         {
160                 entity e;
161                 RandomSelection_Init();
162                 for(e = world; (e = find(e, targetname, self.target)); )
163                 {
164                         if(e.cnt)
165                                 RandomSelection_Add(e, 0, string_null, e.cnt, 1);
166                         else
167                                 RandomSelection_Add(e, 0, string_null, 1, 1);
168                 }
169                 other.velocity = trigger_push_calculatevelocity(other.origin, RandomSelection_chosen_ent, self.height);
170         }
171         else
172         {
173                 other.velocity = self.movedir;
174         }
175
176         UNSET_ONGROUND(other);
177
178 #ifdef SVQC
179         if (IS_PLAYER(other))
180         {
181                 // reset tracking of oldvelocity for impact damage (sudden velocity changes)
182                 other.oldvelocity = other.velocity;
183
184                 if(self.pushltime < time)  // prevent "snorring" sound when a player hits the jumppad more than once
185                 {
186                         // flash when activated
187                         pointparticles(particleeffectnum("jumppad_activate"), other.origin, other.velocity, 1);
188                         sound (other, CH_TRIGGER, self.noise, VOL_BASE, ATTEN_NORM);
189                         self.pushltime = time + 0.2;
190                 }
191
192                 if(IS_REAL_CLIENT(other) || IS_BOT_CLIENT(other))
193                 {
194                         float i;
195                         float found;
196                         found = FALSE;
197                         for(i = 0; i < other.jumppadcount && i < NUM_JUMPPADSUSED; ++i)
198                                 if(other.(jumppadsused[i]) == self)
199                                         found = TRUE;
200                         if(!found)
201                         {
202                                 other.(jumppadsused[other.jumppadcount % NUM_JUMPPADSUSED]) = self;
203                                 other.jumppadcount = other.jumppadcount + 1;
204                         }
205
206                         if(IS_REAL_CLIENT(other))
207                         {
208                                 if(self.message)
209                                         centerprint(other, self.message);
210                         }
211                         else
212                                 other.lastteleporttime = time;
213
214                         if (other.deadflag == DEAD_NO)
215                                 animdecide_setaction(other, ANIMACTION_JUMP, TRUE);
216                 }
217                 else
218                         other.jumppadcount = TRUE;
219
220                 // reset tracking of who pushed you into a hazard (for kill credit)
221                 other.pushltime = 0;
222                 other.istypefrag = 0;
223         }
224
225         if(self.enemy.target)
226         {
227                 entity oldself;
228                 oldself = self;
229                 activator = other;
230                 self = self.enemy;
231                 SUB_UseTargets();
232                 self = oldself;
233         }
234
235         if (other.flags & FL_PROJECTILE)
236         {
237                 other.angles = vectoangles (other.velocity);
238                 switch(other.movetype)
239                 {
240                         case MOVETYPE_FLY:
241                                 other.movetype = MOVETYPE_TOSS;
242                                 other.gravity = 1;
243                                 break;
244                         case MOVETYPE_BOUNCEMISSILE:
245                                 other.movetype = MOVETYPE_BOUNCE;
246                                 other.gravity = 1;
247                                 break;
248                 }
249                 UpdateCSQCProjectile(other);
250         }
251
252         if (self.spawnflags & PUSH_ONCE)
253         {
254                 self.touch = func_null;
255                 self.think = SUB_Remove;
256                 self.nextthink = time;
257         }
258 #endif
259 }
260
261 #ifdef SVQC
262 void trigger_push_link();
263 void trigger_push_updatelink();
264 #endif
265 void trigger_push_findtarget()
266 {
267         entity t;
268         vector org;
269
270         // first calculate a typical start point for the jump
271         org = (self.absmin + self.absmax) * 0.5;
272         org_z = self.absmax_z - PL_MIN_z;
273
274         if (self.target)
275         {
276                 float n = 0;
277                 for(t = world; (t = find(t, targetname, self.target)); )
278                 {
279                         ++n;
280 #ifdef SVQC
281                         entity e = spawn();
282                         setorigin(e, org);
283                         setsize(e, PL_MIN, PL_MAX);
284                         e.velocity = trigger_push_calculatevelocity(org, t, self.height);
285                         tracetoss(e, e);
286                         if(e.movetype == MOVETYPE_NONE)
287                                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
288                         remove(e);
289 #endif
290                 }
291
292                 if(!n)
293                 {
294                         // no dest!
295 #ifdef SVQC
296                         objerror ("Jumppad with nonexistant target");
297 #endif
298                         return;
299                 }
300                 else if(n == 1)
301                 {
302                         // exactly one dest - bots love that
303                         self.enemy = find(world, targetname, self.target);
304                 }
305                 else
306                 {
307                         // have to use random selection every single time
308                         self.enemy = world;
309                 }
310         }
311 #ifdef SVQC
312         else
313         {
314                 entity e = spawn();
315                 setorigin(e, org);
316                 setsize(e, PL_MIN, PL_MAX);
317                 e.velocity = self.movedir;
318                 tracetoss(e, e);
319                 waypoint_spawnforteleporter(self, trace_endpos, vlen(trace_endpos - org) / vlen(e.velocity));
320                 remove(e);
321         }
322
323         trigger_push_link();
324         defer(0.1, trigger_push_updatelink);
325 #endif
326 }
327
328 #ifdef SVQC
329 float trigger_push_send(entity to, float sf)
330 {
331         WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_PUSH);
332         WriteByte(MSG_ENTITY, sf);
333
334         if(sf & 1)
335         {
336                 WriteString(MSG_ENTITY, self.target);
337                 WriteByte(MSG_ENTITY, self.team);
338                 WriteInt24_t(MSG_ENTITY, self.spawnflags);
339                 WriteByte(MSG_ENTITY, self.active);
340                 WriteByte(MSG_ENTITY, self.warpzone_isboxy);
341                 WriteByte(MSG_ENTITY, self.height);
342                 WriteByte(MSG_ENTITY, self.scale);
343                 WriteCoord(MSG_ENTITY, self.origin_x);
344                 WriteCoord(MSG_ENTITY, self.origin_y);
345                 WriteCoord(MSG_ENTITY, self.origin_z);
346
347                 WriteCoord(MSG_ENTITY, self.mins_x);
348                 WriteCoord(MSG_ENTITY, self.mins_y);
349                 WriteCoord(MSG_ENTITY, self.mins_z);
350                 WriteCoord(MSG_ENTITY, self.maxs_x);
351                 WriteCoord(MSG_ENTITY, self.maxs_y);
352                 WriteCoord(MSG_ENTITY, self.maxs_z);
353
354                 WriteCoord(MSG_ENTITY, self.movedir_x);
355                 WriteCoord(MSG_ENTITY, self.movedir_y);
356                 WriteCoord(MSG_ENTITY, self.movedir_z);
357
358                 WriteCoord(MSG_ENTITY, self.angles_x);
359                 WriteCoord(MSG_ENTITY, self.angles_y);
360                 WriteCoord(MSG_ENTITY, self.angles_z);
361         }
362
363         if(sf & 2)
364         {
365                 WriteByte(MSG_ENTITY, self.team);
366                 WriteByte(MSG_ENTITY, self.active);
367         }
368
369         return TRUE;
370 }
371
372 void trigger_push_updatelink()
373 {
374         self.SendFlags |= 1;
375 }
376
377 void trigger_push_link()
378 {
379         Net_LinkEntity(self, FALSE, 0, trigger_push_send);
380 }
381 #endif
382 #ifdef SVQC
383 /*
384  * ENTITY PARAMETERS:
385  *
386  *   target:  target of jump
387  *   height:  the absolute value is the height of the highest point of the jump
388  *            trajectory above the higher one of the player and the target.
389  *            the sign indicates whether the highest point is INSIDE (positive)
390  *            or OUTSIDE (negative) of the jump trajectory. General rule: use
391  *            positive values for targets mounted on the floor, and use negative
392  *            values to target a point on the ceiling.
393  *   movedir: if target is not set, this * speed * 10 is the velocity to be reached.
394  */
395 void spawnfunc_trigger_push()
396 {
397         SetMovedir ();
398
399         EXACTTRIGGER_INIT;
400
401         self.active = ACTIVE_ACTIVE;
402         self.use = trigger_push_use;
403         self.touch = trigger_push_touch;
404
405         // normal push setup
406         if (!self.speed)
407                 self.speed = 1000;
408         self.movedir = self.movedir * self.speed * 10;
409
410         if (!self.noise)
411                 self.noise = "misc/jumppad.wav";
412         precache_sound (self.noise);
413
414         // this must be called to spawn the teleport waypoints for bots
415         InitializeEntity(self, trigger_push_findtarget, INITPRIO_FINDTARGET);
416 }
417
418
419 float target_push_send(entity to, float sf)
420 {
421         WriteByte(MSG_ENTITY, ENT_CLIENT_TARGET_PUSH);
422
423         WriteByte(MSG_ENTITY, self.cnt);
424         WriteString(MSG_ENTITY, self.targetname);
425         WriteCoord(MSG_ENTITY, self.origin_x);
426         WriteCoord(MSG_ENTITY, self.origin_y);
427         WriteCoord(MSG_ENTITY, self.origin_z);
428
429         return TRUE;
430 }
431
432 void target_push_link()
433 {
434         Net_LinkEntity(self, FALSE, 0, target_push_send);
435         self.SendFlags |= 1; // update
436 }
437
438 void spawnfunc_target_push() { target_push_link(); }
439 void spawnfunc_info_notnull() { target_push_link(); }
440 void spawnfunc_target_position() { target_push_link(); }
441
442 #endif
443
444 #ifdef CSQC
445 void ent_trigger_push()
446 {
447         float sf = ReadByte();
448
449         if(sf & 1)
450         {
451                 self.classname = "jumppad";
452                 self.target = strzone(ReadString());
453                 float mytm = ReadByte(); if(mytm) { self.team = mytm - 1; }
454                 self.spawnflags = ReadInt24_t();
455                 self.active = ReadByte();
456                 self.warpzone_isboxy = ReadByte();
457                 self.height = ReadByte();
458                 self.scale = ReadByte();
459                 self.origin_x = ReadCoord();
460                 self.origin_y = ReadCoord();
461                 self.origin_z = ReadCoord();
462                 setorigin(self, self.origin);
463                 self.mins_x = ReadCoord();
464                 self.mins_y = ReadCoord();
465                 self.mins_z = ReadCoord();
466                 self.maxs_x = ReadCoord();
467                 self.maxs_y = ReadCoord();
468                 self.maxs_z = ReadCoord();
469                 setsize(self, self.mins, self.maxs);
470                 self.movedir_x = ReadCoord();
471                 self.movedir_y = ReadCoord();
472                 self.movedir_z = ReadCoord();
473                 self.angles_x = ReadCoord();
474                 self.angles_y = ReadCoord();
475                 self.angles_z = ReadCoord();
476
477                 self.solid = SOLID_TRIGGER;
478                 self.draw = trigger_draw_generic;
479                 self.trigger_touch = trigger_push_touch;
480                 self.drawmask = MASK_ENGINE;
481                 self.move_time = time;
482                 trigger_push_findtarget();
483         }
484
485         if(sf & 2)
486         {
487                 self.team = ReadByte();
488                 self.active = ReadByte();
489         }
490 }
491
492 void ent_target_push()
493 {
494         self.classname = "push_target";
495         self.cnt = ReadByte();
496         self.targetname = strzone(ReadString());
497         self.origin_x = ReadCoord();
498         self.origin_y = ReadCoord();
499         self.origin_z = ReadCoord();
500         setorigin(self, self.origin);
501
502         self.drawmask = MASK_ENGINE;
503 }
504 #endif