]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/monsters/monster/mage.qc
Transifex autosync
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / monsters / monster / mage.qc
1 #include "mage.qh"
2
3 #ifdef SVQC
4 float autocvar_g_monster_mage_health;
5 float autocvar_g_monster_mage_damageforcescale = 0.5;
6 float autocvar_g_monster_mage_attack_spike_damage;
7 float autocvar_g_monster_mage_attack_spike_radius;
8 float autocvar_g_monster_mage_attack_spike_delay;
9 float autocvar_g_monster_mage_attack_spike_accel;
10 float autocvar_g_monster_mage_attack_spike_decel;
11 float autocvar_g_monster_mage_attack_spike_chance = 0.45;
12 float autocvar_g_monster_mage_attack_spike_turnrate;
13 float autocvar_g_monster_mage_attack_spike_speed_max;
14 float autocvar_g_monster_mage_attack_spike_smart;
15 float autocvar_g_monster_mage_attack_spike_smart_trace_min;
16 float autocvar_g_monster_mage_attack_spike_smart_trace_max;
17 float autocvar_g_monster_mage_attack_spike_smart_mindist;
18 float autocvar_g_monster_mage_attack_push_chance = 0.7;
19 float autocvar_g_monster_mage_attack_push_damage;
20 float autocvar_g_monster_mage_attack_push_radius;
21 float autocvar_g_monster_mage_attack_push_delay;
22 float autocvar_g_monster_mage_attack_push_force;
23 float autocvar_g_monster_mage_attack_teleport_chance = 0.2;
24 float autocvar_g_monster_mage_attack_teleport_delay = 2;
25 float autocvar_g_monster_mage_attack_teleport_random = 0.4;
26 float autocvar_g_monster_mage_attack_teleport_random_range = 1200;
27 float autocvar_g_monster_mage_heal_allies;
28 float autocvar_g_monster_mage_heal_minhealth;
29 float autocvar_g_monster_mage_heal_range;
30 float autocvar_g_monster_mage_heal_delay;
31 float autocvar_g_monster_mage_shield_time;
32 float autocvar_g_monster_mage_shield_delay;
33 float autocvar_g_monster_mage_shield_blockpercent;
34 float autocvar_g_monster_mage_speed_stop;
35 float autocvar_g_monster_mage_speed_run;
36 float autocvar_g_monster_mage_speed_walk;
37
38 SOUND(MageSpike_FIRE, W_Sound("electro_fire"));
39 void M_Mage_Attack_Spike(entity this, vector dir);
40 void M_Mage_Attack_Push(entity this);
41 METHOD(MageSpike, wr_think, void(MageSpike thiswep, entity actor, .entity weaponentity, int fire))
42 {
43     TC(MageSpike, thiswep);
44     if (fire & 1)
45     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, weaponentity, false, 0.2)) {
46         if (!actor.target_range) actor.target_range = autocvar_g_monsters_target_range;
47         actor.enemy = Monster_FindTarget(actor);
48         monster_makevectors(actor, actor.enemy);
49         W_SetupShot_Dir(actor, weaponentity, v_forward, false, 0, SND_MageSpike_FIRE, CH_WEAPON_B, 0, DEATH_MONSTER_MAGE.m_id);
50         if (!IS_PLAYER(actor)) w_shotdir = normalize((actor.enemy.origin + '0 0 10') - actor.origin);
51         M_Mage_Attack_Spike(actor, w_shotdir);
52         weapon_thinkf(actor, weaponentity, WFRAME_FIRE1, 0, w_ready);
53     }
54     if (fire & 2)
55     if (!IS_PLAYER(actor) || weapon_prepareattack(thiswep, actor, weaponentity, true, 0.5)) {
56         M_Mage_Attack_Push(actor);
57         weapon_thinkf(actor, weaponentity, WFRAME_FIRE2, 0, w_ready);
58     }
59 }
60
61 void M_Mage_Attack_Teleport(entity this, entity targ);
62
63 CLASS(OffhandMageTeleport, OffhandWeapon)
64     .bool OffhandMageTeleport_key_pressed;
65     METHOD(OffhandMageTeleport, offhand_think, void(OffhandMageTeleport this, entity player, bool key_pressed))
66     {
67         TC(OffhandMageTeleport, this);
68         if (key_pressed && !player.OffhandMageTeleport_key_pressed)
69                 M_Mage_Attack_Teleport(player, player.enemy);
70         player.OffhandMageTeleport_key_pressed = key_pressed;
71     }
72 ENDCLASS(OffhandMageTeleport)
73 OffhandMageTeleport OFFHAND_MAGE_TELEPORT;
74 STATIC_INIT(OFFHAND_MAGE_TELEPORT) { OFFHAND_MAGE_TELEPORT = NEW(OffhandMageTeleport); }
75
76 void M_Mage_Defend_Heal(entity this);
77 void M_Mage_Defend_Shield(entity this);
78
79 .entity mage_spike;
80 .float mage_shield_delay;
81
82 bool M_Mage_Defend_Heal_Check(entity this, entity targ)
83 {
84         if(targ == NULL)
85                 return false;
86         if(GetResource(targ, RES_HEALTH) <= 0)
87                 return false;
88         if(DIFF_TEAM(targ, this) && targ != this.monster_follow)
89                 return false;
90         if(STAT(FROZEN, targ))
91                 return false;
92         if(!IS_PLAYER(targ))
93                 return (IS_MONSTER(targ) && GetResource(targ, RES_HEALTH) < targ.max_health);
94         if(StatusEffects_active(STATUSEFFECT_Shield, targ))
95                 return false;
96
97         switch(this.skin)
98         {
99                 case 0: return (GetResource(targ, RES_HEALTH) < autocvar_g_balance_health_regenstable);
100                 case 1:
101                 {
102                         return ((GetResource(targ, RES_CELLS) && GetResource(targ, RES_CELLS) < g_pickup_cells_max)
103                                 ||  (GetResource(targ, RES_PLASMA) && GetResource(targ, RES_PLASMA) < g_pickup_plasma_max)
104                                 ||  (GetResource(targ, RES_ROCKETS) && GetResource(targ, RES_ROCKETS) < g_pickup_rockets_max)
105                                 ||  (GetResource(targ, RES_BULLETS) && GetResource(targ, RES_BULLETS) < g_pickup_nails_max)
106                                 ||  (GetResource(targ, RES_SHELLS) && GetResource(targ, RES_SHELLS) < g_pickup_shells_max)
107                                         );
108                 }
109                 case 2: return (GetResource(targ, RES_ARMOR) < autocvar_g_balance_armor_regenstable);
110         }
111
112         return false;
113 }
114
115 void M_Mage_Attack_Spike_Explode(entity this, entity directhitentity)
116 {
117         this.event_damage = func_null;
118
119         sound(this, CH_SHOTS, SND_GRENADE_IMPACT, VOL_BASE, ATTEN_NORM);
120
121         this.realowner.mage_spike = NULL;
122
123         Send_Effect(EFFECT_EXPLOSION_SMALL, this.origin, '0 0 0', 1);
124         RadiusDamage (this, this.realowner, (autocvar_g_monster_mage_attack_spike_damage), (autocvar_g_monster_mage_attack_spike_damage) * 0.5, (autocvar_g_monster_mage_attack_spike_radius),
125                                                 NULL, NULL, 0, DEATH_MONSTER_MAGE.m_id, DMG_NOWEP, directhitentity);
126
127         delete(this);
128 }
129
130 void M_Mage_Attack_Spike_Touch(entity this, entity toucher)
131 {
132         PROJECTILE_TOUCH(this, toucher);
133
134         M_Mage_Attack_Spike_Explode(this, toucher);
135 }
136
137 .float wait;
138
139 // copied from W_Seeker_Think
140 void M_Mage_Attack_Spike_Think(entity this)
141 {
142         if (time > this.ltime || (this.enemy && GetResource(this.enemy, RES_HEALTH) <= 0) || !this.owner || GetResource(this.owner, RES_HEALTH) <= 0) {
143                 this.projectiledeathtype |= HITTYPE_SPLASH;
144                 M_Mage_Attack_Spike_Explode(this, NULL);
145         }
146
147         float spd = vlen(this.velocity);
148         spd = bound(
149                 spd - (autocvar_g_monster_mage_attack_spike_decel) * frametime,
150                 (autocvar_g_monster_mage_attack_spike_speed_max),
151                 spd + (autocvar_g_monster_mage_attack_spike_accel) * frametime
152         );
153
154         if (this.enemy != NULL)
155                 if (this.enemy.takedamage != DAMAGE_AIM || IS_DEAD(this.enemy))
156                         this.enemy = NULL;
157
158         if (this.enemy != NULL)
159         {
160                 entity e = this.enemy;
161                 vector eorg = 0.5 * (e.absmin + e.absmax);
162                 float turnrate = (autocvar_g_monster_mage_attack_spike_turnrate); // how fast to turn
163                 vector desireddir = normalize(eorg - this.origin);
164                 vector olddir = normalize(this.velocity); // get my current direction
165
166                 // Do evasive maneuvers for world objects? ( this should be a cpu hog. :P )
167                 if ((autocvar_g_monster_mage_attack_spike_smart) && vdist(eorg - this.origin, >, autocvar_g_monster_mage_attack_spike_smart_mindist))
168                 {
169                         // Is it a better idea (shorter distance) to trace to the target itself?
170                         if ( vlen2(this.origin + olddir * this.wait) < vlen2(eorg - this.origin))
171                                 traceline(this.origin, this.origin + olddir * this.wait, false, this);
172                         else
173                                 traceline(this.origin, eorg, false, this);
174
175                         // Setup adaptive tracelength
176                         this.wait = bound((autocvar_g_monster_mage_attack_spike_smart_trace_min), vlen(this.origin - trace_endpos), this.wait = (autocvar_g_monster_mage_attack_spike_smart_trace_max));
177
178                         // Calc how important it is that we turn and add this to the desierd (enemy) dir.
179                         desireddir = normalize(((trace_plane_normal * (1 - trace_fraction)) + (desireddir * trace_fraction)) * 0.5);
180                 }
181
182                 vector newdir = normalize(olddir + desireddir * turnrate); // take the average of the 2 directions; not the best method but simple & easy
183                 this.velocity = newdir * spd; // make me fly in the new direction at my flight speed
184         }
185
186         ///////////////
187
188         //this.angles = vectoangles(this.velocity);                     // turn model in the new flight direction
189         this.nextthink = time;// + 0.05; // csqc projectiles
190         UpdateCSQCProjectile(this);
191 }
192
193 void M_Mage_Attack_Spike(entity this, vector dir)
194 {
195         makevectors(this.angles);
196
197         entity missile = new(M_Mage_Attack_Spike);
198         missile.owner = missile.realowner = this;
199         setthink(missile, M_Mage_Attack_Spike_Think);
200         missile.ltime = time + 7;
201         missile.nextthink = time;
202         missile.solid = SOLID_BBOX;
203         set_movetype(missile, MOVETYPE_FLYMISSILE);
204         missile.flags = FL_PROJECTILE;
205         IL_PUSH(g_projectiles, missile);
206         IL_PUSH(g_bot_dodge, missile);
207         setorigin(missile, this.origin + v_forward * 14 + '0 0 30' + v_right * -14);
208         setsize(missile, '0 0 0', '0 0 0');
209         missile.velocity = dir * 400;
210         missile.avelocity = '300 300 300';
211         missile.enemy = this.enemy;
212         settouch(missile, M_Mage_Attack_Spike_Touch);
213
214         this.mage_spike = missile;
215
216         CSQCProjectile(missile, true, PROJECTILE_MAGE_SPIKE, true);
217 }
218
219 void M_Mage_Defend_Heal(entity this)
220 {
221         bool washealed = false;
222
223         FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_monster_mage_heal_range, M_Mage_Defend_Heal_Check(this, it),
224         {
225                 washealed = true;
226                 entity fx = EFFECT_Null;
227                 if(IS_PLAYER(it))
228                 {
229                         switch(this.skin)
230                         {
231                                 case 0:
232                                 {
233                                         Heal(it, this, autocvar_g_monster_mage_heal_allies, autocvar_g_balance_health_regenstable);
234                                         fx = EFFECT_HEALING;
235                                         break;
236                                 }
237                                 case 1:
238                                 {
239                                         if(GetResource(it, RES_CELLS)) GiveResourceWithLimit(it, RES_CELLS, 1, g_pickup_cells_max);
240                                         if(GetResource(it, RES_PLASMA)) GiveResourceWithLimit(it, RES_PLASMA, 1, g_pickup_plasma_max);
241                                         if(GetResource(it, RES_ROCKETS)) GiveResourceWithLimit(it, RES_ROCKETS, 1, g_pickup_rockets_max);
242                                         if(GetResource(it, RES_SHELLS)) GiveResourceWithLimit(it, RES_SHELLS, 2, g_pickup_shells_max);
243                                         if(GetResource(it, RES_BULLETS)) GiveResourceWithLimit(it, RES_BULLETS, 5, g_pickup_nails_max);
244                                         // TODO: fuel?
245                                         fx = EFFECT_AMMO_REGEN;
246                                         break;
247                                 }
248                                 case 2:
249                                         if(GetResource(it, RES_ARMOR) < autocvar_g_balance_armor_regenstable)
250                                         {
251                                                 GiveResourceWithLimit(it, RES_ARMOR, autocvar_g_monster_mage_heal_allies, autocvar_g_balance_armor_regenstable);
252                                                 fx = EFFECT_ARMOR_REPAIR;
253                                         }
254                                         break;
255                         }
256
257                         Send_Effect(fx, it.origin, '0 0 0', 1);
258                 }
259                 else
260                 {
261                         Send_Effect(EFFECT_HEALING, it.origin, '0 0 0', 1);
262                         Heal(it, this, autocvar_g_monster_mage_heal_allies, RES_LIMIT_NONE);
263                         if(!(it.spawnflags & MONSTERFLAG_INVINCIBLE) && it.sprite)
264                                 WaypointSprite_UpdateHealth(it.sprite, GetResource(it, RES_HEALTH));
265                 }
266         });
267
268         if(washealed)
269         {
270                 setanim(this, this.anim_melee, true, true, true);
271                 this.attack_finished_single[0] = time + (autocvar_g_monster_mage_heal_delay);
272                 this.state = MONSTER_ATTACK_MELEE;
273                 this.anim_finished = time + 1.5;
274         }
275 }
276
277 void M_Mage_Attack_Push(entity this)
278 {
279         sound(this, CH_SHOTS, SND_TAGEXP1, 1, ATTEN_NORM);
280         RadiusDamage (this, this, (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_damage), (autocvar_g_monster_mage_attack_push_radius),
281                                                 NULL, NULL, (autocvar_g_monster_mage_attack_push_force), DEATH_MONSTER_MAGE.m_id, DMG_NOWEP, this.enemy);
282         Send_Effect(EFFECT_TE_EXPLOSION, this.origin, '0 0 0', 1);
283
284         setanim(this, this.anim_duckjump, true, true, true);
285         this.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_push_delay);
286         this.anim_finished = time + 1;
287         this.state = MONSTER_ATTACK_MELEE; // prevent moving while firing spike
288 }
289
290 void M_Mage_Attack_Teleport(entity this, entity targ)
291 {
292         if(!targ) return;
293         if(vdist(targ.origin - this.origin, >, autocvar_g_monster_mage_attack_teleport_random_range)) return;
294
295         if(autocvar_g_monster_mage_attack_teleport_random && random() <= autocvar_g_monster_mage_attack_teleport_random)
296         {
297                 vector oldpos = this.origin;
298                 vector extrasize = '1 1 1' * autocvar_g_monster_mage_attack_teleport_random_range;
299                 if(MoveToRandomLocationWithinBounds(this, this.absmin - extrasize, this.absmax + extrasize,
300                                                                                         DPCONTENTS_SOLID | DPCONTENTS_CORPSE | DPCONTENTS_PLAYERCLIP, DPCONTENTS_SLIME | DPCONTENTS_LAVA | DPCONTENTS_SKY | DPCONTENTS_BODY | DPCONTENTS_DONOTENTER, 
301                                                                                         Q3SURFACEFLAG_SKY, 10, 64, 256, true))
302                 {
303                         vector a = vectoangles(targ.origin - this.origin);
304                         this.angles = '0 1 0' * a.y;
305                         this.fixangle = true;
306                         Send_Effect(EFFECT_SPAWN_NEUTRAL, oldpos, '0 0 0', 1);
307                         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
308                         this.attack_finished_single[0] = time + autocvar_g_monster_mage_attack_teleport_delay;
309                         return;
310                 }
311         }
312
313         if(!IS_ONGROUND(targ)) return;
314
315         makevectors(targ.angles);
316         tracebox(CENTER_OR_VIEWOFS(targ), this.mins, this.maxs, CENTER_OR_VIEWOFS(targ) + ((v_forward * -1) * 200), MOVE_NOMONSTERS, this);
317
318         if(trace_fraction < 1)
319                 return;
320
321         vector newpos = trace_endpos;
322
323         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
324         Send_Effect(EFFECT_SPAWN_NEUTRAL, newpos, '0 0 0', 1);
325
326         setorigin(this, newpos);
327
328         vector a = vectoangles(targ.origin - this.origin);
329         a.x = -a.x;
330         this.angles_x = a.x;
331         this.angles_y = a.y;
332         this.fixangle = true;
333         this.velocity *= 0.5;
334
335         this.attack_finished_single[0] = time + autocvar_g_monster_mage_attack_teleport_delay;
336 }
337
338 void M_Mage_Defend_Shield(entity this)
339 {
340         StatusEffects_apply(STATUSEFFECT_Shield, this, time + autocvar_g_monster_mage_shield_time, 0);
341         this.mage_shield_delay = time + (autocvar_g_monster_mage_shield_delay);
342         SetResourceExplicit(this, RES_ARMOR, autocvar_g_monster_mage_shield_blockpercent);
343         setanim(this, this.anim_shoot, true, true, true);
344         this.attack_finished_single[0] = time + 1; // give just a short cooldown on attacking
345         this.anim_finished = time + 1;
346 }
347
348 bool M_Mage_Attack(int attack_type, entity actor, entity targ, .entity weaponentity)
349 {
350         switch(attack_type)
351         {
352                 case MONSTER_ATTACK_MELEE:
353                 {
354                         if(random() <= autocvar_g_monster_mage_attack_push_chance)
355                         {
356                                 Weapon wep = WEP_MAGE_SPIKE;
357
358                                 wep.wr_think(wep, actor, weaponentity, 2);
359                                 return true;
360                         }
361
362                         return false;
363                 }
364                 case MONSTER_ATTACK_RANGED:
365                 {
366                         if(random() <= autocvar_g_monster_mage_attack_teleport_chance)
367                         {
368                                 OffhandWeapon off = OFFHAND_MAGE_TELEPORT;
369                                 actor.OffhandMageTeleport_key_pressed = 0;
370                                 off.offhand_think(off, actor, 1);
371                                 return true;
372                         }
373                         else if(!actor.mage_spike && random() <= autocvar_g_monster_mage_attack_spike_chance)
374                         {
375                                 setanim(actor, actor.anim_shoot, true, true, true);
376                                 actor.attack_finished_single[0] = time + (autocvar_g_monster_mage_attack_spike_delay);
377                                 actor.anim_finished = time + 1;
378                                 actor.state = MONSTER_ATTACK_MELEE; // prevent moving while firing spike
379                                 Weapon wep = WEP_MAGE_SPIKE;
380                                 wep.wr_think(wep, actor, weaponentity, 1);
381                                 return true;
382                         }
383
384                         return false;
385                 }
386         }
387
388         return false;
389 }
390
391 spawnfunc(monster_mage) { Monster_Spawn(this, true, MON_MAGE); }
392
393 #endif // SVQC
394
395 #ifdef SVQC
396 METHOD(Mage, mr_think, bool(Mage thismon, entity actor))
397 {
398     TC(Mage, thismon);
399     bool need_help = false;
400
401     FOREACH_CLIENT(IS_PLAYER(it) && it != actor,
402     {
403         if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
404         if(M_Mage_Defend_Heal_Check(actor, it))
405         {
406             need_help = true;
407             break;
408         }
409     });
410
411     if(!need_help)
412     {
413         IL_EACH(g_monsters, it != actor,
414         {
415                 if(vdist(it.origin - actor.origin, <=, autocvar_g_monster_mage_heal_range))
416                 if(M_Mage_Defend_Heal_Check(actor, it))
417                 {
418                     need_help = true;
419                     break;
420                 }
421         });
422     }
423
424     if(GetResource(actor, RES_HEALTH) < (autocvar_g_monster_mage_heal_minhealth) || need_help)
425     if(time >= actor.attack_finished_single[0])
426     if(random() < 0.5)
427         M_Mage_Defend_Heal(actor);
428
429     if(actor.enemy && time >= actor.mage_shield_delay && random() < 0.5)
430     if(GetResource(actor, RES_HEALTH) < actor.max_health && !StatusEffects_active(STATUSEFFECT_Shield, actor))
431         M_Mage_Defend_Shield(actor);
432
433     return true;
434 }
435
436 METHOD(Mage, mr_pain, float(Mage this, entity actor, float damage_take, entity attacker, float deathtype))
437 {
438     TC(Mage, this);
439     return damage_take;
440 }
441
442 METHOD(Mage, mr_death, bool(Mage this, entity actor))
443 {
444     TC(Mage, this);
445     setanim(actor, ((random() > 0.5) ? actor.anim_die2 : actor.anim_die1), false, true, true);
446     return true;
447 }
448
449 #endif
450 #ifdef GAMEQC
451 METHOD(Mage, mr_anim, bool(Mage this, entity actor))
452 {
453     TC(Mage, this);
454     vector none = '0 0 0';
455     actor.anim_idle = animfixfps(actor, '0 1 1', none);
456     actor.anim_walk = animfixfps(actor, '1 1 1', none);
457     actor.anim_run = animfixfps(actor, '1 1 1', none);
458     actor.anim_shoot = animfixfps(actor, '2 1 5', none); // analyze models and set framerate
459     actor.anim_duckjump = animfixfps(actor, '4 1 5', none); // analyze models and set framerate
460     actor.anim_melee = animfixfps(actor, '5 1 5', none); // analyze models and set framerate
461     //actor.anim_fire1 = animfixfps(actor, '3 1 5', none); // analyze models and set framerate
462     //actor.anim_fire2 = animfixfps(actor, '4 1 5', none); // analyze models and set framerate
463     //actor.anim_fire3 = animfixfps(actor, '5 1 5', none); // analyze models and set framerate
464     actor.anim_pain1 = animfixfps(actor, '6 1 2', none); // 0.5 seconds
465     actor.anim_pain2 = animfixfps(actor, '7 1 2', none); // 0.5 seconds
466     //actor.anim_pain3 = animfixfps(actor, '8 1 2', none); // 0.5 seconds
467     actor.anim_die1 = animfixfps(actor, '9 1 0.5', none); // 2 seconds
468     actor.anim_die2 = animfixfps(actor, '10 1 0.5', none); // 2 seconds
469     //actor.anim_dead1 = animfixfps(actor, '11 1 0.5', none); // 2 seconds
470     //actor.anim_dead2 = animfixfps(actor, '12 1 0.5', none); // 2 seconds
471     return true;
472 }
473 #endif
474 #ifdef SVQC
475 .float speed;
476 METHOD(Mage, mr_setup, bool(Mage this, entity actor))
477 {
478     TC(Mage, this);
479     if(!GetResource(this, RES_HEALTH)) SetResourceExplicit(actor, RES_HEALTH, autocvar_g_monster_mage_health);
480     if(!actor.speed) { actor.speed = (autocvar_g_monster_mage_speed_walk); }
481     if(!actor.speed2) { actor.speed2 = (autocvar_g_monster_mage_speed_run); }
482     if(!actor.stopspeed) { actor.stopspeed = (autocvar_g_monster_mage_speed_stop); }
483     if(!actor.damageforcescale) { actor.damageforcescale = (autocvar_g_monster_mage_damageforcescale); }
484
485     actor.monster_loot = ITEM_HealthBig;
486     actor.monster_attackfunc = M_Mage_Attack;
487
488     return true;
489 }
490 #endif