]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_impulse.qc
Impulse refactor
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_impulse.qc
1 #include "round_handler.qh"
2
3 #include "bot/waypoints.qh"
4
5 #include "weapons/throwing.qh"
6 #include "command/common.qh"
7 #include "cheats.qh"
8 #include "bot/navigation.qh"
9 #include "weapons/selection.qh"
10 #include "weapons/tracing.qh"
11 #include "weapons/weaponsystem.qh"
12
13 #include "../common/minigames/sv_minigames.qh"
14
15 #include "../common/weapons/all.qh"
16 #include "../common/vehicles/sv_vehicles.qh"
17
18 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
19
20 .entity vehicle;
21
22 REGISTRY(IMPULSES, 255)
23 REGISTER_REGISTRY(IMPULSES)
24 REGISTRY_SORT(IMPULSES)
25 STATIC_INIT(IMPULSES_renumber)
26 {
27         FOREACH(IMPULSES, true, LAMBDA(it.m_id = i));
28 }
29 REGISTRY_CHECK(IMPULSES)
30
31 .void(entity this) impulse_handle;
32
33 #define IMPULSE(id, n) _IMPULSE(IMP_##id, n)
34 #define _IMPULSE(id, n) \
35         void id##_handle(entity this); \
36         REGISTER(IMPULSES, id, m_id, new(Impulse)) \
37         { \
38                 make_pure(this); \
39                 this.impulse = n; \
40                 this.impulse_handle = id##_handle; \
41         } \
42         void id##_handle(entity this)
43
44 /**
45  * Impulse map:
46  *
47  * 0 reserved (no input)
48  *
49  * 99: loaded
50  *
51  * 140: moving clone
52  * 141: ctf speedrun
53  * 142: fixed clone
54  * 143: emergency teleport
55  * 148: unfairly eliminate
56  *
57  * TODO:
58  * 200 to 209: prev weapon shortcuts
59  * 210 to 219: best weapon shortcuts
60  * 220 to 229: next weapon shortcuts
61  * 230 to 253: individual weapons (up to 24)
62  */
63
64 // weapon switching impulses
65
66 #define X(slot, imp) \
67         IMPULSE(weapon_group_##slot, imp) \
68         { \
69                 if (this.deadflag != DEAD_NO) return; \
70                 W_NextWeaponOnImpulse(slot); \
71         }
72 X(1, 1)
73 X(2, 2)
74 X(3, 3)
75 X(4, 4)
76 X(5, 5)
77 X(6, 6)
78 X(7, 7)
79 X(8, 8)
80 X(9, 9)
81 X(0, 14)
82 #undef X
83
84 IMPULSE(weapon_next_byid, 10)
85 {
86         if (this.vehicle) return;
87         if (this.deadflag != DEAD_NO) return;
88         W_NextWeapon(0);
89 }
90
91 IMPULSE(weapon_prev_byid, 12)
92 {
93         if (this.vehicle) return;
94         if (this.deadflag != DEAD_NO) return;
95         W_PreviousWeapon(0);
96 }
97
98 IMPULSE(weapon_next_bygroup, 18)
99 {
100         if (this.vehicle) return;
101         if (this.deadflag != DEAD_NO) return;
102         W_NextWeapon(1);
103 }
104 IMPULSE(weapon_prev_bygroup, 19)
105 {
106         if (this.vehicle) return;
107         if (this.deadflag != DEAD_NO) return;
108         W_PreviousWeapon(1);
109 }
110
111 IMPULSE(weapon_next_bypriority, 15)
112 {
113         if (this.vehicle) return;
114         if (this.deadflag != DEAD_NO) return;
115         W_NextWeapon(2);
116 }
117 IMPULSE(weapon_prev_bypriority, 16)
118 {
119         if (this.vehicle) return;
120         if (this.deadflag != DEAD_NO) return;
121         W_PreviousWeapon(2);
122 }
123
124 IMPULSE(weapon_last, 11)
125 {
126         if (this.vehicle) return;
127         if (this.deadflag != DEAD_NO) return;
128         W_LastWeapon();
129 }
130
131 IMPULSE(weapon_best, 13)
132 {
133         if (this.vehicle) return;
134         if (this.deadflag != DEAD_NO) return;
135         W_SwitchWeapon(w_getbestweapon(this));
136 }
137
138 IMPULSE(weapon_throw, 17)
139 {
140         if (this.vehicle) return;
141         if (this.deadflag != DEAD_NO) return;
142         W_ThrowWeapon(W_CalculateProjectileVelocity(this.velocity, v_forward * 750, false), '0 0 0', true);
143 }
144
145 IMPULSE(weapon_reload, 20)
146 {
147         if (this.vehicle) return;
148         if (this.deadflag != DEAD_NO) return;
149         if (forbidWeaponUse(this)) return;
150         Weapon w = Weapons_from(this.weapon);
151         w.wr_reload(w);
152 }
153
154 void ImpulseCommands(entity this)
155 {
156         if (gameover) return;
157
158         int imp = this.impulse;
159         if (!imp) return;
160         this.impulse = 0;
161
162         if (this.active_minigame && MinigameImpulse(imp)) return;
163
164         if (timeout_status == TIMEOUT_ACTIVE) return;  // don't allow any impulses while the game is paused
165
166         // allow only weapon change impulses when not in round time
167         if (round_handler_IsActive() && !round_handler_IsRoundStarted())
168                 if (imp == 17 || (imp >= 20 && imp < 200) || imp > 253) return;
169
170         if (this.vehicle && this.vehicle.deadflag == DEAD_NO)
171         {
172                 bool(int) f = this.vehicle.vehicles_impulse;
173                 if (f && f(imp)) return;
174                 if (vehicle_impulse(imp)) return;
175         }
176
177         if (CheatImpulse(imp)) return;
178
179         FOREACH(IMPULSES, it.impulse == imp, {
180                 it.impulse_handle(this);
181                 return;
182         });
183
184         if (imp >= 200 && imp <= 229)
185         {
186                 if (!this.vehicle && this.deadflag == DEAD_NO)
187                 {
188                         // custom order weapon cycling
189                         int i = imp % 10;
190                         int m = (imp - (210 + i));  // <0 for prev, =0 for best, >0 for next
191                         W_CycleWeapon(this.(cvar_cl_weaponpriorities[i]), m);
192                 }
193                 // else // don't retry, as this can break weaplast bind
194                 // this.impulse = imp; // retry in next frame
195         }
196         else if (imp >= WEP_IMPULSE_BEGIN && imp <= WEP_IMPULSE_END)
197         {
198                 if (!this.vehicle && this.deadflag == DEAD_NO) W_SwitchWeapon(imp - WEP_IMPULSE_BEGIN + WEP_FIRST);
199                 // else // don't retry, as this can break weaplast bind
200                 // this.impulse = imp; // retry in next frame
201         }
202 }
203
204 IMPULSE(use, 21)
205 {
206         PlayerUseKey();
207 }
208
209 IMPULSE(waypoint_personal_here, 30)
210 {
211         entity wp = WaypointSprite_DeployPersonal(WP_Waypoint, this.origin, RADARICON_WAYPOINT);
212         if (wp) WaypointSprite_Ping(wp);
213         sprint(this, "personal waypoint spawned at location\n");
214 }
215
216 IMPULSE(waypoint_personal_crosshair, 31)
217 {
218         WarpZone_crosshair_trace(this);
219         entity wp = WaypointSprite_DeployPersonal(WP_Waypoint, trace_endpos, RADARICON_WAYPOINT);
220         if (wp) WaypointSprite_Ping(wp);
221         sprint(this, "personal waypoint spawned at crosshair\n");
222 }
223
224 IMPULSE(waypoint_personal_death, 32)
225 {
226         if (!this.death_origin) return;
227         entity wp = WaypointSprite_DeployPersonal(WP_Waypoint, this.death_origin, RADARICON_WAYPOINT);
228         if (wp) WaypointSprite_Ping(wp);
229         sprint(this, "personal waypoint spawned at death location\n");
230 }
231
232 IMPULSE(waypoint_here_follow, 33)
233 {
234         if (!teamplay) return;
235         if (this.deadflag != DEAD_NO) return;
236         if (!MUTATOR_CALLHOOK(HelpMePing, this))
237         {
238                 entity wp = WaypointSprite_Attach(WP_Helpme, true, RADARICON_HELPME);
239                 if (!wp) WaypointSprite_HelpMePing(this.waypointsprite_attachedforcarrier);
240                 else WaypointSprite_Ping(wp);
241         }
242         sprint(this, "HELP ME attached\n");
243 }
244
245 IMPULSE(waypoint_here_here, 34)
246 {
247         entity wp = WaypointSprite_DeployFixed(WP_Here, false, this.origin, RADARICON_HERE);
248         if (wp) WaypointSprite_Ping(wp);
249         sprint(this, "HERE spawned at location\n");
250 }
251
252 IMPULSE(waypoint_here_crosshair, 35)
253 {
254         WarpZone_crosshair_trace(this);
255         entity wp = WaypointSprite_DeployFixed(WP_Here, false, trace_endpos, RADARICON_HERE);
256         if (wp) WaypointSprite_Ping(wp);
257         sprint(this, "HERE spawned at crosshair\n");
258 }
259
260 IMPULSE(waypoint_here_death, 36)
261 {
262         if (!this.death_origin) return;
263         entity wp = WaypointSprite_DeployFixed(WP_Here, false, this.death_origin, RADARICON_HERE);
264         if (wp) WaypointSprite_Ping(wp);
265         sprint(this, "HERE spawned at death location\n");
266 }
267
268 IMPULSE(waypoint_danger_here, 37)
269 {
270         entity wp = WaypointSprite_DeployFixed(WP_Danger, false, this.origin, RADARICON_DANGER);
271         if (wp) WaypointSprite_Ping(wp);
272         sprint(this, "DANGER spawned at location\n");
273 }
274
275 IMPULSE(waypoint_danger_crosshair, 38)
276 {
277         WarpZone_crosshair_trace(this);
278         entity wp = WaypointSprite_DeployFixed(WP_Danger, false, trace_endpos, RADARICON_DANGER);
279         if (wp) WaypointSprite_Ping(wp);
280         sprint(this, "DANGER spawned at crosshair\n");
281 }
282
283 IMPULSE(waypoint_danger_death, 39)
284 {
285         if (!this.death_origin) return;
286         entity wp = WaypointSprite_DeployFixed(WP_Danger, false, this.death_origin, RADARICON_DANGER);
287         if (wp) WaypointSprite_Ping(wp);
288         sprint(this, "DANGER spawned at death location\n");
289 }
290
291 IMPULSE(waypoint_clear_personal, 47)
292 {
293         WaypointSprite_ClearPersonal();
294         if (this.personal)
295         {
296                 remove(this.personal);
297                 this.personal = NULL;
298         }
299         sprint(this, "personal waypoint cleared\n");
300 }
301
302 IMPULSE(waypoint_clear_team, 48)
303 {
304         WaypointSprite_ClearOwned();
305         if (this.personal)
306         {
307                 remove(this.personal);
308                 this.personal = NULL;
309         }
310         sprint(this, "all waypoints cleared\n");
311 }
312
313 IMPULSE(navwaypoint_spawn, 103)
314 {
315         if (!autocvar_g_waypointeditor) return;
316         waypoint_schedulerelink(waypoint_spawn(this.origin, this.origin, 0));
317         bprint(strcat("Waypoint spawned at ", vtos(this.origin), "\n"));
318 }
319
320 IMPULSE(navwaypoint_remove, 104)
321 {
322         if (!autocvar_g_waypointeditor) return;
323         entity e = navigation_findnearestwaypoint(this, false);
324         if (!e) return;
325         if (e.wpflags & WAYPOINTFLAG_GENERATED) return;
326         bprint(strcat("Waypoint removed at ", vtos(e.origin), "\n"));
327         waypoint_remove(e);
328 }
329
330 IMPULSE(navwaypoint_relink, 105)
331 {
332         if (!autocvar_g_waypointeditor) return;
333         waypoint_schedulerelinkall();
334 }
335
336 IMPULSE(navwaypoint_save, 106)
337 {
338         if (!autocvar_g_waypointeditor) return;
339         waypoint_saveall();
340 }
341
342 IMPULSE(navwaypoint_debug, 107)
343 {
344         if (!autocvar_g_waypointeditor) return;
345         for (entity e = findchain(classname, "waypoint"); e; e = e.chain)
346         {
347                 e.colormod = '0.5 0.5 0.5';
348                 e.effects &= ~(EF_NODEPTHTEST | EF_RED | EF_BLUE);
349         }
350         entity e2 = navigation_findnearestwaypoint(this, false);
351         navigation_markroutes(e2);
352
353         int i, m;
354
355         i = 0;
356         m = 0;
357         for (entity e = findchain(classname, "waypoint"); e; e = e.chain)
358         {
359                 if (e.wpcost < 10000000) continue;
360                 LOG_INFO("unreachable: ", etos(e), " ", vtos(e.origin), "\n");
361                 e.colormod_z = 8;
362                 e.effects |= EF_NODEPTHTEST | EF_BLUE;
363                 ++i;
364                 ++m;
365         }
366         if (i) LOG_INFOF("%d waypoints cannot be reached from here in any way (marked with blue light)\n", i);
367         navigation_markroutes_inverted(e2);
368
369         i = 0;
370         for (entity e = findchain(classname, "waypoint"); e; e = e.chain)
371         {
372                 if (e.wpcost < 10000000) continue;
373                 LOG_INFO("cannot reach me: ", etos(e), " ", vtos(e.origin), "\n");
374                 e.colormod_x = 8;
375                 if (!(e.effects & EF_NODEPTHTEST))  // not already reported before
376                         ++m;
377                 e.effects |= EF_NODEPTHTEST | EF_RED;
378                 ++i;
379         }
380         if (i) LOG_INFOF("%d waypoints cannot walk to here in any way (marked with red light)\n", i);
381         if (m) LOG_INFOF("%d waypoints have been marked total\n", m);
382
383         i = 0;
384         for (entity e = findchain(classname, "info_player_deathmatch"); e; e = e.chain)
385         {
386                 vector org = e.origin;
387                 tracebox(e.origin, PL_MIN, PL_MAX, e.origin - '0 0 512', MOVE_NOMONSTERS, world);
388                 setorigin(e, trace_endpos);
389                 if (navigation_findnearestwaypoint(e, false))
390                 {
391                         setorigin(e, org);
392                         e.effects &= ~EF_NODEPTHTEST;
393                         e.model = "";
394                 }
395                 else
396                 {
397                         setorigin(e, org);
398                         LOG_INFO("spawn without waypoint: ", etos(e), " ", vtos(e.origin), "\n");
399                         e.effects |= EF_NODEPTHTEST;
400                         _setmodel(e, this.model);
401                         e.frame = this.frame;
402                         e.skin = this.skin;
403                         e.colormod = '8 0.5 8';
404                         setsize(e, '0 0 0', '0 0 0');
405                         ++i;
406                 }
407         }
408         if (i) LOG_INFOF("%d spawnpoints have no nearest waypoint (marked by player model)\n", i);
409
410         i = 0;
411         entity start = findchainflags(flags, FL_ITEM);
412         for (entity e = start; e; e = e.chain)
413         {
414                 e.effects &= ~(EF_NODEPTHTEST | EF_RED | EF_BLUE);
415                 e.colormod = '0.5 0.5 0.5';
416         }
417         for (entity e = start; e; e = e.chain)
418         {
419                 if (navigation_findnearestwaypoint(e, false)) continue;
420                 LOG_INFO("item without waypoint: ", etos(e), " ", vtos(e.origin), "\n");
421                 e.effects |= EF_NODEPTHTEST | EF_RED;
422                 e.colormod_x = 8;
423                 ++i;
424         }
425         if (i) LOG_INFOF("%d items have no nearest waypoint and cannot be walked away from (marked with red light)\n", i);
426
427         i = 0;
428         for (entity e = start; e; e = e.chain)
429         {
430                 if (navigation_findnearestwaypoint(e, true)) continue;
431                 LOG_INFO("item without waypoint: ", etos(e), " ", vtos(e.origin), "\n");
432                 e.effects |= EF_NODEPTHTEST | EF_BLUE;
433                 e.colormod_z = 8;
434                 ++i;
435         }
436         if (i) LOG_INFOF("%d items have no nearest waypoint and cannot be walked to (marked with blue light)\n", i);
437 }