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